1

Executing:3,7y does not change the line which contains the cursor (line('.')). This is what I expected.

However, when I execute :3,7call XYZ(), the current line goes to the third line. I didn't expect this behavior.

Why is that? Can I change the behavior so that the current line does not change?

1
  • Read :help :call. This behaviour is explained there. It doesn't say how to keep the cursor on the original line though. Nov 26, 2016 at 16:55

2 Answers 2

1

One way to do this would be to pass current line as function argument. But line('.') is evaluated for first line of range, thus this needs to be done earlier. My solution to this would be to use user command. Below complete sample code:

function XYZ(curline) range
  [normal body]
  exe "normal " . a:curline . "gg"
endfunction

command -range XYZ :let l=line('.') | <line1>,<line2>call XYZ(l)

That way you need to do :3,7XYZ

0

The anwolib plugin provides (among others) a :KeepView command. With it, you can wrap any other command invocation, and it will maintain the current position in the buffer.

:KeepView 3,7call XYZ()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.