314

Is it possible in (g)Vim to move the cursor to its previous position (while in normal mode)? Something to cycle back and forth in the list of previous cursor positions would be ideal. But also just to switch to the last location would suffice (something like cd - in bash with directories).

Here's a little demonstration:

line |1| <- cursor position
line 2
line 3
line 4

And suppose I did 2j, here's how it is now:

line 1
line 2
line |3| <- cursor position
line 4

Now I'd like to press something (other than 2k obviously) to move back to the first position and possibly to previous positions.

4 Answers 4

512

The quickest way is to hit either:

''

(two apostrophes) or:

``

(two backticks). Note that the difference is that the backtick goes to the same location on the line, whereas the apostrophe goes to the start of the line. On a UK keyboard, the apostrophe is more accessible, so I tend to use that one. There are loads of useful marks like this, see :help mark-motions.

For some other motions (not 2j I think), there's also the jump-list that lets you navigate back and forth among a number of motions. CtrlO and CtrlI do this navigation, but see :help jump-motions for more information.

8
  • 7
    actually none of those behave exactly like I expect but I guess there isn't anything else available.
    – Idan K
    Feb 19, 2011 at 16:58
  • 1
    @Idan K - how does `` (double backtick) not give you exactly what you want? (Except for the "history" aspect).
    – Dan
    Feb 19, 2011 at 20:28
  • 7
    @Dan: if I try it on the example above then I see this behavior: I stand on line 1, then press j twice in a row (now I'm on line 3) then pressing double backtick gets to me line 1 and I expect it to move to line 2.
    – Idan K
    Feb 20, 2011 at 8:56
  • 2
    I think you have to move (2j or something), then edit (or make any action to modify the text) then use '' to move back to the previous position. That is not logical to use "2j" , do nothing and go back to the previous position
    – Asenar
    May 21, 2013 at 13:49
  • 13
    @IdanK The jumplist is only updated (i.e. the previous cursor position is recorded) when you use an actual "jump," which is (I think) any motion command other than the i j k l movements. (The complete list, from the help docs, is "'"', "`", "G", "/", "?", n", "N", "%", "(", ")", "[[", "]]", "{", "}", ":s", ":tag", "L", "M", H" and the commands that start editing a new file.) So no, this won't "undo" 2j or similar, but it will work for almost everything else. This makes sense because 2j et al. already have simple inversions (2k, etc), while the others do not. Jan 10, 2014 at 20:51
84

You can also use g; and g, to move back- and forward in the list of your previous edit locations.

On Non-US Keyboards

On my Swiss and German keyboard layouts, typing ; inconveniently requires using the Shift key. Hence, I defined g- as a more convenient alias for g; in $MYVIMRC:

" Map g- as an alias for g;
nnoremap g- g;
1
  • 1
    While I like this a lot, it only will jump between changes in one file. If you edit buffer 1, then edit buffer 2, it won't jump back to buffer 1.
    – dessalines
    Jan 22, 2020 at 14:30
74

Why no one figured out the problem with DrAl's answer? The '' or `` will not solve the original problem of this post! These two command will not work for some cursor movement like 2j, at least for me. It will make newbie to vim more confused.

The behavior of '' or ``, and CtrlI or CtrlO are based on jump list. The 2j will not save the position changes into the jump list so these command will not work for 2j.

'' or `` switch between the last position and the current position. CtrlI and CtrlO work through the jump list history.

g; and g, move through edit positions, which are also very frequently used.

3
  • 11
    Thanks for pointing this out! I wonder, what will work for cases like 2j, then?
    – user8554766
    Jul 17, 2018 at 9:06
  • Thanks @ZhaoGang, this was exactly what I was looking for! Jul 19, 2021 at 12:42
  • Not every movement needs something fancy. If I 2j and want to go back I can say 2k :)
    – gbarry
    Sep 21, 2022 at 17:27
36

Right from the help (:help jump):

:ju[mps] Print the jump list (not a motion command). {not in Vi} {not available without the |+jumplist| feature}

                          *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. {not available without the |+jumplist| feature}

0

Your Answer

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

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