如何在Vim中快速引用/取消引用单词并更改引用(例如从“到”)?我知道环绕的事。vim插件,但我想只使用vim。


当前回答

在选定的文本块周围添加单引号的可视化模式映射示例:

:vnoremap qq <Esc>`>a'<Esc>`<i'<Esc>

其他回答

环绕。Vim是最简单的答案。如果你真的反对使用它,这里有一些你可以做的例子。不一定是最有效的,但这就是为什么要环绕。Vim写完了。

Quote a word, using single quotes ciw'Ctrl+r"' ciw - Delete the word the cursor is on, and end up in insert mode. ' - add the first quote. Ctrl+r" - Insert the contents of the " register, aka the last yank/delete. ' - add the closing quote. Unquote a word that's enclosed in single quotes di'hPl2x di' - Delete the word enclosed by single quotes. hP - Move the cursor left one place (on top of the opening quote) and put the just deleted text before the quote. l - Move the cursor right one place (on top of the opening quote). 2x - Delete the two quotes. Change single quotes to double quotes va':s/\%V'\%V/"/g va' - Visually select the quoted word and the quotes. :s/ - Start a replacement. \%V'\%V - Only match single quotes that are within the visually selected region. /"/g - Replace them all with double quotes.

如果你使用vim插件https://github.com/tpope/vim-surround(或使用VSCode vim插件,它附带了预先安装的vim-surround),它非常方便!

add

ysiw' // surround in word `'`

下降

ds' // drop surround `'`

改变

cs'" // change surround from `'` to `"`

它甚至适用于html标签!

cst<em> // change surround from current tag to `<em>`

查看github上的自述文件以获得更好的示例

除了其他命令外,这将把一行中的所有单词用双引号括起来(根据您的注释)。

:s/\(\S\+\)/"\1"/

或者如果你想减少反斜杠的数量,你可以在模式的开头放一个\v(非常神奇的)修饰符

:s/\v(\S+)/"\1"/

用引号括住所有单词:

s/\(\w\+\)/"\1"/g

之前:

aaa,bbb,ccc

后:

"aaa","bbb","ccc"

这个怎么样?

 :%s/\'/"/g