Vim: How do you efficiently search for text?

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


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


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.


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