tags:

views:

52

answers:

3

Hi guys,

is there some way to tell VIM place the cursor at some position after abbreviation expansion? Say you have something like this in .gvimrc

iabberv <? <?=|?> 

and you want to place cursor where pipe character is automatically.

+1  A: 

A quick solution that I'd use in this case is to insert some key presses to the abbreviation:

iabbrev <? <?=?><Left><Left>

Would place the cursor two places left, after the =.

In this manner, you can use various movement keys such as <End>, <Home>, or even return to normal mode with <Esc> and command usual normal-mode commands. The example would then be

iabbrev <? <?=?><Esc>hha

and it does the same as the first example. If you expand an abbreviation with space, it will have one extra space. which you can get rid of by using a <BS> (backspace) in the abbreviation. Or expand with <C-]> which will leave no space.

Correction: since the abbreviation is first expanded, and after that the space inserted, you need a small function found in the help (map.txt):

func Eatchar(pat)
   let c = nr2char(getchar(0))       
   return (c =~ a:pat) ? '' : c      
endfunc

This is best put in .vimrc. Now the following abbreviation will work fully as intented:

:iabbrev <silent> <? <?=?><Left><Left><C-R>=Eatchar('\s')<CR>

It is a bit messy, but has an external function call that removes the white space and it should work well.

progo
I tried iabbrev <? <?=?><Esc>hha<bs> but it deletes "=" character :) where should I place <bs> ? It is exactly what I want, except this <bs> problem.
ivan73
Really sorry for not testing the `<BS> ` functionality before answering. I edited in a suitable workaround.
progo
Cool, it works, accepting this answer :) I'm using it like this:`iabbrev <? <?=$?><Esc>hha<C-R>=Eatchar('\s')<CR>`
ivan73
A: 

It can be done with lh-map-tools:

"
" in {rtp}/ftpluvin/vim/vim_snippets.vim
inoreab  <buffer> <silent> if    
      \ <C-R>=InsertSeq('if', 'if!cursorhere!\nendif!mark!')<CR>

Other plugins offer a similar feature.

Luc Hermitte
+2  A: 

What you want are snippets. I use snipmate for that.

Daenyth
Vote up, thanks for tip Daenyth
ivan73