Tip #417: combining move and scroll [IMPROVED]
| tip karma
Rating 24/9, Viewed by 1231
||February 3, 2003 14:29
||as of Vim:
A long time ago, I entered vimtip #105. I used that mapping for a long time, but it always had a couple problems. One, it reset the scroll parameter. Two, it didn't work in visual mode, because :set scroll exits visual mode. I was reviewing my vim configuration and learning some new tricks, and in the process I improved this mapping.
Now, Ctrl-J and Ctrl-K will move the cursor one line down or up, and scroll one line down or up--so the cursor remains on the same screen line (except near the beginning and end of the file)--in both normal and visual modes. And the scroll parameter is unaffected.
" N<C-D> and N<C-U> idiotically change the scroll setting
let save_scroll = &scroll
execute "normal" a:cmd
let &scroll = save_scroll
" move and scroll
nmap <C-J> :call <SID>Saving_scroll("1<C-V><C-D>")<CR>
vmap <C-J> <Esc>:call <SID>Saving_scroll("gv1<C-V><C-D>")<CR>
nmap <C-K> :call <SID>Saving_scroll("1<C-V><C-U>")<CR>
vmap <C-K> <Esc>:call <SID>Saving_scroll("gv1<C-V><C-U>")<CR>
This is an example of several terrible vim hacks, to boot.
<<Fix error "E97: Cannot create diffs" on VIM for Windows |
Stop the beeping in gvim. >>