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?
- 9,549
- 5
- 51
- 64
- 2,983
- 2
- 18
- 20
6 Answers
The `. command will bring you to your last change.
The ` 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 `^.
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).
Use CtrlO
When you press gg, your old cursor position is pushed onto the jump list. From :help jumplist:
Jumps are remembered in a jump list. With the
CTRL-OandCTRL-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 +jumplist feature.
If :echo has('jumplist') prints 1, then you can use this.
- 2,717
- 14
- 24
To add to dnetserr's answer and Peter Rincker's comment, Vim maintains a list of changes, and has some commands associated with this.
:changes
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; or 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 g,).
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'. and d'^ do work as motions.
- 8,552
- 2
- 42
- 58
Saving a character on the accepted answer:
gi
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!)
- 297
- 2
- 8
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.
- 62,054
- 25
- 192
- 271
- 203
- 2
- 5
-
-
2My
vim(withnocompatible) does two undos withuu, but I useuandCTRL+rfor the same effect. – Shahbaz Feb 20 '15 at 11:04 -
This answer has the advantage that it works with vi as well, in case you have no vim on Aix/HPUX/Solaris/other Unix variants. – Guntram Blohm Feb 21 '15 at 08:50
g;andg,to move between position in the change list. See:h g;and:h g,– Peter Rincker Feb 19 '15 at 23:21'(apostrophe) instead of ``` (backtick). – Sparhawk Mar 07 '16 at 00:01\.but notg;`. VSCode vim support both. – pambda Nov 05 '20 at 09:34g;andg,both work – jmrah Mar 03 '21 at 18:26