Idan K
Idan K

Reputation: 20881

Vim: Move cursor to its last position

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.

Upvotes: 332

Views: 116788

Answers (5)

ggorlen
ggorlen

Reputation: 56905

In addition to the movements mentioned already...

  • g; and g, to move forward and backward through edit locations, probably the best answer to this question
  • Ctrl+i and Ctrl+o to move forward and backward through the jump list
  • `` and '' to swap between the last jump list positions

...here are some ad-hoc approaches that aren't beautiful, but work for me in practice on typical last cursor position movements I need to perform.

I'm often editing some code, then need to jump to another part of the file to look around for a bit. In this case, u to undo and Ctrl+r to redo brings me back to where I was last editing no matter what.

This doesn't work well for editing amongst multiple locations, though, so in that case I'll often remember a variable that's unique to the function I'm in, then use a standard search with ? and / to get back to where I was last working.

If I'm looking at the definition of a function I'm calling, or vice versa, I'll use * on the variable name to jump between usages and definition. This also works to get back to somewhere--select a unique-ish word on the origin line with *, go exploring and editing, then press n to return back to the original location.

If I keep a line number in mind, I'll use :42 Enter where 42 is the line to go back to after making edits elsewhere.

If I'm toggling between the top and bottom of the file, I use gg and Shift+G.

If I'm toggling between the top and bottom of a function, I use % on the brackets.

I might occasionally add temporary comments at different points in the code with unique identifiers (I usually use my username with suffixes for uniqueness), then use standard searching to jump to or cycle between them, remembering to remove them before commit.

Upvotes: 1

Serge Stroobandt
Serge Stroobandt

Reputation: 31508

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;

Upvotes: 86

ZhaoGang
ZhaoGang

Reputation: 4915

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.

Upvotes: 82

DrAl
DrAl

Reputation: 72626

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.

Upvotes: 535

Facundo Casco
Facundo Casco

Reputation: 10585

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}

Upvotes: 38

Related Questions