views:

316

answers:

4

How to search word start \word in vim. I can do it using the find menu. Is there any other short cut for this?

+4  A: 

You can search for most anything in your document using regular expressions. From normal mode, type '/' and then start typing your regular expression, and then press enter. '\<' would match the beginning of a word, so

/\<foo

would match the string 'foo' but only where it is at the beginning of a word (preceded by whitespace in most cases).

You can search for the backslash character by escaping it with a backslash, so:

/\<\\foo

Would find the pattern '\foo' at the beginning of a word.

Nick Meyer
Combine your answer and mine, and you get: /\<\\word :-)
Chris Jester-Young
Thanks, Chris. Edited.
Nick Meyer
`\<\\` isn't particularly meaningful, though: `\` isn't a word character.
ephemient
+5  A: 

Try:

/\\word

in command mode.

Chris Jester-Young
+3  A: 

The reason searching for something including "\" is different is because "\" is a special character and needs to be escaped (prepended with a backslash)

Similarly, to search for "$100", which includes the special character "$":

Press /
Type \$100
Press return

To search for "abc", which doesn't include a special character:

Press /
Type abc
Press return
frou
+2  A: 

Not directly relevant (/\\word is the the correct solution, and nothing here changes that), but for your information:

:h magic

If you are for a pattern with many characters with special meaning to regexes, you may find "nomagic" and "very nomagic" mode useful.

/\V^.$

will search for the literal string ^.$, instead of "lines of exactly one character" (\v "very magic" and the default \m "magic" modes) or "lines of exactly one period" (\M "nomagic" mode).

ephemient