Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To append a match efficiently in Vim :g/---/s/---/X/

Tags:

vim

append

How can you refer to the match in the command g in Vim?

I would like to put X after the match without replacing the match. For instance, in the following command without writing the create_title twice.

:g/create_title/s/create_title/X/

You should get

create_titleX

by running the command to

create_tile
like image 964
Léo Léopold Hertz 준영 Avatar asked Aug 27 '09 22:08

Léo Léopold Hertz 준영


2 Answers

I'm not sure why you need the g portion of the command - the substitute will only act on matching lines. Here's what you're looking for:

:%s/create_title/&X/

The & represents the entire text which was matched.

like image 60
Cascabel Avatar answered Nov 15 '22 19:11

Cascabel


:%s/\(create_title\)/\1X/g

works for me. (if I understand your question correctly).

like image 24
Bjorn Avatar answered Nov 15 '22 21:11

Bjorn