rnso
rnso

Reputation: 24613

Enclose lines in markers using regex in Gvim

I have following simple text:

one
a line
more lines
more text
end

I want to enclose each line in >> and << using regex. Following work individually:

:%s/^/>>/g
:%s/$/<</g

However, how can I do it with one command. I tried following but they do not work:

:%s/^([.]+)$/>>\1<</g
:%s/\v^([.]+)$/>>\1<</g
:%s/([.]+)/>>\1<</g

Thanks for your help.

Upvotes: 0

Views: 43

Answers (2)

mattn
mattn

Reputation: 7733

:%s/.*/>>&<</

& mean matched whole part. And not require g flag.

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522346

Try using the following:

:%s/^\(.*\)$/>>\1<</g

I notice a problem with your current attempts, for example this one:

:%s/^([.]+)$/>>\1<</g

You are matching a literal dot when you use [.], which is a character class. Doubtless, most of the time this pattern won't match any line, unless it consists entirely of dots.

Also, parentheses need to be escaped in Vim regexes.

Read this excellent SO question and answer for more information.

Upvotes: 2

Related Questions