Vim: How do you efficiently search for text?

30

13

Whenever you want to search for text in vim you would type / and then the string you are looking for.

However, when the string is a long one, and you want to do this multiple times, is there a way to do it so that you don't have to type /andthisreallylongstring more than once?

Dark Templar

Posted 2011-10-11T06:39:02.380

Reputation: 2 329

12

I recommend you briefly peruse a Vim Cheat Sheet or Vim Tips - you may find many other ways of getting more out of vim.

– RedGrittyBrick – 2011-10-11T10:12:42.450

Protip: there are text editors for Linux that don't require weeks of studying to use. – BlueRaja - Danny Pflughoeft – 2011-10-11T22:10:24.753

4@BlueRaja: not sure this is a good place for an editor war. Also for an OS war as there are also nice and user-friendly text editors for Windows or Mac OS. Having to study how to be efficient with an editor will make you probably more efficient in the end than sticking with basic features. – Benoit – 2011-10-12T07:01:55.920

Answers

61

Press "n" after you completed a search, then it will repeat the previous action(search).

lamwaiman1988

Posted 2011-10-11T06:39:02.380

Reputation: 2 551

12Also, N will go to the previous result. – Jin – 2011-10-11T07:12:37.253

15Also, hitting the up-arrow after hitting / will recall the last search term – bastibe – 2011-10-11T12:41:23.047

6Also q/ will show an editable window with all recent search term. – UncleZeiv – 2011-10-11T16:22:26.197

7Also, hitting / and Enter will repeat the previous search. Hitting ? and Enter will repeat the previous search backwards. – Andy Lester – 2011-10-11T17:09:43.817

3/Ctrl-F is an alternative to q/ that may be an easier mnemonic (ctrl-f == find). Like q, it also works for command mode. – idbrii – 2011-10-12T01:53:01.350

@pydave: however if you modify the cedit setting, your shortcut does not work anymore. – Benoit – 2011-10-12T07:03:21.893

@Benoit: Sure it does. It's just now whatever you changed cedit to. – idbrii – 2011-10-12T17:19:52.410

34

If the string you search is under the cursor, then you can type * to search for it forward, or # to search for it backwards.

Didier Trosset

Posted 2011-10-11T06:39:02.380

Reputation: 629

19

How I usually search is something like;

  1. Press '/'
  2. Enter Search Term
  3. Press Enter
  4. Use 'n' and 'N' to navigate forward and backwards within the search.

Vim keeps a history of your searches, so if you need previous searches you can press '/' and use the UP and DOWN keys or <Ctrl-P> <Ctrl-N> to browse the search history.

You can also use '?' instead of '/' to search backwards.

I also like to use:

set ignorecase smartcase

Now it will only be a case sensitive search if you have uppercase characters in the search string.

Valadas

Posted 2011-10-11T06:39:02.380

Reputation: 191

15

Two additional tips that can make your searching more efficient are set hlsearch and set incsearch.

  • set hlsearch, well, highlights your search results. Type :noh to turn them off when you don't need it anymore.

  • set incsearch turns on incremental searching.

    Extracted from :help incsearch

    While typing a search command, show where the pattern, as it was typed so far, matches. The matched string is highlighted. If the pattern is invalid or not found, nothing is shown. The screen will be updated often, this is only useful on fast terminals.

    Without incsearch, your results will only be displayed after pressing <CR> on your search keyword.

    Example:

    You want to search for wildignore

    enter image description here

    You type /wild and the first match gets highlighted automatically.

    enter image description here

    Then type i and it goes on to the next correct match.

    enter image description here

Jin

Posted 2011-10-11T06:39:02.380

Reputation: 4 107

13

Another tip not yet covered: Pressing the characters q and then /, will give you a list of your previous search terms. You may edit them like any other line in a document, and then press enter to re-use them for a new search.

Marcin

Posted 2011-10-11T06:39:02.380

Reputation: 329

3Was looking for this. This is an important note, and very on-topic. – cincodenada – 2011-10-11T20:29:07.180

8

After pressing / to start the search, you can up-arrow to scroll through your search history.

You can enter a regular expression to avoid having to type out the whole search term: and.*longstring

Graham Borland

Posted 2011-10-11T06:39:02.380

Reputation: 321

7

Does just hitting / with no arguments following not work for you? On all our systems, that causes a repeat of whatever search was done last.

Kyle Banerjee

Posted 2011-10-11T06:39:02.380

Reputation: 171

6

There is a nice pattern you can follow to search for yanked text. Then you don't even have to type the word in itself, if you've navigated to it by means other than a search. In summary, if you're on the first character of the word you want to search for:

v
e
y
/
Ctrl-r
0
Enter

(then continue to next match with n or N)

However * (or #) accomplishes pretty much the same thing as all these commands. The only difference is you can be flexible by how much of the word you specify (rather than e, to the end of the word, you can select as much as you want to, e.g. 9l)

Benedict

Posted 2011-10-11T06:39:02.380

Reputation: 161

1

Additionally, you can paste the contents of any register into the search prompt by pressing Ctrl+R and then the name of the register, e.g., / Ctrl+R 0 Enter.

tdammers

Posted 2011-10-11T06:39:02.380

Reputation: 203

I think you a word. – dotancohen – 2013-12-26T07:35:56.807

1

  • /string — Search forward for string
  • ?string — Search back for string
  • n — Search for next instance of string
  • N — Search for previous instance of string

AbdulFattah Popoola

Posted 2011-10-11T06:39:02.380

Reputation: 111

1

A lot of time when programming I'll want to search for aVeryLongVariableNameInTheProgram.

In vim you can very easily yank text into a buffer/register and then search for it:

  • Put cursor on first character of word
  • "Byw to yank text aVeryLongVariableNameInTheProgram into buffer B (choose your favorite, I used B)
  • /^RB (slash, Ctrl-R, B) to paste buffer B on the search line

Of course you can also yank to end of line, etc. instead of yanking a single word - I just happen to use the above sequence all the time.

Dan7119

Posted 2011-10-11T06:39:02.380

Reputation: 223

0

Is the long string that you're entering a variable name? Or a long string that's in your text? Then hit * to search for the word/string under your cursor.

Andy Lester

Posted 2011-10-11T06:39:02.380

Reputation: 1 121