How to call a function that moves the cursor without leaving the visual mode?

I have a function that moves the cursor with a built-in function cursor()and works fine in normal mode.
For concreteness, suppose this is a function:

function! F()
    call cursor( line('.')+1, 1)
endfunction

used with mappings like:

 nnoremap <buffer> a :call F()<cr>

Now I want to reuse this function to move the cursor to any visual mode (visual, linear and visual) and without losing the previous selection.

For example, with an initial buffer in visual mode ( cmeans that the cursor is in a line, vmeans that the line is part of the current visual selection):

vc 1
   2
   3

pressing awill give:

v  1
vc 2
   3

and clicking aagain will give:

v  1
v  2
vc 3

to keep the old choice.

F() , F() .
?

, , :

function! VisMove(f)
    normal! gv
    call function(a:f)()
endfunction

:

 vnoremap <buffer> a :call VisMove('F')<cr>

, :

  • fgplugin, .
  • , ( ), () , , . <expr>, , .
+5
1

, mode (, , isVisual) :

fu! F(mode) range
    if a:mode ==# 'v'
        normal! gv
    endif
    ...
endf
nn <buffer> a :cal F('n')<cr>
vn <buffer> a :cal F('v')<cr>
+2

All Articles