Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searching whole word in Vim (dash character)

Tags:

find

vim

vi

editor

I know for searching a whole word I should use /\<mypattern\>. But this is not true for dash (+U002d) character and /\<-\> always fails. I also try /\<\%d45\> and it fails too. anyone know the reason?

Edit2: As @bobbogo mentioned dash is not in 'iskeyword' so I add :set isk+=- and /\<-\> works!

Edit1: I think in Vim /\<word\> only is valid for alphanumeric characters and we shouldn't use it for punctuation characters (see Edit2). I should change my question and ask how we can search punctuation character as a whole world for example I want my search found the question mark in "a ? b" and patterns like "??" and "abc?" shouldn't be valid.

like image 422
Saman Avatar asked Feb 04 '11 17:02

Saman


People also ask

How do I match a word in Vim?

In normal mode, press / to start a search, then type the pattern ( \<i\> ), then press Enter. If you have an example of the word you want to find on screen, you do not need to enter a search pattern. Simply move the cursor anywhere within the word, then press * to search for the next occurrence of that whole word.


1 Answers

\< matches the zero-width boundary between a non-word character and a word character. What is a word character? It's specified by the isk option (:help isk).

Since - is not in your isk option, then - can never start a word, thus \<- will never match.

I don't know what you want, but /\>-\< will match the dash in hello-word.

like image 106
bobbogo Avatar answered Sep 28 '22 09:09

bobbogo