Returning to previous line with Vim - vim

I was wondering if there's a way to return to the previous line you were on in Vim.
Say, for example, I'm writing C code and I just wanted to add an #include at the top of the page: I press gg and go to the top, add the #include, and then I want to return to the line I left off of.
Is this possible in Vim?

Yes. You can use `` in order to jump between the last two positions.
Otherwise, Ctrl+O and Ctrl+I can help you. See :help CTRL-I.

Use a mark:
ma
marks the spot (before the gg)
`a
brings you back.

Related

Add whitespace to current line in vim

I fairly often find myself in a situation like this:
I'd like to start typing on the line on which my cursor is currently. However, in order to get to the correct indentation level, I'd have to press TAB several times.
Usually I'd press ddO (delete current line and insert a new one above the cursor), which resets my indentation position to the right place:
But that seems like an odd way to go about adding the correct amount of whitespace.
Is there a better way that I'm overlooking?
When in normal mode, you can use cc or its synonym S. If you are already in insert mode, Ctrlf is the default key for this, but that can be changed by altering cinkeys (see :h cink for details).
See also this answer on the Vi/Vim stack
Kevin mentioned some shortcuts, but another method is <C-i> (indent) and <C-d> (dedent) in insert mode.

Vim return to second last edited line

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.

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. 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}

how to jump back to the previous edit place

Is it possible to go back to the previous edit place in vim? I know using marks, one can move back and forth between different places within a file, but I don't know whether or not vim could automatically remember the previous edit place for you.
I use the following (from the documentation):
g; Go to [count] older position in change list.
g, Go to [count] newer cursor position in change list.
Do :help g, to read more about this
The last change is held in the mark named . so you can jump to the mark with `. (backtick, dot) or '. (apostrophe, dot). See:
:help mark-motions
:help '.
ctrl+o
ctrl+i to go forward (once you've gone backward, of course)
To jump to the last edit position type gi.
I used to use u then Ctrl-r before I learned about '.

Vim keep cursor location while scrolling

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.

Resources