Mike S
Mike S

Reputation: 308

How to add characters to non-blank lines using vim

I have a text document that has blank lines and lines that start with capitalized months.

I want to add "- " to the beginning of each non-blank line.

Tried this:

1,$s/^[A-Z]/- / 

and it removes the first letter of the month (e.g. "- une" when it should be "- June") How can I add that letter back? Or is there a "better" way of doing this for a large document where I need to keep the blank lines untouched.

Upvotes: 1

Views: 722

Answers (2)

Ben
Ben

Reputation: 8905

You have an answer that would either add the matched text back in, or avoid matching the text altogether during the substitution. Another method is to specify the match as zero-width, or set the ending of the match, so that the text is matched but not replaced:

%s/^[A-Z]\@=/- /

or

%s/^\ze[A-Z]/- /

See :help zero-width, :help /\@=, :help /\ze.

Upvotes: 2

Kent
Kent

Reputation: 195039

try this, it should give what you want

%s/^[A-Z]/- &/

or use :g:

:g/^[A-Z]/s/^/- /

Upvotes: 4

Related Questions