I often make changes to the middle of a big file, use
gg to jump to the top to check something, and then realize I forgot to set a mark where I was editing. Is there a built-in way to jump to the location of the last-changed line?
`. command will bring you to your last change.
` goes to a mark, and
. is a "special" mark which is automatically set to the position where the last change was made. See
:help `. for some more information.
There is also
`` which will bring you back to where the cursor was before you made your last jump. See :help
`` for more information.
Another useful mark is
`^; this is the position where the cursor was the last time when insert mode was stopped. See
:help mark-motions for some more general info about using marks (including some other "special" marks that are automatically set).
Here's another approach that fits your given scenario, and will jump to where you were immediately prior to the
gg (not to the last changed line).
When you press
gg, your old cursor position is pushed onto the jump list. From
Jumps are remembered in a jump list. With the
CTRL-Icommand you can go to cursor positions before older jumps, and back again. Thus you can move up and down the list. There is a separate jump list for each window. The maximum number of entries is fixed at 100.
If you use a jump command, the current line number is inserted at the end of the jump list. If the same line was already in the jump list, it is removed. The result is that when repeating
CTRL-Oyou will get back to old positions only once.
This requires that you be using Vim (not vi) with the
:echo has('jumplist') prints
1, then you can use this.
To add to dnetserr's answer and Peter Rincker's comment, Vim maintains a list of changes, and has some commands associated with this.
will list the changes, showing you where they were and what they were. For example:
change line col text 2 8 17 #include <stdio.h> 1 3 0 #include "stm32f407.auto.h" >
The line with the
> shows where in the change stack you are, kind of like the jump list (
:jumps) or tag stack (
:tags). Also like the jump list and tag stack, you can traverse this list.
In normal mode, the motions are
g; to go to a previous change location, and
g, to go to the next one. You can also type the number of the change prior to
g, to go to that change from the list. Above,
2g; would take me to where the change involving stdio.h occurred.
When in the middle of the stack, the numbers from
:changes updates to show the relative distances. For example:
change line col text 1 8 17 #include <stdio.h> > 0 3 0 #include "stm32f407.auto.h" 1 10 4 other
This shows that I can go back one (
1g; or just
g;) or forward one (
1g, or just
This is great if the change you want to get to actually occurred a few modifications ago
The disadvantage of this is that it doesn't appear to act like a normal motion. For example, you cannot do
dg; to delete from the cursor to the previous change location, whereas
d'^ do work as motions.
g; will go to the previously edited line.
Since the undo command in Vi will act as a re-do when the last command was undo, I have used
uu as a means to go back to where I last edited.
If you're running Vim with
nocompatible set (as most people do), you want to use
u followed by
CTRL+R to undo & redo. Otherwise
uu will work as in Vi.
Saving a character on the accepted answer:
Will take the cursor to the exact point last edited and put you in insert mode, ready to continue typing. (It's also quicker to type!)