开发者

vim command for search and substitute

开发者 https://www.devze.com 2023-02-20 06:52 出处:网络
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 gette

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"));


You should be able to do:

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


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消