tags:

views:

74

answers:

3

I created the following rule:

iabbrev <lt>script <script type="text/javascript"></script>

But during launch vim says:

Error detected while processing .vimrc:
line 290:
E474: Invalid argument

Pointing on that line. And I cannot workaround this, seems like the problem is in the left part, just like "script" is a reserved word. Is there a way to escape it? Or am I doing something wrong here?

+3  A: 

consider using something like SnipMate: http://www.vim.org/scripts/script.php?script_id=2540

Then you have many macros already at hand, e.g.

snippet script
   <script type="text/javascript" charset="utf-8">
       ${1}
   </script>${2}

in html.snippet

to use it, type in insert mode the following: script<TAB> - the snipped will be inserted (without the ${n} pieces) and the cursor will be where ${1} was. Type your code, then press <TAB> again (still in insert mode), and the cursor will be after </script>.

There are MANY more snippets shipped with Snipmate for many languages. And it's really easy to create your own snippets as well.

mawimawi
Thanks for hinting! Will take a look on that as well.
Denis Chmel
+2  A: 

After several trys and reading of the documentation I don't think this mapping is possible. Citing from :help abbreviations:

There are three types of abbreviations:

full-id The "full-id" type consists entirely of keyword characters (letters and characters from 'iskeyword' option). This is the most common abbreviation.

Examples: "foo", "g3", "-1"

end-id The "end-id" type ends in a keyword character, but all the other characters are not keyword characters.

Examples: "#i", "..f", "$/7"

non-id The "non-id" type ends in a non-keyword character, the other characters may be of any type, excluding space and tab. {this type is not supported by Vi}

Examples: "def#", "4/7$"

Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"

So an abbreviation like <script seems to be impossible. You could of course define an abbreviation for script like this:

:inoreabbrev script <script type="text/javascript"></script>

This way the opening bracket isn't part of the abbreviation and so it is also not needed in the expansion. The only problem is that you have to be careful when you want to write the text script. In that case you have to switch out of insert mode in the middle of the word or type script<C-V><space>.

Another useful trick to place the cursor in between the tags is a mapping like this, which jumps backwards to the previous opening bracket:

:inoreabbrev script <script type="text/javascript"></script><C-O>F<

Or, to place the tags on different lines with the cursor between them:

:inoreabbrev script <script type="text/javascript"><CR></script><C-O>O
Jörn Horstmann
Even if you thought it was not possible, what prevented you from adding `<` after you have wrote `script` (for example, using `<C-o>3Fs<`)?
ZyX
Thanks, however it's weird that <script cannot be an abbreviation. Everything works fine with <script> for instance.
Denis Chmel
@Denis Chmel While trying to find a workaround I noticed two things: 1. `<sc` also does not work. 2. Everything works in help window. Latter is very strange.
ZyX
The allowed characters in abbreviations depend on the setting of 'iskeyword'. In help mode nearly all printable ascii characters are allowed as keywords. You can check the setting with `:set iskeyword?`.
Jörn Horstmann
A: 

Thanks to @Jörn Horstmann and @James Vega (from vim-dev mailing list) the problem may be solved: just add < to 'iskeyword' option, for example using

set iskeyword+=60 " 60==char2nr("<")

Note that 'iskeyword' option is used in lots of motions, so this may cause some negative side effects.

ZyX
That's works, awesome! :)Cool that there's an academic solution, bad that it's so deep and has side effects. However many thanks for your help!
Denis Chmel