tags:

views:

1672

answers:

6

Programming in vim I often go search for something, yank it, then go back to where I was, insert it, modify it.

The problem is that after I search and find, I need to MANUALLY find my way back to where I was.

Is there an automatic way to go back to where I was when I initiated my last search?

+5  A: 

I've always done by it setting a mark.

  1. In command-mode, press m [letter]. For example, m a sets a mark at the current line using a as the mark indentifier.

  2. To get back to the mark press ' [letter]. For example, ' a takes you back to the mark set in step 1.

To see all of the marks that currently set, type :marks.


On a slightly unrelated note, I just discovered another nifty thing about marks.

Let's say you jump to mark b by doing 'b. Vim automatically sets the mark ' (that's a single-quote) to be whichever line you were on before jumping to mark b.

That means you can do 'b to jump to that mark, then do '' (2 single-quotes) to jump back to whever you were before.

I discovered this accidentally using the :marks command, which shows a list of all marks.

Mark Biek
+5  A: 

The simplest way is to set a mark, with m[letter], then go back to it with '[letter]

Paul Tomblin
+20  A: 

Ctrl+O takes me to the previous location. Don't know about location before the search.

Edit: Also, `. will take you to the last change you made.

Vulcan Eager
It also appears that pressing CTRL+O enough times will also start taking you back through previously opened files.
Mark Biek
Ctrl+O is my preferred method also. I use it constantly and wish other editors replicated its behavior.
amrox
Yes, CTRL-O and CTRL-I seem to take you back and forth where you've been, nice.
Edward Tanguay
With CTags, Ctrl+T will take you back... don't know how it is different from Ctrl+O.
Vulcan Eager
Ctrl+T will only take you back if you got there using a tag. If you searched it without using tags Ctrl+T will take you back to the place you were before you searched for your last *tag*
Nathan Fellman
Hah! It's less than 2 years since this post and I don't remember any of this.
Vulcan Eager
+1  A: 

Use mark and return. To mark a spot before your search, Esc into command mode, then hit ma, where a is any letter of the alphabet. After you do your search you can return to the spot you marked by hitting `a (that's a backquote then the letter you marked your spot with) in edit mode.

Bill the Lizard
Paul Tomblin and Mark Biek, you guys are fast.
Bill the Lizard
You gotta get up pretty early in the morning to beat the Stack Overflow addicts.
Paul Tomblin
Hell, no. You gotta stay up all night !!
ldigas
+8  A: 

Use `` to jump back to the exact position you were in before you searched/jumped, or '' to jump back to the start of the line you were on before you searched/jumped.

Max
+2  A: 

You really should read :help jumplist it explains all of this very well.

André