Vim Tips Wiki
Tip 908 Printable Monobook Previous Next

created April 3, 2005 · complexity basic · author Gerald Lai · version 5.7

This is for those of you who do not edit in virtualedit mode but would occasionally want to move the cursor up/down virtually to get the cursor to a character column quickly. Keep in mind, the mappings below is basically a switch to virtualedit mode, followed by a cursor move up/down and a switch back to non-virtualedit mode, with a couple of minor details.

Here is an example of the effect:

Let's say you have your cursor (the pipe character) between 'a' and 'b' in the first line. Also assume the second line ends at the character 'z'.


Moving the cursor down virtually would have the cursor 5 characters from the beginning of the second line, automatically inserting 3 spaces between the new cursor location and character 'z'. The colon indicates the previous cursor location.

zz   |

Place the following in your vimrc:

"cursor down/up existing lines
imap <S-Down> _<Esc>mz:set ve=all<CR>i<Down>_<Esc>my`zi<Del><Esc>:set ve=<CR>`yi<Del>
imap <S-Up> _<Esc>mz:set ve=all<CR>i<Up>_<Esc>my`zi<Del><Esc>:set ve=<CR>`yi<Del>
"cursor down with a new line
imap <S-CR> _<Esc>mz:set ve=all<CR>o<C-o>`z<Down>_<Esc>my`zi<Del><Esc>:set ve=<CR>`yi<Del>

The mappings of Shift-Up/Down/Enter may not work with terminal versions of Vim (in which case, switch mappings to another key sequence). Works fine with gvim.

After mapping is done, mode is set back to non-virtualedit.

These mappings are for insert mode, not while in replace mode.