tags:

views:

68

answers:

3

Dear all,

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 C-j). 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

+4  A: 

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.

Habi
Hi Habi,thanks for pointing this out, I'll have a look...
Paul
+1 for snipMate. It is perhaps the most mature of those listed. (SnippetsMgr is the only I haven't tried)
sleepynate
+1  A: 

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 Pput (or :put) it into the buffer. E.g. setreg() allows one to tell that the content of a register are lines and thus Putting 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.

Dummy00001
Another silly option is to use list (`:help List`) to store the line and use `join()` to automatically add \n between the lines. But again, I do not see any syntax which might facilitate something like that.
Dummy00001
Real silly option: put the text into a file (some fixed location) and simply paste its content.
Dummy00001
+4  A: 

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>
Curt Nelson
+1 for mentionning :read
matias