4

In some editors, if you press while in the middle of the last line, it takes you to the end of the line (or, equivalently, the document). I have grown somewhat used to this. How do I obtain the same behaviour in Vim?

7

This seems to do what you want:

:noremap <expr> <Down> line('.') == line('$') ? '$' : 'j'
  • We use the <expr> keyword to tell Vim to expect an expression, and use the value of that.
  • line('.') gets the current line number, line('$') gets the last line number

For insert mode, you can use a slightly different version:

:inoremap <expr> <Down> line('.') == line('$') ? '<End>' : '<Down>'

This uses <End> and <Down>, rather than $ and j (actually, those will work in noremap as well).

| improve this answer | |
  • Dammit I forgot to mention that I'd like it for insert mode too. I suppose that can be handled by an imap with <ESC> prepended and a appended? – muru Mar 15 '15 at 6:29
  • @muru I would say that using <Down> and <End> is easier ;-) See updated answer. – Martin Tournoij Mar 15 '15 at 6:32
  • (mumble mumble) tunnel vision (mumble mumble). :D – muru Mar 15 '15 at 6:34
  • 1
    Whitespace is totally valid in the right-hand-side of an <expr> mapping. You might be thinking of :set? – tommcdo Mar 15 '15 at 11:52
  • @tommcdo Yeah, I had it mixed up :-/ – Martin Tournoij Mar 15 '15 at 15:22

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.