Almost every time you move your cursor, the previous location is added to "the jumplist". Using <C-o>
will move you back to the previous location in the jump list. From :h CTRL-o
*CTRL-O*
CTRL-O Go to [count] Older cursor position in jump list
(not a motion command).
{not in Vi}
{not available without the |+jumplist| feature}
However, binding to
nnoremap <CR> o<ESC><C-o>
Does not work because o
is one of the movements that doesn't add the previous location. To manually add the current location, type m`
. From :h m`
*m'* *m`*
m' or m` Set the previous context mark. This can be jumped to
with the "''" or "``" command (does not move the
cursor, this is not a motion command).
So the end mapping would be
nnoremap <CR> m`o<ESC><C-o>
Actually, we can make this mapping even better! We can make it accept a count.
nnoremap <expr> <cr> "m`".v:count1."o\<esc>\<C-o>"
:nnoremap <CR> o
. This will add a newline and stay there itself[ Space
and] Space
mappings suffer from the same problem you describe.