简体   繁体   中英

Is there a “verbatim” mode for the vim map command?

I am trying to set up some useful coding templates in vim, for example I have mapped

map `cl iclass <+CLASSNAME+><CR>{<CR><Esc>Iprotected:<CR><+PROTECTED MEMBERS+><CR><Esc>Ipublic:<CR><+PUBLIC INTERFACE+><CR>};<CR><++><CR><Esc>3kv<0v3k<2k

so that when I type `cl in vim I get

class <+CLASSNAME+>
{
protected:
    <+PROTECTED MEMBERS+>
public:
    <+PUBLIC INTERFACE+>
};
<++>

(so that I can jump between the <+ +> tags with Cj). This works fine, but I find the above remap pretty obscure. Is there a way to enter what I want vim to type in "verbatim mode"? So I would want to write something like

map `cl i{VERBATIMSTART}class <+CLASSNAME+>
{
protected:
    <+PROTECTED MEMBERS+>
public:
    <+PUBLIC INTERFACE+>
};
<++>{VERBATIMEND}

?

Thank you Paul

I don't know if there is such a "verbatim"-mode for mappings. I, personally, would use one of the snippet-plugins to do this. See www.vim.org and search for "snippet". I have not tried all of them (just SnippetsMgr ;-) ), but I suppose that they are handier to define multi-line-snippets.

Some of the available snippet-plugins on vim.org: snippets.vim , snippetsEmu, snipMate, SnippetsMgr, etc.

As Habi has mentioned, one way to go about this is with a snippet plugin.

Another way is to copy that snippet of code into its own file and set up your mapping to insert that file below the cursor:

map `cl :r /path/to/code_snippet<CR>

Kind of obvious (and probably not what you want) is:

map `cl iclass <+CLASSNAME+>
\<CR>{
\<CR>protected:
\<CR>    <+PROTECTED MEMBERS+>
\<CR>public:
\<CR>    <+PUBLIC INTERFACE+>
\<CR>};
\<CR><++>
\<CR>

\\ in beginning of line tells that the line is the continuation of the previous one. But this is rather literal continuation: it doesn't add new lines so one has to add them manually. Since it uses the insert mode, the operation would be also affected by the current indentation mode. (Though one can try to work that around with :set paste / :set nopaste .)

I would have tried to put the text into a temp variable or register then P put (or :put ) it into the buffer. Eg setreg() allows one to tell that the content of a register are lines and thus P utting it would work regardless of indentation.

Otherwise, looking in :help line-continuation or :help variables I see no way how one can specify a multi-line string or text.

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