Vijay
Vijay

Reputation: 67299

simple question in vi editor on unix

how could i add some text like 'ABC' at the beginning of all the lines in vi editor? this doesnot work!

%s/^/^ABC

i know this command is used for replacing text

%s/vggv/uggv/g

Upvotes: 0

Views: 446

Answers (5)

user2323020
user2323020

Reputation:

:%s/^/ABC/g

this will add ABC in front of every line in vi editor

Upvotes: 1

bhinesley
bhinesley

Reputation: 2289

As others have said, :%s/^/ABC will do the trick. Consider what ^ means. It is a logical construct, not an actual character in the file. Therefore, you're not really replacing it, so you don't have to use ^ABC. In fact, as you've seen, ^ is treated as a string in that context.

If you wanted to skip lines that only contain whitespace, you could use :v/^[:space:]*$/s/^/ABC.

Upvotes: 1

Magnun Leno
Magnun Leno

Reputation: 2738

I really love the normal command for things like these:

:%normal IABC

Upvotes: 8

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

Doesn't :%s/^/ABC/ work for you?

Upvotes: 4

Grammin
Grammin

Reputation: 12215

You want:

:%s/^/ABC/g

That will put ABC in front of every line.

Don't forget the : in front

Upvotes: 8

Related Questions