Cursor movement history in Vim

10

3

How can I restore cursor position in Vim as it was before scrolling, selecting or PgUp/PgDn?

I'm tired of searching where I was before I scrolled up to look something at the top.

Are there something like "Prev cursor position" and "Next cursor position" commands (like 'u' and 'R' for regular undo/redo)?

Example: Looking somewhere in the middle of the source file (no editing), then jumping to the top (1G) then failing to find where you was before that. Of course, no marks set. (I know that I can use ":sp" to view several places simultaneously).

Update. The question is still open, I have received a follow-up:

The answerers didn't understand your question and the answers were too weak of a solution. c-o & c-i don't work for movements.
If I understood correctly, I find that your question was un-answered and that you were looking for a solution for a scenario like this:
As I am vim beginner I frequently do incorrect movements and actions and would like to undo them as though I had never pressed anything, returning to my previous editing location before I started messing up.

So, are there any standard or non-standard (involving patches to Vim or Vim extensions) ways to make it remember the cursor movement history?

Vi.

Posted 2010-05-21T16:21:01.000

Reputation: 13 705

Answers

9

  • Ctrlo goes back in jump list
  • Ctrli goes forward in jump list

additionally i would consider to set a mark via m' and when done with scrolling press ''.

akira

Posted 2010-05-21T16:21:01.000

Reputation: 52 754

OK, Ctrl+o jumps me to the place where I was editing. How to just to the place where I was looking? I enter "1G" to jump to the beginning and don't know how to jump back (I wasn't editing). Of course mark is not set and I haven't used ":sp". – Vi. – 2010-05-21T17:17:24.810

@Vi: no, ctrl-o jumps to where your last jump was (jump list). g; goes where you edited last – akira – 2010-05-21T17:28:47.187

1>

  • "vim initrd.txt". 2. 5 times PgDn, 10 times Down. 3. Hold PgUp until hit the top. 4. Ctrl+o? No, It's just jumped to the top. 5. Crtl+o again? It opened some other file... I don't understand what exactly Ctrl+o does, but it doesn't just undo the last cursor movement command.
  • < – Vi. – 2010-05-21T18:32:59.890

    i know, it is using the "jump list" .. :help jumps ... thats why i meant, probably a good idea to use the markers. if you want to jump around in your file and you want to go back to THAT place you started: set a mark, jump around and then come back with ''. – akira – 2010-05-21T20:05:52.473

    So I can't make Vim replay text and cursor scroll history backwards (I also expected it will handle mouse scrolling in gvim), right? I expected that while holding Ctrl+O is see my text movements reversed, finishing at just opened file positioned in the beginning. – Vi. – 2010-05-21T22:54:40.130