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. 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}
Related
I am wondering about a improvement for vim that I can jump back for where I was the last time I stopped to move around for like 3 seconds. Then I can scroll around and have a kick shortcut to jump back where I was with the same old CTRL+o.
Vim put a lot of movements in the :jumps list and I wondering that half of that is useless in any moment for me, so how could I do that ?
In any another words I'm trying to make the jumplist more sensible.
ps: Netbeans and VS has a similar behavior.
To have the current position automatically added to the jump list, you can utilize the CursorHold event:
:autocmd CursorHold * normal! m'
Navigation is with the default <C-o> / <C-i>.
It's common to use the m and ' commands to jump around. For instance, you can type mx, go off and do some things, and then do 'x to jump back to where you were when you did mx. This works for every letter of the alphabet (i.e. ma, mb,... mz). Uppercase marks (mA, mB,...) will also remember the filename so you can jump between files. There are a number of other special marks you can set for various purposes.
The "previous context mark" is called '. It is set automatically when you jump around, and can also be manually set with m'. Jump to it with ''. The m' command also manually adds a jump to the jumplist. '' is roughly equivalent to Ctrl+O, but doesn't take a count.
If you replace the apostrophes in these commands with backticks, Vim will jump to the specific column rather than just the line. In practice, apostrophe is easier to type and often close enough.
You can set a mark in the ' register. The keystrokes would be m'
I found this in vim's documentation :help jumplist.
You can explicitly add a jump by setting the ' mark with "m'".
I have been currently tweaking my Vim _gvimrc file to add a few features etc. and I've recently noticed, that when I use the cursor key to move left while at the beginning of a line, it no longer moves to the end of the previous one, it just stays put.
I used to be able to move from line to line just by holding the left or right cursor keys but for some reason that functionality has been lost. This is also true for the standard Vim h j k l keys.
:set whichwrap=b,s,h,l
whichwrap can be abbreviated to ww.
Is there a key-combination that behaves as though I'd press ctrl-E followed by a j, that is the text scrolls up a line but the cursor keeps where it is, relativ to the screen.
I am aware that I could achieve what I want with a :map but before I do I thought I'd rather want to know if there is already some "built-in" functionality
Yes, use CTRL-D with a count of 1 (not that that saves you anything, really).
The CTRL-D command does the same as CTRL-E, but also moves the cursor down the same number of lines
There is the z command
z. Redraw, line [count] at center of window (default
cursor line). Put cursor at first non-blank in the
line.
zz Like "z.", but leave the cursor in the same column.
Careful: If caps-lock is on, this commands becomes
"ZZ": write buffer and exit! {not in Vi}
These mappings makes it possible to scroll up and down one line with focus on center line (hard to describe so that it sound correct, try it instead)
"scroll with line in center
map <C-Up> <ESC>0kzz
map <C-Down> <ESC>0jzz
In vim you can go back to the last edited line with '. , but is there a way to go back to the second-last edited line? Suppose I did some edits at line 100, then some edits at line 1, is there a quick way to return to line 100? I know a mark can be used but that requires forethought...
You can use the jumplist (:help jumplist). Simply use Ctrl-O to go back (and Ctrl-I to go forward) in the list of the last places you either edited or jumped to (via %, /, [[ etc: see :help jumplist for the full list).
To go back to the place you edited before last then assuming you've done no other jumps in the meantime, this would be Ctrl-O Ctrl-O or (I think) 2 Ctrl-O.
In addition to jumplist, vim has also a changelist, which can be iterated over using g; (to older item) and g, (to newer item) motions.
Is there a way to keep the cusror location off-screen in Vim / gVim while scrolling? Similar to many Windows editors.
I know about marks, and do use them. I also know the '.' mark (last edit location), But looking for other ideas.
I'm asking this because sometimes i want to keep the cursor at some location, scroll to another place using the mouse-wheel, and then just press an arow key or something to get me back to that location.
No. vim is a console application, so it doesn't really make sense to have the cursour off-screen (it's possible, but would just be confusing)
An alternative solution, to paraphrase posts from this thread from comp.editors:
Ctrl+o goes to the previous cursor location, Ctrl+i goes to the next (like undo/redo for motions)
Marks seem like the other solution..
Also, use marks. Marks are named by letters. For instance typing ma remembers
the current location under mark a. To jump to the line containing mark a,
type 'a. To the exact location use `a.
Lower-case-letter marks are per-file. Upper-case-letter marks are global;
`A will switch to the file containing mark A, to the exact location.
Basically ma, move around, then `a to jump back.
Another option which Paul suggested,
gi command switches Vim to Insert mode and places cursor in the same position as where Insert mode was stopped last time.
Why don't you split the window, look at what you wanted to look at, and then close the split?
:split
or
:vsplit (if you want to split vertically)
The only similar behavior that I've found in Vim:
zt or zENTER "scroll the screen down as far as possible without moving the cursor"
zb "scroll as far up as possible".
Ctrl+E "scroll one line down, if possible"
Ctrl+Y"scroll one line up, if possible"
Sometimes you can avoid jumping to marks before entering text — gi command switches Vim to Insert mode and places cursor in the same position as where Insert mode was stopped last time.
Google says that the cursor (and therefore current line) must be visible in Vi, so you'll have to use marks.
Also very useful are the '' (2x single quotes) and `` (2x back quotes).
The former jumps back to the line you were prior to the last jump (for instance, a page down).
The latter jumps back to the line and column you were prior to the last jump.