Unable to match one or more whitespaces in Vim

前端 未结 9 647
一个人的身影
一个人的身影 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:44

    Spaces at the beginning of the line in Vim:

    /^  *
    

    That's:

    • '/' for search.
    • '^' for start of line.
    • ' ' for at least one space.
    • ' *' for zero or more spaces after that.
    0 讨论(0)
  • 2020-12-28 12:45

    Btw, don't be surprised if you are using the hlsearch option and your whole text lights up after entering / * - instead of just the spaces. That's because zero spaces can match anywhere!

    So matching zero or more of anything is only helpful when used in conjunction with something else.

    Addition after clarification of question:

    To match one or more whitespaces at the beginning of a line do:

    /^\s\+
    

    See:

    :help whitespace
    :help /\+
    
    0 讨论(0)
  • 2020-12-28 12:45

    You're almost there. You don't need the ( ). The regex is just "^ *", and in Vim you search with / so you'd enter /^ *

    Note that this matches on every line, because every line starts with zero or more spaces! So, do you really intend that? If you meant "one or more spaces", you need to replace * by \+ (in most regex languages, it's +, but the + is escaped in vim). So, /^ \+

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