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.


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. Ctrl-O and Ctrl-I 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 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;

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}

链接地址: http://www.djcxy.com/p/49394.html

上一篇: 消除UITableView下面的额外分隔符

下一篇: Vim:将光标移动到最后位置