Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from programmers.stackexchange.com Oct 11 '11 at 7:07

This question came from our site for professional programmers interested in conceptual questions about software development.

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

12 Answers 12

up vote 57 down vote accepted

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

share|improve this answer
12  
Also, N will go to the previous result. –  Jin Oct 11 '11 at 7:12
15  
Also, hitting the up-arrow after hitting / will recall the last search term –  bastibe Oct 11 '11 at 12:41
6  
Also q/ will show an editable window with all recent search term. –  UncleZeiv Oct 11 '11 at 16:22
7  
Also, hitting / and Enter will repeat the previous search. Hitting ? and Enter will repeat the previous search backwards. –  Andy Lester Oct 11 '11 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. –  idbrise Oct 12 '11 at 1:53

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

share|improve this answer

How I usualy 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 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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
2  
Was looking for this. This is an important note, and very on-topic. –  cincodenada Oct 11 '11 at 20:29

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

share|improve this answer

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.

share|improve this answer

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)

share|improve this answer

Additionally, you can paste the contents of any register into the search prompt by pressing and then the name of the register.

share|improve this answer
    
I think you a word. –  dotancohen Dec 26 '13 at 7:35
  • /string — Search forward for string
  • ?string — Search back for string
  • n — Search for next instance of string
  • N — Search for previous instance of string
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.