8

In Vim, if I issue a jump command, e.g. G, then I can put the cursor back where it was before the jump by using Ctrl-o. However, Ctrl-o would not put my cursor back to the previous spot if I issued a movement command like 40j or Ctrl-f, since they don't alter the jump list. Is there a command which will undo movements such as those?

To be clear, I'm not looking for a "manual" answer, such as 40k gets you back from 40j, since such a command is not generically applicable in the way that Ctrl-o works.

Also, if no built-in command does this, then does any plugin do it?

Christian Abbott
  • 6,497
  • 2
  • 21
  • 26

2 Answers2

10

You can override the default motions to provide for that, like this:

" j, k          Store relative line number jumps in the jumplist.
nnoremap <expr> k (v:count > 1 ? "m'" . v:count : '') . 'k'
nnoremap <expr> j (v:count > 1 ? "m'" . v:count : '') . 'j'

There's also the reljump plugin, which implements the same.

However, be careful, because overdoing this will reduce the usefulness of the jump list. For that reason, I would advise against changing Ctrl-F / Ctrl-B.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • For anyone interested in making these mappings work in IdeaVim, there is this issue in YouTrack that can be upvoted: https://youtrack.jetbrains.com/issue/VIM-2750/How-to-add-numbered-movement-in-vim-to-ideas-jump-history – HerCerM Sep 23 '22 at 01:24
0

You can set up a mark with m', and then go back with ``.

qiubix
  • 1,302
  • 13
  • 22