移动到角色并在VIM中进入插入模式

时间:2015-01-08 12:28:26

标签: vim

我最近爱上了vim中的fFtT命令。现在我发现自己经常想要在一个可以使用其中一个命令轻松导航到的位置插入一些东西,至少经常足以让我想通过{{1}使整个动作“找到字符和插入文本”可重复}。目前,我可以重复插入操作,但是我必须在我想重复插入的每一行重新键入查找字符移动。

是否有一个命令将这些移动命令的动作与放入插入模式的动作结合起来?或者,如果没有,是否可以在我的.中定义这样的命令?

1 个答案:

答案 0 :(得分:1)

首先,你可以通过;重复最后的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)")