1

I use vim as my $MANPAGER. I will often use :/^[ ]*-F (for example) to quickly jump to a flag and read up on it. However, the equivalent :/^[\s]*-F doesn't return the same results. Why not?

4

The character classes such as \s for spaces or \d for digits do not work inside a [ ] collection in the Vim flavor of regexp. So [\s] is actually matching either a backslash or a lowercase "s".

But in your case, you don't need a [ ] collection anyways, because the \s is the only thing you're matching there.

So this should work just fine:

/^\s*-F

One alternative is to use a full character class expression (not one using the \-letter shorthand notation, but the [:class:] one), in which case /^[[:blank:]]*-F is what you want here ([:blank:] is the one matching a space or a tab, same as \s.)

3
  • "do not work inside a [ ] group".... does this pertain to all of the characters at vimregex.com/#metacharacters and no others?
    – alec
    May 10 at 0:05
  • 1
    @alec It's complicated... Some such as \t will actually work inside [ ]. See :help /[ for more details.
    – filbranden
    May 10 at 0:06
  • 1
    That's confusing, I've only really ever seen character classes written inside square brackets. Thanks for the quick and informative reply.
    – Harv
    May 10 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.