lor*_*age 4 vim text-processing regular-expression
我有以下形式的块:
String that is not supposed to be enclosed in quotes
String that is supposed to be enclosed in quotes
String that is not supposed to be enclosed in quotes
String that is supposed to be enclosed in quotes
String that is not supposed to be enclosed in quotes
String that is supposed to be enclosed in quotes
String that is not supposed to be enclosed in quotes
String that is supposed to be enclosed in quotes
Run Code Online (Sandbox Code Playgroud)
我需要将声明它们应该用引号括起来的行放在引号中:
String that is not supposed to be enclosed in quotes
"String that is supposed to be enclosed in quotes"
String that is not supposed to be enclosed in quotes
"String that is supposed to be enclosed in quotes"
String that is not supposed to be enclosed in quotes
"String that is supposed to be enclosed in quotes"
String that is not supposed to be enclosed in quotes
"String that is supposed to be enclosed in quotes"
Run Code Online (Sandbox Code Playgroud)
是否有使用 Vim 执行此操作的半自动方法?我认为可能的解决方案可能涉及g命令。
使用正则表达式:
:%s/.*is supposed.*/"&"/
Run Code Online (Sandbox Code Playgroud)
如果“半自动”是指您希望在每次替换之前得到提示,只需将/c修饰符添加到替换模式中:
:%s/.*is supposed.*/"&"/c
Run Code Online (Sandbox Code Playgroud)
解释
:%s 表示将此替换应用于当前缓冲区中的所有行is supposed(如果其他一些行包含单词“应该”,而后面没有“用引号括起来”,您可以随时将模式更改为.*is supposed to be enclosed in quotes.*"&",其中&代表模式匹配的任何内容。