1

我正在尝试使用fzf在 vi​​mscript 中创建目录搜索和替换功能。我阻止的地方是尝试使用Alt-afzf 绑定来选择整个列表时。我什至不确定这是否可能,因为 fzf 是一个外部进程,但我可能错了。

这是我当前的功能。

function! CWDSearchAndReplace()

    " Try to get word under cursor else prompt user for a word
    let wordToReplace = expand("<cword>")
    if wordToReplace == ""
        call inputsave()
        let wordToReplace = input("Replace: ")
        call inputrestore()
    endif

    " Prompt for replacement
    call inputsave()
    let replacement = input("Replace \"" . wordToReplace . "\" with: ")
    call inputrestore()
    execute "Ag " . wordToReplace

    " =====>  Here I'd like to execute Alt-a followed by <CR>

    execute "cdo %s//" . replacement . "/gc"
end function

谢谢你的帮助!

4

1 回答 1

2

正如fzf.vim的创建者所指出的,这里没有必要使用,可以简单地使用.fzfag

function! s:ag_to_qf(line)
  let parts = split(a:line, ':')
  echo parts
  return {'filename': parts[0], 'lnum': parts[1], 'col': parts[2],
        \ 'text': join(parts[3:], ':')}
endfunction

function! AgToQF(query)
  call setqflist(map(systemlist('ag --column '.a:query), 's:ag_to_qf(v:val)'))
endfunction

function! CWDSearchAndReplace()
    let wordUnderCursor = expand("<cword>")
    call inputsave()
    let wordToReplace = input("Replace : ", wordUnderCursor)
    call inputrestore()
    call inputsave()
    let replacement = input("Replace \"" . wordUnderCursor . "\" with: ")
    call inputrestore()
    call AgToQF(wordUnderCursor)
    execute "cdo %s/" . wordToReplace . "/" . replacement ."/gc"
endfunction

nnoremap <leader>r :call FileSearchAndReplace()<CR>
于 2016-07-26T17:27:58.817 回答