35

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?

3
  • 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. Oct 11, 2011 at 10:12
  • Protip: there are text editors for Linux that don't require weeks of studying to use. Oct 11, 2011 at 22:10
  • 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
    Oct 12, 2011 at 7:01

14 Answers 14

63

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

7
  • 13
    Also, N will go to the previous result.
    – Jin
    Oct 11, 2011 at 7:12
  • 16
    Also, hitting the up-arrow after hitting / will recall the last search term
    – bastibe
    Oct 11, 2011 at 12:41
  • 7
    Also q/ will show an editable window with all recent search term.
    – UncleZeiv
    Oct 11, 2011 at 16:22
  • 8
    Also, hitting / and Enter will repeat the previous search. Hitting ? and Enter will repeat the previous search backwards. Oct 11, 2011 at 17:09
  • 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
    Oct 12, 2011 at 1:53
35

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

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.

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

14

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.

1
  • 3
    Was looking for this. This is an important note, and very on-topic. Oct 11, 2011 at 20:29
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

8

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.

7

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)

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
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.

2
  • 1
    See also: :help CTRL-R_CTRL-W
    – Gebb
    Dec 7, 2023 at 0:37
  • @Gebb - nice! Thank you.
    – Dan7119
    Dec 8, 2023 at 7:21
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.

1
  • I think you a word.
    – dotancohen
    Dec 26, 2013 at 7:35
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.

0
0

I found this also useful:

  • :g// will show all lines where last search term occurs
  • :g/pattern same as above but for the given pattern term
  • ]I show lines with word under cursor (only forward)
  • [I show lines with word under cursor (in whole file)
0

Refining your search pattern interactively

(Works when 'incsearch' is active.)

Press /, start typing the search pattern.

  • CTRL-L add the next character of the highlighted match to the search pattern.
  • CRTL-H remove a character from the search pattern.
  • CTRL-G go to (highlight) the next match.
  • CTRL-T go to the previous match.

See :help /_CTRL-L

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .