Is there a way to let the cursor sit on a newline in Vim, similarly to the default behavior of Emacs?

When you delete a character in the middle of a line, the character gets removed and the cursor now sits on the character that was to its right. If you delete a character at the end of the line however, the character is removed and the cursor now sits on the character that was to its left. I would like it to be able to sit on the newline, which is the character (or characters depending on the system) that was to the right of the deleted character. I would also like to be able to have the cursor sit on the newline in general.

  • That newline characters are treated specially is the one thing that annoys me most when using vim! In my opinion, this is very un-vimlike, as my actions now have to depend on where in the line the cursor is (e.g., I cannot use i to insert a character at the end of the line, but I have to use a and i depending on the situation). – Scz Feb 25 '16 at 13:55

This is possible with the virtualedit setting. Try e.g. :set virtualedit=onemore and read more of it at :h 'virtualedit' Note, this might break plugins or other slightly less tested corners in Vim.

If you also want to make x delete the final newline in this mode, you might be able to map x to do something more clever, e.g.

:nnoremap <expr> x col('$')==col('.')?'gJ':'x'

This basically maps x to an expression, that checks, whether the cursor is on the last character (the newline) of the line, and if it is, it will be mapped to the gJ command otherwise a normal x will be performed.

(Note: not very thoroughly tested, there might be some edge cases)

| improve this answer | |
  • 1
    This is very close to what I want, and answers what I actually asked. Is there any way to make it so I can actually delete the newline character when the cursor is over it and I press x or similar? Basically, I'd like Vim to treat the newline like a normal character, which I suppose would have been a better way to state my original question. – Vaelus Jun 16 '15 at 20:41
  • not that I know of. You might however be able to map x to something. will update the answer – Christian Brabandt Jun 16 '15 at 20:56
  • If you have :set whichwrap+=h you might also like nnoremap <expr> h col('.')==1?'k$l':'h' so that h and l behave alike around the linebreak. [edit: does not work if there is an empty line before] – Scz Feb 25 '16 at 14:11

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.