简体   繁体   中英

Why does this uncommon VIM dot concatenation syntax work, while my common VIM syntax doesn't?

Customizing my .vimrc I added template (skeleton) file support. This auto-command rule from a book didn't do anything (no errors nor templates):

:autocmd BufNewFile * silent! Or $HOME/.vim/templates/%:e.tpl

This non-%e-wildcard version produced an error:

:autocmd BufNewFile *.html Or $HOME/.vim/templates/html.tpl

"test.html" [New File]
Error detected while processing BufNewFile Auto commands for "*.html":
E492: Not an editor command: Or $HOME/.vim/templates/html.tpl

In another post user orftz (+1) showed us an uncommon syntax (what I'd recognize as PHP) using concatenation and double quotes.

:autocmd BufNewFile * :silent! :exec ":0r " . $HOME . "/.vim/templates/" . &ft . ".tpl"

What's wrong with my version of the common and why does the other version work (I don't see any quoting in other vim scripts like this).

Quoting or concatenation has nothing to do with this.

The original command most likely doesn't do anything because you forgot to declare the Or command. I obviously don't know what the book you referring to uses, but it most likely looks like :command -nargs=1 Or read for example.

Your second command fails because you also removed silent! . This statement will suppress all errors. Since you removed it, Vim will now warn you that Or <argument> is not known by the editor, so the missing %:e has nothing to with it.

If you want to know more about %:e , read http://vimdoc.sourceforge.net/htmldoc/cmdline.html#%:e - it will be substituted by the extension of the current file.


EDIT: I see you were also confused by the . operator which can be used for string concatenation, and rightly so.

. simply means "concatenate the first string with the second", eg let val2 = 'value ' . val1 let val2 = 'value ' . val1 .

The hairy thing is that Vim has a few commands which do concatenation themselves, :execute , :echo and :echomsg to just name a few. Such commands will automatically add in a space character if you pass it several expressions. The following commands should all be equivalent:

:exec ":0r" $HOME . "/.vim/templates/" . &ft . ".tpl" :exec ":0r " . $HOME . "/.vim/templates/" . &ft . ".tpl" :exec (":0r " . $HOME . "/.vim/templates/" . &ft . ".tpl") :exec ":0r" ($HOME . "/.vim/templates/" . &ft . ".tpl")

This just goes to say that Vim script is all but pretty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM