Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Vim command to undo the last motion, e.g. <count>j or Ctrl-f?

Tags:

vim

vim-plugin

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?

like image 464
Christian Abbott Avatar asked Apr 20 '15 11:04

Christian Abbott


People also ask

What does Ctrl F do in Vim?

Ctrl+f will search within the file ctrl+shift+f will search in all the files in the folder tree.

How do I undo in Vim?

Remember, yo undo a change in Vim / Vi use the command u , and to redo a change which was undone use Ctrl-R .

How do you cancel an action in Vim?

To cancel a running command in Vim, press Ctrl+C .

How many times can you use the Vim undo command?

2 Answers. Show activity on this post. You have only 1 level of undo if you are in Vi compatible mode. You are missing out on a number of features by being in 'compatible' mode.


1 Answers

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.

like image 152
Ingo Karkat Avatar answered Sep 22 '22 13:09

Ingo Karkat