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


当前回答

我使用的是vim-surround命令,本质上几乎是vim。

环绕。vim都是关于“环境”的:圆括号、括号、引号、XML标记等等。该插件提供了映射,可以轻松地成对删除、更改和添加这样的环境。

Plugin 'tpope/vim-surround' 

例如,要完全删除分隔符,请按ds”。 详情如下: https://github.com/tpope/vim-surround

其他回答

我写了一个这样的脚本:

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可以做的不仅仅是这些,但是这已经足够满足我的需求了。

添加引用

我开始在我的.vimrc中使用这个快速而肮脏的函数:

vnoremap q <esc>:call QuickWrap("'")<cr>
vnoremap Q <esc>:call QuickWrap('"')<cr>

function! QuickWrap(wrapper)
  let l:w = a:wrapper
  let l:inside_or_around = (&selection == 'exclusive') ? ('i') : ('a')
  normal `>
  execute "normal " . inside_or_around . escape(w, '\')
  normal `<
  execute "normal i" . escape(w, '\')
  normal `<
endfunction

所以现在,我视觉上选择任何我想要的(通常通过view - visual select inside word)在引号中,并按Q为双引号,或按Q为单引号。

删除引号

vnoremap s <esc>:call StripWrap()<cr>

function! StripWrap()
  normal `>x`<x
endfunction

我使用vim-textobj-quotes,以便vim将引号视为文本对象。这意味着我可以做vaq(视觉选择周围的引号。这将查找最近的引号并可视化地选择它们。(这是可选的,你可以只做f"vww)。然后按s从选择中删除引号。

改变报价

吻。我先删除引号,然后再添加引号。 例如,要用双引号替换单引号,我将执行以下步骤: 1. 删除单引号:vaqs, 2。添加新的引用:vwQ。


这里的http://vim.wikia.com/wiki/Wrap_a_visual_selection_in_an_HTML_tag代码被修改并用作我的回答的一部分 https://github.com/beloglazov/vim-textobj-quotes vim-text-obj-quotes

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

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

用引号括住所有单词:

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

之前:

aaa,bbb,ccc

后:

"aaa","bbb","ccc"

环绕。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.