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


当前回答

如果你使用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上的自述文件以获得更好的示例

其他回答

对于VSCodeVim、Neovim和Macvim的用户,可以这样做

vwS"

你可以用你想用的任何东西来代替“”。 你可以用任何其他选择运算符替换w

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

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

VIM for vscode做得很好。如果你不使用vscode,它是基于一个vim-surround。

一些例子: "test",光标在引号内,类型为cs"',以'test'结束 "test",光标在引号内,类型ds",以test结束 "test",光标在引号内,输入cs"t,然后输入123>结束 < 123 >测试 用光标在单词上测试测试类型锯)结束于(测试)

用引号括住所有单词:

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

之前:

aaa,bbb,ccc

后:

"aaa","bbb","ccc"

我写了一个这样的脚本:

function! WrapSelect (front)
    "puts characters around the selected text.
    let l:front = a:front
    if (a:front == '[')
        let l:back = ']'
    elseif (a:front == '(')
        let l:back = ')'
    elseif (a:front == '{')
        let l:back = '}'
    elseif (a:front == '<')
        let l:back = '>'
    elseif (a:front =~ " ")
        let l:split = split(a:front)
        let l:back = l:split[1]
        let l:front = l:split[0]
    else
        let l:back = a:front
    endif
    "execute: concat all these strings. '.' means "concat without spaces"
    "norm means "run in normal mode and also be able to use \<C-x> characters"
    "gv means "get the previous visual selection back up"
    "c means "cut visual selection and go to insert mode"
    "\<C-R> means "insert the contents of a register. in this case, the
    "default register"
    execute 'norm! gvc' . l:front. "\<C-R>\""  . l:back
endfunction
vnoremap <C-l> :<C-u>call WrapSelect(input('Wrapping? Give both (space separated) or just the first one: '))<cr>

要使用,只需要突出显示一些东西,按ctrl l,然后键入一个字符。如果它是函数所知道的字符之一,它将提供正确的结束字符。如果不是,它将在两边插入相同的字符。

环绕。Vim可以做的不仅仅是这些,但是这已经足够满足我的需求了。