You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Telescope.nvim实现与Fzf.vim同款的插入模式补全?

用Telescope.nvim实现Fzf.vim式的插入模式补全

Fzf.vim 实现了插入模式下的模糊查找补全功能,扩展了Vim原生的ins-completion能力。比如在插入模式下,可通过<c-x><c-f>调用模糊查找并插入带相对路径的文件名(对应fzf-complete-path),同时还支持单词(fzf-complete-word)和整行(fzf-complete-line)的补全,其映射示例如下:

" Insert mode completion
imap <c-x><c-k> <plug>(fzf-complete-word)
imap <c-x><c-f> <plug>(fzf-complete-path)
imap <c-x><c-l> <plug>(fzf-complete-line)

下面是用Telescope.nvim实现相同插入模式补全行为的配置方案:


核心配置方案

Telescope没有预定义的类似Fzf.vim的补全插件映射,但可以通过其内置picker和自定义映射、选中行为来实现完全一致的效果。以下是针对三个补全场景的具体配置(基于Neovim Lua配置):

1. 文件名补全(对应fzf-complete-path)

调用Telescope的find_files picker,自定义选中后将路径插入光标位置:

vim.keymap.set('i', '<c-x><c-f>', function()
  require('telescope.builtin').find_files({
    attach_mappings = function(_, map)
      map('i', '<cr>', function(prompt_bufnr)
        local selected = require('telescope.actions.state').get_selected_entry()
        require('telescope.actions').close(prompt_bufnr)
        vim.api.nvim_put({selected.value}, 'c', false, true)
      end)
      return true
    end
  })
end, { desc = "插入模式下用Telescope补全文件路径" })

如果是Git仓库,可将find_files替换为git_files,仅搜索仓库内的文件。

2. 单词补全(对应fzf-complete-word)

基于当前缓冲区内容模糊匹配单词,选中后插入目标单词:

vim.keymap.set('i', '<c-x><c-k>', function()
  require('telescope.builtin').current_buffer_fuzzy_find({
    prompt_title = "缓冲区单词补全",
    word_match = '-w', -- 仅匹配完整单词
    attach_mappings = function(_, map)
      map('i', '<cr>', function(prompt_bufnr)
        local selected = require('telescope.actions.state').get_selected_entry()
        require('telescope.actions').close(prompt_bufnr)
        -- 从选中行中提取单词
        local target_word = vim.fn.matchstr(selected.value, [[\k\+]])
        vim.api.nvim_put({target_word}, 'c', false, true)
      end)
      return true
    end
  })
end, { desc = "插入模式下用Telescope补全单词" })

3. 整行补全(对应fzf-complete-line)

模糊匹配当前缓冲区的行内容,选中后插入整行:

vim.keymap.set('i', '<c-x><c-l>', function()
  require('telescope.builtin').current_buffer_fuzzy_find({
    prompt_title = "缓冲区行补全",
    attach_mappings = function(_, map)
      map('i', '<cr>', function(prompt_bufnr)
        local selected = require('telescope.actions.state').get_selected_entry()
        require('telescope.actions').close(prompt_bufnr)
        vim.api.nvim_put({selected.value}, 'c', false, true)
      end)
      return true
    end
  })
end, { desc = "插入模式下用Telescope补全行内容" })

Vimscript配置兼容

如果你仍在使用Vimscript配置,可以将上述Lua代码包裹在lua << EOF块中:

" 插入模式文件路径补全
imap <c-x><c-f> <cmd>lua << EOF
require('telescope.builtin').find_files({
  attach_mappings = function(_, map)
    map('i', '<cr>', function(prompt_bufnr)
      local selected = require('telescope.actions.state').get_selected_entry()
      require('telescope.actions').close(prompt_bufnr)
      vim.api.nvim_put({selected.value}, 'c', false, true)
    end)
    return true
  end
})
EOF<cr>

" 其余两个补全的Vimscript写法类似,只需替换对应的picker和映射键位

额外优化

如果想要更接近Fzf的模糊查找体验,可以安装telescope-fzy-native.nvim扩展,启用后能提升模糊匹配的性能和精准度。


内容的提问来源于stack exchange,提问作者Gustavo Basso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 18:48:38