cmp-fuzzy-path

nvim-cmp source for filesystem paths, employing fd and regular expressions to find files.

Depends on fuzzy.nvim (which depends either on fzf or on fzy).

To facilitate fuzzy matching, when cmp-fuzzy-path tries to find a path the path is first transformed to a regular expression like this: p/t/f --> p.*/.*t.*/.*f.', which will match path/to/file and also pa/toooo/other_file.

Installation

Using Packer with fzy:

use "hrsh7th/nvim-cmp"
use {'abdallahz3/cmp-fuzzy-path'}

You should have fd and skim in your PATH, or edit the configuation to point at the exact location.

Setup

require'cmp'.setup {
  sources = cmp.config.sources({
    { name = 'fuzzy_path'},
  })
}

This plugin can also be used to complete file names for :edit or :write in cmdline mode of cmp:

cmp.setup.cmdline(':', {
  sources = cmp.config.sources({
    { name = 'fuzzy_path' }
  })
})

Note: the plugin's name is fuzzy_path in cmp's config.

Configuration

Configuration can be passed when configuring cmp:

cmp.setup.cmdline(':', {
  sources = cmp.config.sources({
    { name = 'fuzzy_path', options = {fd_timeout_msec = 1500} }
  })
})

fd_timeout_msec (type: int)

Default: 500

How much grace to give the file finder before killing it. If you set this to too short a value, you will probably not get enough suggestions.

fd_cmd (type: table(string))

Default: {'fd', '-d', '20', '-p'}

The commend to use as a file finder. Note that -p is needed so we match on the entire path, not just on the file or directory name.

Sorting

cmp-fuzzy-path adds a score entry to each completion item's data field, which can be used to override cmp's default sorting order:

local compare = require('cmp.config.compare')
cmp.setup({
  sorting = {
    priority_weight = 2,
    comparators = {
      require('cmp_fuzzy_path.compare'),
      compare.offset,
      compare.exact,
      compare.score,
      compare.recently_used,
      compare.kind,
      compare.sort_text,
      compare.length,
      compare.order,
    },
  },
}