移动到字符并进入 VIM 中的插入模式

Move to character and drop into insert mode in VIM

我最近爱上了 fFtT 命令 vim。现在我发现自己经常想在一个可以使用这些命令之一轻松导航到的位置插入一些东西,至少足够频繁以至于我想让整个动作 "find character and insert text" 可以通过 . 重复。目前,我可以重复插入操作,但我必须在要重复插入的每一行重新键入查找字符移动。

是否有命令将这些移动命令的动作与进入插入模式的动作结合起来?或者,如果没有,是否可以在我的 .vimrc?

中定义这样的命令

首先,你可以通过;重复最后的f/t/F/T动作(通过,反转),所以你可以用两个键重复:;.


如果这还不够好,repeat.vim 插件可用于构建自定义映射,就像内置命令一样重复:

"<Leader>it{char}       Insert text before the [count]'th occurrence of {char}
"                       to the right.
"<Leader>if{char}       Insert text after the [count]'th occurrence of {char}
"                       to the right.
"                       These mappings can be repeated atomically, this is
"                       faster than ";."
function! s:InsertAtCharPrepare( motion, moveOffMotion, repeatMapping )
    augroup InsertAtChar
        autocmd!
        " Enter insert mode automatically after the f/t motion.
        " XXX: :startinsert doesn't work on the first movement somehow, use
        " feedkeys() instead.
        autocmd CursorMoved <buffer> call feedkeys('a', 'n')

        " Prime repeat.vim after insertion is done.
        execute printf('autocmd InsertLeave <buffer> %scall repeat#set(%s, %d) | autocmd! InsertAtChar',
        \   (v:count1 <= 1 || empty(a:moveOffMotion) ? '' : 'execute "normal!" ' . string(a:moveOffMotion) . '|'),
        \   string(a:repeatMapping),
        \   v:count1
        \)

        " Abort in case something unexpected happens.
        autocmd WinLeave,BufLeave <buffer> autocmd! InsertAtChar
    augroup END

    return a:motion
endfunction
function! s:InsertAtCharRepeat( moveOffMotion, repeatMapping )
    let l:count = v:count1  " Save the original count to pass this on to repeat.vim.
    execute 'normal!' l:count . ';.' . (l:count <= 1 ? '' : a:moveOffMotion)
    call repeat#set(a:repeatMapping, l:count)
endfunction
" With "t" and [count] > 1, we need to move off from before {char} (where we're
" left when leaving insert mode) onto {char}, so that a repeat will happen
" before the next occurrence, not on the same again.
nnoremap <silent> <Plug>(InsertUntilCharRepeat) :<C-u>call <SID>InsertAtCharRepeat('l', "\<lt>Plug>(InsertUntilCharRepeat)")<CR>
nnoremap <silent> <Plug>(InsertFromCharRepeat)  :<C-u>call <SID>InsertAtCharRepeat('', "\<lt>Plug>(InsertFromCharRepeat)")<CR>
nnoremap <expr> <Leader>it <SID>InsertAtCharPrepare('t', 'l', "\<lt>Plug>(InsertUntilCharRepeat)")
nnoremap <expr> <Leader>if <SID>InsertAtCharPrepare('f', '',  "\<lt>Plug>(InsertFromCharRepeat)")