Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Vim normal mode, the 0 command takes you to the first column on the line and ^ takes you to the logical start of line (e.g. the first non-whitespace character). In the regex world, ^ matches the first character on the line, whitespace or not. Does Vim have a pattern that behaves like its '^' command--matching the logical beginning of a line?

share|improve this question
I think ^ in a regex normally matches the start of the line, not the first character. ^. will match the start and then the first character, not the start and then the second character. –  bdsl yesterday

2 Answers 2

up vote 7 down vote accepted

There's no shortcut to match the first non-whitespace character on a line, you have to build the pattern yourself, like:


If you don't want to include the whitespace in your match, you have to use a zero-width assertion, like:


Not exactly pretty, but at least it gets the job done.

share|improve this answer

To match the first non-whitespace character, you'd just use \S like you normally do.

If you use ^ in a regex in vim, it will match the actual start of the line, even if it contains whitespace.

For instance, this line starts with a space:

 <- there's a space there you can't see :)

This vim command will remove the leading space:

:%s/^ //

resulting in the following:

<- there's a space there you can't see :)

So, the regex will behave as you expect, even if the command doesn't.

share|improve this answer
I presume you're going to explain the downvote. This answer, as far as I know, is correct. –  Michael Hampton Nov 14 '12 at 20:08
I didn't downvote, but by way of clarification: I was wondering if Vim has an operator to match the first non-whitespace character of the line. The ^ operator (like all sane regex implementations), will match the first character even if it is whitespace. –  Michael Nov 14 '12 at 20:19
Well, how were you expecting to do it normally? Usually in a regex you would use something like \S to match the first non-whitespace character. –  Michael Hampton Nov 14 '12 at 20:23
\S will match any non-whitespace character. To put it another way, I'm wondering if Vim has a zero-width shorthand for this: ^\W*\S. –  Michael Nov 14 '12 at 20:31
No engine that I am aware of provides that functionality, but then again--when would you have cared in a general-purpose engine? Vim isn't a general purpose regex engine. It is an editor that has a regex engine so I was wondering if it had any special constructs for something that only matters inside an editor. –  Michael Nov 14 '12 at 20:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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