If I select multiple lines and do substitution at the beginning of text it actually substitutes at the beginning of the line. For example, given:


If we do:


then we get:

-->    abc
-->        def
-->      ghi

but this is no different than


What we actually want is:

--> abc
    --> def
  --> ghi

I would have expected the circumflex accent, ^, to go to the beginning of text, not the beginning of line like 0 does. What is going wrong here?

2 Answers 2


^ represents the beginning of the line in regex language it is not related to the motion ^ that's why your solution won't work.

You'll have to use that:


Here you use the substitution with a capture group, here is a detail of the pattern:

\(    \)     Will be used to capture characters and reuse them in the subtitution string
  ^\s*       You capture any number of whitespaces at the beginning of the line

In the substitution string \1 is used to insert the content of your capture group.

You can even simplify the expression with:


Here you don't use capture group (\( \)) because & makes reference to the whole matched pattern.

  • What is the purpose of the "1"? Could you walk through this expression and explain what it is doing? Commented Aug 11, 2016 at 8:04
  • @TylerDurden: Yup I edited my answer, I hope it clarifies the expressions.
    – statox
    Commented Aug 11, 2016 at 8:06

What's going wrong, here, is your reluctance to Read The Fantastic Manual.

First, V is useless in that context because Ex commands like :s are always line-wise.

Second, vjj:s/0/-->/ would do nothing on your sample because it doesn't contain a single 0.

Third, the ^ and 0 you refer to are motions. What they do in normal/visual mode has no bearing on their meaning as search pattern atoms.

Fourth, what the ^ atom does is clearly explained in : help /^.

Fifth, you need a space after -->.

Use this pattern for a zero-width match before the first printable character on a line:


For your example:

vjj:s/^\s*\zs/--> /

Another approach:

vjj:s/^\s*/&--> /

And another one, without visual selection:

3:s/^\s*/&--> /

See :help /\s, :help /*, :help /\zs, :help s/\&.

  • It would take me a very long time to figure all this out just by "reading the manual". Just the section on "s" is pages long and it is not light reading. Reading all the pages and pages on regex expressions would take me hours, and even then I probably would not even find the part on using "z" qualifier. This is very complicated stuff to me and just say read an 800-page manual is not a very practical suggestion. Commented Aug 11, 2016 at 8:27
  • 4
    @TylerDurden And how do you think other people got the knowledge to answer your questions? Yeah, reading the doc. romainl is pretty right: you just asked 3 questions which could have been answered with the man or a simple search on this site. What you have to do is to learn how to search efficiently in the documentation and be ready to spend some time learning how to use Vim.
    – statox
    Commented Aug 11, 2016 at 8:30
  • 4
    I second @statox here. Reading the vim documentation is hard when you start, but so is everything else. And it's a huge benefits to be able to search in it. You'll have all knowledge you need at your fingertips. Although reading all 800 pages of manual in one go is not recommended, reading the s is definitely worth it if you want to understand what you are doing. The Vim help is your first source of answer in any case.
    – nobe4
    Commented Aug 11, 2016 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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