52

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.

Ayman
  • 11,265
  • 16
  • 66
  • 92

6 Answers6

35

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.

dbr
  • 165,801
  • 69
  • 278
  • 343
  • 4
    "vim is a console application" - yeh gVim/MacVim are GUI applications, but they are ports/extensions to the console vim, and can't really change too much – dbr Mar 25 '09 at 07:42
  • Should also add in Paul's response below. – Andrew Coleson Mar 25 '09 at 18:30
  • By "it's possible" do you mean it can be implemented in your vimrc somehow, or would you have to modify vim's source? – trusktr Apr 14 '13 at 06:20
  • 1
    @trusktr I meant it's technically possible (e.g [like this](http://stackoverflow.com/questions/2649733/hide-cursor-on-remote-terminal)).. I guess you could maybe write a vim script to have offscreen cursors (e.g vim-multicursor creates fake cursors - maybe it's possible to hide them, faking an offscreen cursor?) – dbr Apr 14 '13 at 12:38
  • 1
    @truskt Any progress? I'd use this quite a bit, I think. I'm really not a fan of the philosophy that because gVim came out of Vim, it should remain faithful in silly ways to the terminal world. – Kyle Strand Jun 11 '14 at 16:11
  • 1
    @KyleStrand Not yet actually, but check out the NeoVim project on github. It's been discussed to add this feature there. – trusktr Jun 12 '14 at 09:16
  • I do keep meaning to check that out, but it'll have to wait until I have some spare time to spend trying to get it built on Windows. I'm pretty excited for it, though, because I think it's pretty likely to eventually replace gVim as my editor. – Kyle Strand Jun 12 '14 at 19:39
20

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)
jthompson
  • 7,178
  • 2
  • 34
  • 33
17

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"

rpattabi
  • 9,984
  • 5
  • 45
  • 53
user189557
  • 169
  • 1
  • 3
  • 2
    while this answer doesn't match the question body, it is the exact match for the question title, which is what I need. Thank you. I'd like to add this: `z-Enter` is the same as `z-t` – Hoang Tran May 24 '17 at 14:37
12

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.

Paul
  • 13,042
  • 3
  • 41
  • 59
  • I didn't know about this one but it reminded me of another that I use, among the variety of ways of doing this given here: you can make a line edit, and then undo it, then go look around, and then just redo/undo the edit again and the redo flips you back to where the edit was. It's like a mark I guess but you don't have to remember a letter. I also bind `U` to redo so the undo/redo is really quick: `uU`. – NeilG Dec 08 '21 at 22:56
4

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

Andrew Coleson
  • 10,100
  • 8
  • 32
  • 30
4

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.

Cyber Oliveira
  • 8,178
  • 4
  • 28
  • 18