Denis Chmel
Denis Chmel

Reputation: 789

How to make vim substitute <script with <script type="..."></script>

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?

Upvotes: 0

Views: 406

Answers (4)

joharr
joharr

Reputation: 535

Just using two characters works in gVim (no idea why):

:iab <s <script>

typing <s then space gets "<script>"

Upvotes: 0

ZyX
ZyX

Reputation: 53654

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.

Upvotes: 0

J&#246;rn Horstmann
J&#246;rn Horstmann

Reputation: 34034

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

Upvotes: 2

mawimawi
mawimawi

Reputation: 4353

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.

Upvotes: 3

Related Questions