I'm trying to write a vimscript function that moves the cursor all the way up or all the way down without changing the column. (similar to the way
:set nostartofline works).
It works, but when I tried to add a mapping for visual mode, it would exit visual mode.
Here is my script:
function! StraightDown() let line=line('.') let end=line('$') exe "normal ".eval(end-line)."j" endfunction function! StraightUp() let line=line('.') exe "normal ".eval(line-1)."k" endfunction nnoremap <M-j> :<C-u>call StraightDown()<cr> nnoremap <M-k> :<C-u>call StraightUp()<cr> xnoremap <M-j> :<C-u>call StraightDown()<cr> xnoremap <M-k> :<C-u>call StraightUp()<cr>
After experimenting with it, it seems like any command executed with
normal ... will leave visual mode unless it starts with
One possible workaround I thought of is to add some more logic, and if I call the function in visual mode, add
v to the normal command. However, this doesn't work if I'm in visual line or visual block mode.
So really, my TL;DR question is how can I do a
normal command in visual mode without changing modes?