Unable to match one or more whitespaces in Vim

前端 未结 9 646
一个人的身影
一个人的身影 2020-12-28 11:55

I want match spaces at the beginning of lines in Vim

PseudoCode of what I want to do

^( )*

I know the following fr

相关标签:
9条回答
  • 2020-12-28 12:21

    If you're looking to match any sort of whitespace (be it space or tab) the following regex is applicable

    ^[\s]*
    

    ^ matches the beginning of the line
    [\s] / [\t] matches space or tab character (both seem to work, though according to the vim documentation you should use [\s]
    * provides repetition, 0 or more.

    of course using the / to search as mentioned by others.

    0 讨论(0)
  • 2020-12-28 12:22

    If I understand correctly..

    / * will match 0 or more spaces

    / {0,n} will match 0 to n spaces (where n is a number)

    To match 1 or more space starting from the beginning of the line:

    /^ \+
    
    0 讨论(0)
  • 2020-12-28 12:25

    I think you can really do is match spaces until some kind of non-space character, right? Try this:

    ^\( \+\)

    Or, for any kind of whitespace:

    ^\(\s\+\)

    In Vim regex (, ) and + need to be escaped. Also, if you planning to use backreference, the syntax is \1 (the first group, for example) and not $1 like Perl.

    0 讨论(0)
  • 2020-12-28 12:26

    / * matches zero or more spaces.

    Example:

    /foo *bar will match foobar, foo bar, foo    bar,etcetera`.

    Note that if you want to use parenthesis like in your example then you need to escape them with a \\. Vim expressions are not standard Perl regular expressions nor POSIX regular expressions. You example would be:

    \\( \\)*
    
    0 讨论(0)
  • 2020-12-28 12:31

    Typing

    /^ \+
    

    In command mode will match one or more space characters at the beginning of a line.

    Typing

    /^\s\+
    

    In command mode will match one or more whitespace characters (tabs etc. as well as spaces) at the beginning of a line.

    0 讨论(0)
  • 2020-12-28 12:37

    To match spaces or tabs from the beginning of the line, you can also use: ^\s*.

    0 讨论(0)
提交回复
热议问题