Vim keep cursor location while scrolling

Vim

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

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)

Solution 3 - Vim

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"

Solution 4 - Vim

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.

Solution 5 - Vim

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.

Solution 6 - Vim

Google says that the cursor (and therefore current line) must be visible in Vi, so you'll have to use marks.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAymanView Question on Stackoverflow
Solution 1 - VimdbrView Answer on Stackoverflow
Solution 2 - VimjthompsonView Answer on Stackoverflow
Solution 3 - Vimuser189557View Answer on Stackoverflow
Solution 4 - VimPaulView Answer on Stackoverflow
Solution 5 - VimCyber OliveiraView Answer on Stackoverflow
Solution 6 - VimAndrew ColesonView Answer on Stackoverflow