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?

  • 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 Dec 19 '14 at 11:28

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.


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.

  • 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
  • 1
    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
  • 3
    \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
  • 1
    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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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