1

Let's say I have the text:

foo bar baz

With the cursor on z, I would expect :normal! lb to move the cursor to the first letter of baz. In actual fact the cursor does not move.

However typing :normal! l and then :normal! b does move the cursor as expected.

The docs for :normal! state:

Execution stops when an error is encountered.

Is this why :normal! lb doesn't do what I expect? The l cannot move to the right and so it is treated as a (silent) error?

Thanks in advance!

  • 5
    I think you asked the question and gave the answer in the same post ;) l erroring and canceling the question is the reason. – statox Dec 7 '17 at 17:06
  • @statox It seems like l would be a no-op but evidently not. – Andy Stewart Dec 8 '17 at 9:42
  • 1
    Actually it makes sense that norm! l produces an error when it is on the last character of a line: you ask Vim to move the cursor to the right, by default l can't change line so Vim tries to move the cursor and can't do it so it is an error. – statox Dec 8 '17 at 9:59
1

As @statox notes, you've answered your own question!

As your cursor is already at the rightmost position in the line, attempting to move further to right causes an error, and the :normal! execution ends.

Note, however, that this behaviour is dependent on at least two of Vim's default options. Try either of the following commands first, and your normal command will work as you originally expected:

" This allows `l` to move down to the beginning of the next line
:set whichwrap+=l

" This allows `l` to move past the last character on the line
:set virtualedit=all

Similar behaviour can be utilised to write recursive macros that stop at the end of a line.

  • It surprises me that attempting to move rightwards from the end of the line counts as an error instead of a simple no-op. After all, no error is thrown (those E123456 errors). – Andy Stewart Dec 7 '17 at 18:05

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.