138

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?

6 Answers 6

183

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).

5
  • 21
    You can also use g; and g, to move between position in the change list. See :h g; and :h g, Commented Feb 19, 2015 at 23:21
  • 7
    You can also use ' (apostrophe) instead of ` (backtick).
    – Sparhawk
    Commented Mar 7, 2016 at 0:01
  • 3
    Just FYI for you guys, ideavim support `. but not g;. VSCode vim support both.
    – pambda
    Commented Nov 5, 2020 at 9:34
  • @pambda I'm running IdeaVim 0.65 and g; and g, both work
    – jmrah
    Commented Mar 3, 2021 at 18:26
  • For vim plugin in intellij in MACOS, as far as I have observed, `` works but g; does not. Commented Feb 2, 2023 at 11:59
36

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-O and CTRL-I command 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-O you 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.

33

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.

24

g; will go to the previously edited line.

1
  • 5
    You can also use g, (comma) to go to a newer editing position.
    – James
    Commented Feb 20, 2015 at 23:12
13

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!)

7

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.

3
  • It will if :set nocompatible is not used.
    – muru
    Commented Feb 20, 2015 at 9:47
  • 3
    My vim (with nocompatible) does two undos with uu, but I use u and CTRL+r for the same effect.
    – Shahbaz
    Commented Feb 20, 2015 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. Commented Feb 21, 2015 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.