Search code examples
vimselectmoveextend

How to extend a selection in vimscript


p.e. I have a block of text selected (using Ctrl-V) and want to extend it in a vimscript to a new location p.e. 30 lines below

Does anyone know how to do this?


Solution

  • You can use the markers '< and '> to move to the beginning and end respectively of the most recent visual selection. So a simple function such as

    EDITED to use gv and a jump variable.

    function! ExtendVisual(jump)
        execute "normal! gv" . a:jump . "j"
    endfunction
    
    vnoremap <silent> <leader>e :call ExtendVisual(30)<CR>
    

    will let you extend the current visual:q region by 30 lines using \e.