yuvaeasy
yuvaeasy

Reputation: 833

vim command for search and substitute

This is my question.

In vim editor I want to select all the words between double quotes through out the whole file and i want to replace the selected words by preceding that with gettext string. Please anybody tell me vim command to do this.

for ex: if the file contains

printf("first string\n"); printf("second string\n");

After replacement my file should like this

printf(gettext("first string\n")); printf(gettext("second string\n"));

Upvotes: 0

Views: 325

Answers (3)

Yannick Loiseau
Yannick Loiseau

Reputation: 1454

in command mode:

:%s!"\([^"]*\)"!gettext("\1")!g

the % is for whole document, [^"]* for anything except quotes, and the g at the end for all occurence in the line (default is only the first one). The separator char can be anything not in the regexp... I often use ! rather than / (more convenient when dealing with path e.g.)

Upvotes: 1

anubhava
anubhava

Reputation: 784998

try this in vim:

:%s/\(".*"\)/gettext(\1)/g

Here \( and \) is being used to group the text and \1 is then used to put 1st backreference back along with gettext function.

Upvotes: 1

Lazarus
Lazarus

Reputation: 43064

You should be able to do:

s/\".\{-}\"/gettext\(\1\)/g

Upvotes: 1

Related Questions