Vim在N项中的变化可能吗?例如。 CI2"

时间:2014-10-06 09:52:40

标签: vim

我通常想要更改HTML字符串中的第二个(或第三个)属性:

<div class="Something" data-change="TOBECHANGED">Test</div>

目前,我正在做 f“然后; 然后 ci”。我意识到我可以搜索和替换,但这不是我感兴趣的。

然后发生了我应该能够做的事情,比如 c2i“(改变第二个)。这不起作用 - 这可能吗?如果是这样,那么正确的语法是什么?

1 个答案:

答案 0 :(得分:1)

使用内置命令,这需要两个步骤:首先找到引号(例如,通过3f"),然后选择它们(ci")。

但是你可以通过自定义文本对象进行单步操作。我有以下内容,您可以使用c2if"

" af{c}, if{c}      a / inner [count]'th next {c} text object in the current
"           line.
" aF{c}, iF{c}      a / inner [count]'th previous {c} text object in the
"           current line.
"           For example, "dif(" would go to the next "()" pair and
"           delete its contents.
" Source: Steve Losh, https://bitbucket.org/sjl/dotfiles/src/tip/vim/.vimrc
function! s:NextTextObject( scope, isBackward )
    let l:char = ingo#query#get#Char()
    if empty(l:char) | return | endif

    let l:save_cursor = getpos('.')
    let l:direction = (a:isBackward ? 'F' : 'f')
    " Special case for "tag" text object.
    let l:findChar = tr(l:char, 't', '>')

    let l:nextTextObject = l:direction . l:findChar . 'v' . a:scope . l:char

    " To handle [count], we can't just prepend it to the f / F command, as
    " depending on the text object, there can be two identical delimiters that
    " need to be skipped (e.g. in i", but not in i[). Instead, select each text
    " object in turn, and then repeat at the corresponding border.
    let l:count = v:count1
    while l:count > 1
        let l:cursor = getpos('.')
            execute 'normal!' l:nextTextObject . "\<Esc>" .
            \   (a:isBackward ? 'g`<' . (a:scope ==# 'i' ? "\<Left>" : '') : '')
        if l:cursor == getpos('.')
            call cursor(l:save_cursor[1:2])
            execute "normal! \<C-\>\<C-n>\<Esc>" | " Beep.
            return
        endif
        let l:count -= 1
    endwhile

    execute 'normal!' l:nextTextObject
    if mode() ==# 'n'
        call cursor(l:save_cursor[1:2])
        execute "normal! \<C-\>\<C-n>\<Esc>" | " Beep.
    endif
endfunction
onoremap <silent> af :<C-u>call <SID>NextTextObject('a', 0)<CR>
xnoremap <silent> af :<C-u>call <SID>NextTextObject('a', 0)<CR>
onoremap <silent> if :<C-u>call <SID>NextTextObject('i', 0)<CR>
xnoremap <silent> if :<C-u>call <SID>NextTextObject('i', 0)<CR>

onoremap <silent> aF :<C-u>call <SID>NextTextObject('a', 1)<CR>
xnoremap <silent> aF :<C-u>call <SID>NextTextObject('a', 1)<CR>
onoremap <silent> iF :<C-u>call <SID>NextTextObject('i', 1)<CR>
xnoremap <silent> iF :<C-u>call <SID>NextTextObject('i', 1)<CR>

注意:这需要我的ingo-library plugin