70

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?

86

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

  • 7
    You can also use g; and g, to move between position in the change list. See :h g; and :h g, – Peter Rincker Feb 19 '15 at 23:21
  • 3
    You can also use ' (apostrophe) instead of ` (backtick). – Sparhawk Mar 7 '16 at 0:01
21

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.

19

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.

15

g; will go to the previously edited line.

  • 3
    You can also use g, (comma) to go to a newer editing position. – James Feb 20 '15 at 23:12
5

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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