How can I use Telescope.nvim to complete a path in insert mode?

1.9k Views Asked by At

Fzf.vim implements a fuzzy finder in insert mode, extending the native ins-completion functionality.

For example: in insert mode, we can map <c-x><c-f> to enable Fzf.vim to fuzzy find and insert file names with relative paths (fzf-complete-path). The same functionalities are implemented for words (fzf-complete-word) and lines (fzf-complete-line) completions.

Here are the Fzf.vim mapping example of these functions:

" 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)

How can I set the same behavior with Telescope.nvim?

1

There are 1 best solutions below

0
On

You can define a custom action and map it to a key to execute it in Telescope.

I've written a set of actions for this task (to insert file paths in the current buffer): telescope-insert-path.nvim

You just need to make your custom mappings in the require'telescope'.setup().

local path_actions = require('telescope_insert_path')

require('telescope').setup {
  defaults = {
    mappings = {
      n = {
        ["pi"] = path_actions.insert_relpath_i_insert,
        ["pI"] = path_actions.insert_relpath_I_insert,
        ["pa"] = path_actions.insert_relpath_a_insert,
        ["pA"] = path_actions.insert_relpath_A_insert,
        ["po"] = path_actions.insert_relpath_o_insert,
        ["pO"] = path_actions.insert_relpath_O_insert,
        ["Pi"] = path_actions.insert_abspath_i_insert,
        ["PI"] = path_actions.insert_abspath_I_insert,
        ["Pa"] = path_actions.insert_abspath_a_insert,
        ["PA"] = path_actions.insert_abspath_A_insert,
        ["Po"] = path_actions.insert_abspath_o_insert,
        ["PO"] = path_actions.insert_abspath_O_insert,
        ["<leader>pi"] = path_actions.insert_relpath_i_visual,
        ["<leader>pI"] = path_actions.insert_relpath_I_visual,
        ["<leader>pa"] = path_actions.insert_relpath_a_visual,
        ["<leader>pA"] = path_actions.insert_relpath_A_visual,
        ["<leader>po"] = path_actions.insert_relpath_o_visual,
        ["<leader>pO"] = path_actions.insert_relpath_O_visual,
        ["<leader>Pi"] = path_actions.insert_abspath_i_visual,
        ["<leader>PI"] = path_actions.insert_abspath_I_visual,
        ["<leader>Pa"] = path_actions.insert_abspath_a_visual,
        ["<leader>PA"] = path_actions.insert_abspath_A_visual,
        ["<leader>Po"] = path_actions.insert_abspath_o_visual,
        ["<leader>PO"] = path_actions.insert_abspath_O_visual,
        -- Additionally, there's normal mode mappings for the same actions:
        -- ["<leader><leader>pi"] = path_actions.insert_relpath_i_normal,
        -- ...
      }
    }
  }
}