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.
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.
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 Or expand with <BS>
(backspace) in the abbreviation.<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.
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.