1

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?

0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. I created a variable "jump" which indicates how many lines the selection has to go down. What would be the function with my value "jump" inside? `normal! gv"jump"j ??
If you want to add a variable you need to explicity execute the "normal" comand, so the line would become: execute "normal! '<V'>" . jump . "j". If jump is an argument supplied to the function, then you need to use a:jump when you use it.
Btw your function extend the selection as a line selection. What would it be in case of block selection?
This function is 1. Changing type of visual selection. 2. Does not work when called for the second time without quiting selection. 3. Quits visual mode when called for multiline selection with even number of lines.
4. Extends 30 lines down from the first line of selection, not from the last. 5. I guess it relies on the (bug?) that '> marker is not moved during the selection: : in visual mode produces :'<,'> and thus for 3-line selection it is called three times.
|
3

It is better expressed with <expr> mappings:

vnoremap <expr> \e g:jump."j"

With a function call:

function Jump()
    " Do something (modifying text, switching buffers and 
    " something other is forbidden, see :h map-<expr>)
    return jump."j"
endfunction
vnoremap <expr> \e Jump()

4 Comments

thank you. Nice solution. However maybe difficult in my case...I declare the jump value in a function.
@Remonn What is the problem? You can put a function call there. See update.
In my function I calculate jump p.e. jump = 30. How would you put that in a function call?
@Remonn You can call a function that calculates jump. I can't say more without seeing this function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.