1

In vim, when executing a command with a range specified, the cursor will move to the end of the range. For example, after 10;+4print, cursor will be at line 14 wherever you run this command.

Q1: Is there a method to hold the cursor while executing this?

In addition, consider a user defined command with range, such as

command -range Test call TestFunc(<line1>, <line2>)
function TestFunc(line1, line2)
    echo line('.')
endfunction

with line number shown.

Q2. run :10;+4Test will have cursor stop at line 10 and the result will has is also 10. This means the function is called after setting the cursor to line 10. The current line where the command is executed is not accessible (at least using line('.')). How should I get the correct line number in the function?

  • 2
    Lookup winsaveview() and winrestview() – muru Aug 7 '17 at 7:49
  • That one returns the same line number. It is because before the function gets called, the cursor position is already changed (to 10) – Liu Sha Aug 7 '17 at 10:00

Your Answer

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

Browse other questions tagged or ask your own question.