</script> But during launch vim says:" />
开发者

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?


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.


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


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.


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

:iab <s <script>

typing <s then space gets "<script>"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜