In vim, how do I go back to where I was before a search?

╄→гoц情女王★ 提交于 2019-12-04 07:18:59

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/53911/in-vim-how-do-i-go-back-to-where-i-was-before-a-search

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!