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?
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>'
<Down>, rather than
j (actually, those will work in
noremap as well).