czz
czz

Reputation: 474

vim: why whitespace character \s not working in bracket expression, but \n do

for example, put /\s in vim will match any whitespace, but /[\s] does not.

I thought it's because backslash escapes are not allowed in bracket expression as stated in https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended, but then I tested /[\n] which correctly match the end-of-line.

Why is this behavior? Is there any document describe this behavior?

Upvotes: 2

Views: 238

Answers (1)

romainl
romainl

Reputation: 196536

Vim uses its own brand of regular expression syntax which is AFAIK not documented on Wikipedia. Like everything Vim, it's documented in Vim itself, :help pattern, and there is a good introduction online too.

Collections contain single characters. Since \s is itself a collection of whitespace characters and not a single character it can't be contained in another collection. If you want to include whitespace characters in a collection, you'll have to include them one-by-one: [ \t].

[\n] works because \n is a single character.

:help pattern
:help /[]
:help /[\n]

Upvotes: 2

Related Questions