如何在Vim中快速引用/取消引用单词并更改引用(例如从“到”)?我知道环绕的事。vim插件,但我想只使用vim。
当前回答
VIM for vscode做得很好。如果你不使用vscode,它是基于一个vim-surround。
一些例子: "test",光标在引号内,类型为cs"',以'test'结束 "test",光标在引号内,类型ds",以test结束 "test",光标在引号内,输入cs"t,然后输入123>结束 < 123 >测试 用光标在单词上测试测试类型锯)结束于(测试)
其他回答
引用一个词,使用单引号 被'Ctrl + r”
这样做对我来说比较容易
ciw '' Esc P
环绕。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 for vscode做得很好。如果你不使用vscode,它是基于一个vim-surround。
一些例子: "test",光标在引号内,类型为cs"',以'test'结束 "test",光标在引号内,类型ds",以test结束 "test",光标在引号内,输入cs"t,然后输入123>结束 < 123 >测试 用光标在单词上测试测试类型锯)结束于(测试)
这里有一些简单的映射,可以用来引用和取消引用一个单词:
" 'quote' a word
nnoremap qw :silent! normal mpea'<Esc>bi'<Esc>`pl
" double "quote" a word
nnoremap qd :silent! normal mpea"<Esc>bi"<Esc>`pl
" remove quotes from a word
nnoremap wq :silent! normal mpeld bhd `ph<CR>
宏观的方式!
按q和q记录到q寄存器(我们用“q”作为快捷键来记住“引号”)。 按shift + b移动光标到当前单词的前面 按I输入'(单引号) 按esc键,然后按e键移动到单词的末尾 按a,然后再按',用引号包围单词。 按esc键进入正常模式。 最后按q将其记录到q寄存器中。
如何使用
移动光标到所需的单词。 按@q将单词用引号括起来。 如果你想把它重复成另一个单词,请按@@。
你可以修改步骤4任何你喜欢的{一行,一个词,直到找到一些字符,等等}。
使记录的宏持久
打开. vimrc 转到文件末尾 切换到插入模式。输入这个使它持久:
let @q='ctrl + r ctrl + r q'
保存并退出 打开你的文件,翻到一些单词 现在按@q
如果你做得正确,神奇的东西应该出现在你的话语中。
您可以将此应用于您喜欢的其他宏。