67

I want match spaces at the beginning of lines in Vim

PseudoCode of what I want to do

^( )*

I know the following from manual

notation        meaning             equivalent  decimal value(s)        
-----------------------------------------------------------------------

<Space>         space                            32     *space* 

I am not sure how to use the decimal value 32.

How can you match one or more whitespaces in Vim?

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

9 Answers9

119

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.

ahcox
  • 9,349
  • 5
  • 33
  • 38
Lucas Lindström
  • 1,928
  • 1
  • 12
  • 14
19

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 /\+
kenorb
  • 155,785
  • 88
  • 678
  • 743
user55400
  • 3,929
  • 1
  • 21
  • 13
9

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:

/^ \+
gacrux
  • 930
  • 1
  • 9
  • 16
6

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.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • If you want to match 1 or more spaces you can use /^ \+ instead of two spaces. Same number of chars but a little clearer I think. – gacrux May 08 '09 at 10:16
  • @paxdiablo, could you let me knoe if I would like to match at least 2 space in the middle of the lines? – Amir Jan 29 '14 at 11:42
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.

Andy
  • 3,794
  • 24
  • 28
  • 1
    According to the documentation you're absolutely right, but it does seem to work in gvim. I'll alter accordingly. – Andy May 08 '09 at 10:19
  • I've had a play and [\t]* works to match both space and tab, but [\t] matches only tabs. Must be something I'm missing in the regex. – Andy May 08 '09 at 10:25
  • 1
    Hi, `^[\s]*` matches `\ ` or `s`, not whitespace. What you want is`^\s*`, but that also matches zero spaces: i.e., every line. `^\s\+` Does the real job. – ahcox Feb 13 '15 at 13:44
1

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

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    In vim you need to escape the + - so it would be /^ \+ This is different from perl compatible regex I think. – gacrux May 08 '09 at 10:17
0

/ * 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:

\\( \\)*
kenorb
  • 155,785
  • 88
  • 678
  • 743
Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
0

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

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • If you're specifying the pattern as a string (e.g. as an argument to `matchstr()`), make sure to use single quoted. A double quoted string will unescape the `\s` and make it an `s`. – webninja Aug 04 '21 at 11:54
0

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.