0

There are several answers on how to search a whole word in vim. For example, this link How to do whole-word search similar to "grep -w" in Vim answers it.

I am wondering is there any alternative to \<word\> in vim to search whole word? The reason I am asking is out of curiosity. I am wondering an alternative in vim. I know some other tools, such as grep can do the same task, but I am interested in finding way in vim.

What's wrong with \<\>?

I am asking alternative.

E_net4
  • 27,810
  • 13
  • 101
  • 139
iod93883
  • 31
  • 2
  • 2
    What's wrong with `\<` `\>`? – melpomene Oct 20 '16 at 17:25
  • I think the downvotes are because it is not clear what you are looking for. Is there another way to search for words? Yes, of course. For example, you can use `:!grep -w word`. But why? Just to be different? Or do you have some actual purpose? – Mad Wombat Oct 20 '16 at 18:00
  • What exactly do you mean by "alternative"? And why are you looking for one? What are the constraints you're operating under? – melpomene Oct 20 '16 at 18:18
  • 2
    What do you mean by "not good"? What are your criteria for "good"? (We can't suggest good alternatives without knowing how to rate them.) – melpomene Oct 20 '16 at 18:20
  • @melpomene, :alternative" literally means other way (to find whole word in `vim`. And why are you looking for one? Curiosity (which, I believe, led mankind to progress up to current level). Is there anything wrong to find alternative? – iod93883 Oct 20 '16 at 18:22
  • Is this for interactive use, or for syntax highlighting, or for a script, or for macros, or ...? Should it respect the `iskeyword` option (like `\<` `\>` does) or is that not important? – melpomene Oct 20 '16 at 18:22
  • @iod93883 Your question is like "I'm looking for an alternative to the number `pi` (3.14159...). I'm asking because I personally did not find it good to use `pi`." – melpomene Oct 20 '16 at 18:23
  • 1
    Sorry to miss that information. This is for interactive use. – iod93883 Oct 20 '16 at 18:24
  • 1
    @melpomene I think you are confusing. `pi` is defined as constant ratio, where as in computer software, there are, in general, multiple ways of achieving things. If there is no alternative, please put `no` in answer. I am surprised why there is no direct answer for this question. – iod93883 Oct 20 '16 at 18:27
  • Are you looking for things like `*` and `#`? (search forward/backward for the word under the cursor) – William Pursell Oct 20 '16 at 18:42
  • Not word under cursor. I am looking something like grep -w "word" – iod93883 Oct 21 '16 at 12:59

2 Answers2

2

Technically yes, you can place your cursor over that word in the document and press * which will automatically populate the search with the word surrounded by word boundaries. Same with # for searching backwards.

Also, Vim's default search escaping is quite verbose and poorly designed. I highly recommend starting all searches with the "very magic" switch, \v, so that you don't have to escape special characters. As in you can type /\v<word> and the < and > will be treated correctly as word boundaries without having to escape them.

A mapping to "enable" very magic for all searches:

nnoremap / /\v
nnoremap ? ?\v
Andy Ray
  • 30,372
  • 14
  • 101
  • 138
0

See also answers here:

The second link is probably (I'm guessing) the most applicable to your question, especially if you put in in your .vimrc

Even if you don't, you only need to do the mapping once. Of course, mapping w overrides its typical "next word" behavior. This might be acceptable to you, since W (capital W) has a similar behavior, of "next WORD" (which is typically what I actually want to do).


  • ASIDE: Vim has between two use cases of "non-blank-characters."
    From :help word in Vim (Emphasis mine on '**or**') :
These commands move over words or WORDS.

                                                       *word*
A word consists of a sequence of letters, digits and underscores, **or** a
sequence of other non-blank characters, separated with white space (spaces,
tabs, <EOL>).  This can be changed with the 'iskeyword' option.  An empty 
line is also considered to be a word.
                                                       *WORD*
A WORD consists of a sequence of non-blank characters, separated with white
space.  An empty line is also considered to be a WORD.

What this effectively means is that:
a word is        /(w+|[\W]+/   ---   e.g., *foo* is 3 words total (*, foo, *)
But a WORD is /S+/                  ---   e.g., *foo* is 1 WORD total

Randall
  • 2,859
  • 1
  • 21
  • 24