tags:

views:

146

answers:

2

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
+3  A: 
:%s/\(create_title\)/\1X/g

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

apphacker
Jefromi
+6  A: 

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.

Jefromi
That only searches the current line.
apphacker
apphacker
Edited to search the whole file. My bad.
Jefromi
`\0` also works
rampion
I tend to use either this or \0X, but just to add another option: `:%s/create_title\zs/X/`. `\zs` makes the match start at that point, so nothing is replaced with X at the end of the string "create_title".
Al