vim function

Alex Esplin alex.esplin at
Thu Aug 10 15:06:39 MDT 2006

On 8/10/06, Alan Young <alansyoungiii at> wrote:
> I was showing my .vimrc last night to NUPM (and those who came early for
> PLUG) and ran into problems with a function I was trying to create.  I
> got it fixed and here it is:
> function! AddBlock( KeyWord )
>   return a:KeyWord . " {\<CR>\<CR>}\<ESC>bmbbb\<LEFT>\<Insert>"
> endfunction
> imap IF <C-R>=AddBlock( "if (  )" )<Enter>
> imap WH <C-R>=AddBlock( "while (  )") )<Enter>
> Put this in your ftplugin/perl.vim file (whereever you have put it) and
> while in insert mode you can type IF and you'll get the following:
> if ( _ ) {
> }
> (the '_' is where the cursor will be).  The only problem I have with
> this is moving to inside the block.  You can either use your arrow keys,
> if they're mapped, or you can press <Esc>'b and you'll be placed on the
> blank line between the curly braces.  There's got to be a better way.
> Any ideas?

put this in your .vimrc: (mapping can be different if you want, I
prefer to use a \ to start my function maps)

"map a shortcut to create an 'if' block
map \if iif ()<CR>{<CR>}<Esc>?(<CR>a

this produces:
if ()

This will place the cursor in "insert mode" between the () in your
'if' statement.  The curly brace placing is a matter of personal
preference using:

map \if iif () {<CR><CR>}<Esc>?(<CR>a

will give you:
if () {


with the cursor in between the ().  To get the cursor in the block in
insert mode, place your "normal mode" cursor on the opening curly
brace and press 'o'

Alex Esplin

More information about the PLUG mailing list