如何在 vim 中的每个 "string" 前面插入一个 word/text 并删除引号
How to insert a word/text in front of every "string" in vim and remove quotation marks
我的文件中有这样的字符串:case field == "first_name" || field == "last_name"
,我想删除引号并将文本 constants.
放在所有引号的前面。
这是最终目标:case field == constants.first_name || field == constants.last_name
。
使用搜索和替换,只需运行以下命令:
%s/\("\)\([^"]\{-}\)/constants./g
逐个部分:
% - In all lines
s - substitute
\("\)\([^"]\{-}\) - part1 (find any string that is surronded by double-quotes)
constants. - by part2 (add constants. to second cached group in regex)
g - globally
您可以使用宏来做到这一点。
按 qaq
清空 a
寄存器。
按 :1
转到第 1 行
按qa
开始在a
寄存器中记录。
然后,搜索 /"\S\+"
这样的文本。
它将突出显示文本。
然后,键入 xiconstants.
,然后按 ESC,然后按 f"
,然后按 x
。
键入 @a
以递归地重播寄存器。
按 q
.
停止录制
以后按@a
一次,递归替换,直到所有这些词都被替换。
我的文件中有这样的字符串:case field == "first_name" || field == "last_name"
,我想删除引号并将文本 constants.
放在所有引号的前面。
这是最终目标:case field == constants.first_name || field == constants.last_name
。
使用搜索和替换,只需运行以下命令:
%s/\("\)\([^"]\{-}\)/constants./g
逐个部分:
% - In all lines
s - substitute
\("\)\([^"]\{-}\) - part1 (find any string that is surronded by double-quotes)
constants. - by part2 (add constants. to second cached group in regex)
g - globally
您可以使用宏来做到这一点。
按 qaq
清空 a
寄存器。
按 :1
按qa
开始在a
寄存器中记录。
然后,搜索 /"\S\+"
这样的文本。
它将突出显示文本。
然后,键入 xiconstants.
,然后按 ESC,然后按 f"
,然后按 x
。
键入 @a
以递归地重播寄存器。
按 q
.
以后按@a
一次,递归替换,直到所有这些词都被替换。