284

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?

Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • possible duplicate of [Move cursor to its last position](http://stackoverflow.com/questions/5052079/move-cursor-to-its-last-position) – Simone Carletti Sep 19 '15 at 09:09

7 Answers7

444

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.

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
  • 2
    It also appears that pressing CTRL+O enough times will also start taking you back through previously opened files. – Mark Biek Sep 10 '08 at 12:50
  • 1
    Ctrl+O is my preferred method also. I use it constantly and wish other editors replicated its behavior. – amrox Sep 10 '08 at 12:59
  • 54
    Yes, CTRL-O and CTRL-I seem to take you back and forth where you've been, nice. – Edward Tanguay Sep 10 '08 at 13:04
  • With CTags, Ctrl+T will take you back... don't know how it is different from Ctrl+O. – Agnel Kurian Sep 10 '08 at 13:08
  • 8
    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 Sep 13 '08 at 17:26
  • 1
    I've found that if I jump vía `:70` and then `:100`, pressing `ctrl+o` once goes back to the original location, *not* line 70. :( – WhyNotHugo May 04 '15 at 11:56
  • @Hugo read `:h up-down-motions`, where it states about `:[range]` that “in contrast with G this command does not modify the jumplist.” – Amir Oct 07 '15 at 08:23
101

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.

Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
Max Cantor
  • 8,229
  • 7
  • 45
  • 59
48

I've always done by it setting a mark.

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

  2. To get back to the mark press ' [letter]. For example, 'a takes you back to the line mark set in step 1. To get back to the column position of the row where you marked the line, use `a (back-tick [letter]).

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 mb. 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 wherever you were before.

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

Mark Stewart
  • 2,046
  • 4
  • 22
  • 32
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
  • 6
    luckily a mark is often unnecessary since vim keeps special track of some positions and gives you access to them with ``, ^O, etc. – aehlke Apr 14 '11 at 03:19
  • the mark only seems to take me to the beginning of the line on which it was set. ): – john-jones Apr 18 '15 at 09:39
  • 1
    @HermannIngjaldsson You may have figured this out by now, but while using ' will take you to the marked line, ` will take you to the exact location. – azmr Feb 11 '16 at 16:11
43

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

André
  • 12,971
  • 3
  • 33
  • 45
38

CTRL+O and CTRL+I, for jumping back and forward.

none
  • 11,793
  • 9
  • 51
  • 87
sale
  • 389
  • 3
  • 2
8

I use this one:

nnoremap / ms/
nnoremap ? ms?

Then if I search something by using / or ?, I can go back quickly by `s. You could replace the letter s to any letter you like.

Ethan Zhang
  • 4,189
  • 3
  • 22
  • 17
  • Only caveat here is it breaks searching in a window such as NerdTree where `m` is defined to be something other than mark, in the case of NerdTree it is menu. – Kris Mar 14 '14 at 10:31
7

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

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • It's a treat to see you when I search for vim tips. I've been using ctrl-o more along with ctrl-i because it steps back and forth easier and more automatically. – MattK Dec 22 '11 at 00:54