Text-Processing

用引號括起多行 Vim

  • June 23, 2014

我有以下形式的塊:

   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

我需要將說明它們應該用引號括起來的行放在引號中:

   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"

有沒有半自動的方式用 Vim 做到這一點?我認為可能的解決方案可能涉及g命令。

使用正則表達式:

:%s/.*is supposed.*/"&"/

如果“半自動”是指您希望在每次替換之前得到提示,只需將/c修飾符添加到替換模式:

:%s/.*is supposed.*/"&"/c

解釋

  • :%s表示將此替換應用於目前緩衝區中的所有行
  • 我們匹配的模式是包含單詞的任何行is supposed(如果其他一些行包含單詞“應該”而後面沒有“用引號括起來”,您可以隨時將模式更改為.*is supposed to be enclosed in quotes.*
  • 我們用來替換匹配模式的字元串是"&",其中&代表模式匹配的任何內容。

引用自:https://unix.stackexchange.com/questions/138687