Home

Awesome

fuzzy.nvim

Fast, Simple, Powerfull fuzzy finder all in lua.

Why another fuzzy finder ?

When I started this project the alternatives were fzf.vim which was in vimscript but I needed my fuzzy finder to be completely in Lua, so i started this. Demo

Installation

Packer.nvim

use { 'kyazdani42/nvim-web-devicons' } --Optional if you want icons, also you need to have a patched font, look at nvim-web-devicons README for information.
use { 'amirrezaask/fuzzy.nvim', requires={'nvim-lua/plenary.nvim'}}

Terminology:

Source:

source is either a function that returns a list of data that we are going to search or is simply just a lua table or a string which is a command that it's output will be used as a source.

Sorter

Sorter is a function that gets our query and sorts the source data based on that.

Handler

Handler is a function that gets called with the selected_line when you hit <CR> in the fuzzy window.

Mappings

You can attach any mappings you want with mappings key in fuzzy.new like below

require('fuzzy').new {
    source = ...,
    sorter = ...,
    mappings = {
        ['<CR>'] = function()
            local selected_line = CURRENT_FUZZY:get_output()
            -- do anything you want
        end
    }
}

Customization

Settings

require'fuzzy'.setup {
  width = 60,
  height = 40,
  blacklist = {
    "vendor"
  },
  border = 'yes' -- can be 'no' as well
  location = loc.bottom_center,
  sorter = require'fuzzy.lib.sorter'.fzy -- Also fzf_native, fzy_native, string_distance are supported
  prompt = '> ',
  register = {
    some_custom_function = function() -- This function appears in complete menu when using :Fuzzy command.
    end
  }
}

Custom Fuzzy usage

let's say you want to define a simple fuzzy search on a simple lua table.

  require'fuzzy'.new {
    source = {
      'data1', ...
    },
    handler = function(data)
      print(data)
    end,
    -- Almost all the keys that are supported in setup function are valid here and will override the default one set by user.
  }

Extending Fuzzy

You can extend fuzzy.nvim with new features since almost all properties of fuzzy can be changed or even adding new ones, forexample a preview window using fuzzy.lib tools that you can open with a keymapping.

require('fuzzy').setup {
    ... other keys
    mappings = {
        ['P'] = function preview()
          local parser = function(line)
              local parts = vim.split(line, ':')
              return remove_icon(parts[1]), parts[2]
            end
          local preview_window = function (line)
            local floating_buffer = require('fuzzy.lib.floating').floating_buffer
            local buf, win, _ = floating_buffer {
              height = 50,
              width = 50,
              location = loc.center,
              border = 'no',
            }
            local file, lnum = parser(line)
            require('fuzzy.lib.helpers').open_file_at(file, lnum)
            vim.cmd([[ call feedkeys("\<C-c>") ]])
            vim.api.nvim_buf_set_option(0, 'modifiable', false)
          end
          local line = require('fuzzy').CurrentFuzzy():get_output()
          preview_window(line)
        end
    }
}

Configuration

Fuzzy.nvim uses a 3 layered approach for configuration, based on priority:

  1. opts table passed in calling fuzzy.new or any fuzzy function like fuzzy.find_files
  2. global user configuration set in fuzzy.setup
  3. fuzzy defaults

Builtin functions

Credits