196

I can search word in vim with /word. How can I search only for word, excluding searches for word1 and word2?

Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
kal
  • 28,545
  • 49
  • 129
  • 149
  • 3
    Possible duplicate of [Finding exact match in vim](https://stackoverflow.com/questions/22147639/finding-exact-match-in-vim) – LSchueler Jul 12 '18 at 09:27

5 Answers5

262

like this:

/\<word\>

\< means beginning of a word, and \> means the end of a word,

Adding @Roe's comment:
VIM provides a shortcut for this. If you already have word on screen and you want to find other instances of it, you can put the cursor on the word and press '*' to search forward in the file or '#' to search backwards.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • 6
    placing your cursor over a word and pressing * (forward) or # (backward) is a shortcut – falstro Jan 19 '09 at 19:53
  • 1
    why is this beneficial vs just doing: /word – jimh Mar 27 '17 at 08:15
  • 3
    @semja: let's say you have a variable 'data' and its valid indication 'data_valid'. If I want to search only for 'data' but not 'data_valid', then '/data' won't do the job. I need to tell vim where to *stop* matching the pattern. – Nathan Fellman Mar 30 '17 at 09:19
  • This fails in following example: `1word1` See https://stackoverflow.com/a/22148017/2609994 for a solution. – LSchueler Jul 12 '18 at 09:28
  • 1
    Is there a `vim` configuration to make it search exact word without wrapping the search string with `\<` and `\<` characters? – daparic Sep 14 '19 at 21:45
  • @eigenfield, typing g* seems to do what you want http://vimdoc.sourceforge.net/htmldoc/pattern.html#gstar – leafmeal Nov 08 '21 at 22:02
  • I know this should be the answer, but vim is ignoring it for me. It just acts as if I didn't add `/>` at the end. I am fairly sure this solution has worked for me in the past, too. – felwithe Apr 12 '22 at 21:02
  • @felwithe, maybe it's a typo, but if not, you need to be aware that you used forward slash `/`. You should use backslash '\' at the end of you term. With `/\` (wrongly typing) you actually try to find words that start with `word`, so you will match `word`, `word_data`, `wordy`, and so on. – Damir Dec 28 '22 at 17:02
92
  1. vim filename
  2. press /
  3. type word which you want to search
  4. press Enter
Mushir
  • 1,263
  • 9
  • 12
  • 2
    Could someone explain why this may have received down votes? Worked for me. – Kiee Jan 05 '16 at 10:14
  • 24
    Because the OP asked to search specifically for `"word"`. The solution posted here basically does a search for `%word%`, whether its `thiswordisawesome` or `word56` or `26word`. – Jguy Jan 06 '16 at 16:21
  • 17
    Also, because the OP explicitly stated that they know about the `/` command. As such, this answer is not helpful at all. – cmaster - reinstate monica Aug 11 '16 at 08:57
35

If you are working in Ubuntu,follow the steps:

  1. Press / and type word to search
  2. To search in forward press 'SHIFT' key with * key
  3. To search in backward press 'SHIFT' key with # key
SOFe
  • 7,867
  • 4
  • 33
  • 61
Priya R
  • 483
  • 4
  • 2
  • 1
    how can i highlight searched word? – jorge saraiva Dec 22 '16 at 11:43
  • 3
    @jorgesaraiva: 1st of all, you'll probably get an answer faster by asking a new question (rather than waiting for somebody to stumble upon this comment 3 months after you asked it). 2nd of all, you can do `:set hls` in Vim to highlight your search result. To turn off highlighting do `:set nohls` – Nathan Fellman Mar 30 '17 at 09:21
  • 1
    This really only works if the first result from doing `/word` is exactly `word` instead of `word.+` – pushkin Jun 08 '18 at 16:04
  • this does not work for me on an AWS ubuntu 18.04 instance. using windows git-bash to ssh to the ubuntu server if that is relevant. weird. – CodingMatters Sep 30 '19 at 01:10
34

For basic searching:

  • /pattern - search forward for pattern
  • ?pattern - search backward
  • n - repeat forward search
  • N - repeat backward

Some variables you might want to set:

  • :set ignorecase - case insensitive
  • :set smartcase - use case if any caps used
  • :set incsearch - show match as search
NeeruKSingh
  • 1,545
  • 3
  • 22
  • 26
1

Basic search (once you have opened the file in vim) using vim:

-Hit ESC on your computer keyboard

-Hit the forward slash symbol on your keyboard /

-Type the word or symbol you intend to search; for example, to search for the term "rbmq" type /rbmq

-Hit Enter

-Hit n to search forward (moving towards the end of the file) and N to search backward (moving towards the beginning of the file)

Asif
  • 664
  • 1
  • 7
  • 14
  • How does that answer the question? The OP already shows that they are aware of that. The question is how to ***avoid*** matches like `123rbmq123`... – Tomerikoo Mar 27 '22 at 07:57