Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Move cursor to its last position

Tags:

vim

People also ask

How do you jump to a line in vim?

If we are in the vim editor, then simply do this, “Press the ENTER key, write the Line number, and press Shift+ g”: Again the output is the same.

How do I scroll in vim?

You can make Vim scroll the text using the shifted up/down arrows by mapping Shift-Up to Ctrl-Y and Shift-Down to Ctrl-E. Shift-Down will then scroll down (like moving a scroll-bar down, or like moving a cursor at the bottom of a window down), and Shift-Up will then scroll up (like moving a scroll-bar up, etc).


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.


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;

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.


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}