How can I join two lines in Vim and leave the cursor in its original position instead of it jumping to the merge point?
For example, take the the following two lines with the cursor at the position indicated by the caret:
this is ^line one
this is line two
Merging by J produces:
this is line one ^this is line two
How can I produce:
this is ^line one this is line two
I have tried things like Ctrl + O and variations of ''. None of these seem to work. They go to the beginning of the line, not to the original cursor position.
Another approach that wouldn’t stomp on marks would be this:
Much more verbose but it prevents you from losing a mark.
:nnoremap– Non-recursive map<silent>– Do not echo anything when the mapping is pressedJ– Key to map:let p=getpos('.')– Store cursor position<bar>– Command separator (|for maps, see:help map_bar)join– The ex command for normal’sJ<bar>– …call setpos('.', p)– Restore cursor position<cr>– Run the commands