Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cursor position in VIM abbreviation

Tags:

vim

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.

like image 791
ivan73 Avatar asked Jul 23 '10 16:07

ivan73


1 Answers

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.

like image 118
mike3996 Avatar answered Oct 06 '22 03:10

mike3996