Unable to match one or more whitespaces in Vim

Solution 1:

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.

Solution 2:

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 /\+

Solution 3:

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:

/^ \+

Solution 4:

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.

Solution 5:

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.