Install & Configure FzF in Neovim - PDE p.III

· 706 words · 4 minute read

Info

This is the third part of the personalized development environment series, every part depends heavily on the previous one

Fzf is a fuzzy file finder. It supports searching for files and searching for text in these files with a preview.

Installing ##

Warning

Requirements ####

  • I use bat for previewing file contents while searching. Install instructions here
  • For full text search install ripgrep. Install instructions here

To install fzf, add the following highlighted lines to your nvim/lua/plugins.lua:

 1-- i use vimplug to manage my plugins
 2-- this checks if vimplug is installed, if not install it
 3vim.cmd([[
 4if empty(glob('~/.config/nvim/autoload/plug.vim'))
 5  silent !curl -fLo ~/.config/nvim/autoload/plug.vim --create-dirs
 6    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
 7endif
 8]])
 9
10-- assign variable
11local Plug = vim.fn['plug#']
12
13-- in between this we call Plug to specify plugins
14vim.call('plug#begin', '~/.config/nvim/plugged')
15    -- fuzzy finder
16    Plug('junegunn/fzf', { ['do'] = vim.fn['fzf#install()'] })
17    Plug('junegunn/fzf.vim')
18
19    -- vscode extension provider
20    Plug 'neoclide/coc.nvim'
21
22    -- display buffers and tabs nicely
23    Plug 'akinsho/bufferline.nvim'
24
25
26    -- color theme / sheme
27    Plug('folke/tokyonight.nvim', { branch = 'main' })
28
29
30    -- comment helper
31    Plug 'tpope/vim-commentary'
32
33
34    -- status line
35    Plug 'nvim-lualine/lualine.nvim'
36
37
38    -- file explorer
39    Plug 'nvim-tree/nvim-tree.lua'
40
41
42    -- icons for everything, file explorer, tabs, statusline
43    Plug 'nvim-tree/nvim-web-devicons'
44
45
46    -- automatically add "([ pairs if first one is typed
47    Plug 'jiangmiao/auto-pairs'
48
49
50    -- startup interface
51    Plug 'mhinz/vim-startify'
52
53vim.call('plug#end')

As always save the file with :w and source it using :source %. Now install the plugins using :PlugInstall.

Configuring ##

Keybinds ###

The default keybindings i use for fzf and ripgrep are Leader+f and Leader+p.

To configure these keybinds add the following to your nvim/lua/keybinds.lua:

 1-- helper for mapping custom keybindings
 2-- source: https://gist.github.com/Jarmos-san/d46605cd3a795513526448f36e0db18e#file-example-keymap-lua
 3function map(mode, lhs, rhs, opts)
 4    local options = { noremap = true }
 5    if opts then
 6        options = vim.tbl_extend("force", options, opts)
 7    end
 8    vim.api.nvim_set_keymap(mode, lhs, rhs, options)
 9end
10
11-- taken from the coc.nvim example config:
12-- https://github.com/neoclide/coc.nvim
13function _G.show_docs()
14    local cw = vim.fn.expand('<cword>')
15    if vim.fn.index({'vim', 'help'}, vim.bo.filetype) >= 0 then
16        vim.api.nvim_command('h ' .. cw)
17    elseif vim.api.nvim_eval('coc#rpc#ready()') then
18        vim.fn.CocActionAsync('doHover')
19    else
20        vim.api.nvim_command('!' .. vim.o.keywordprg .. ' ' .. cw)
21    end
22end
23
24-- autocomplete
25function _G.check_back_space()
26    local col = vim.fn.col('.') - 1
27    return col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') ~= nil
28end
29
30
31-- view the definition of the currently hovering over element
32map("n", "gd", "<Plug>(coc-definition)", {silent = true})
33-- view a list of the references of the currently hovering over element
34map("n", "gr", "<Plug>(coc-references)", {silent = true})
35-- view documentation for the currently hovering over element
36map("n", "K", "<CMD>lua _G.show_docs()<CR>", {silent = true})
37
38-- toggle the nvim tree sidebar
39map("n", "<C-b>", ":NvimTreeToggle<CR>", {silent = true})
40
41-- on space f open fzf for files
42map("n", "<Leader>f", ":FZF<CR>", {silent = true})
43-- on space p open ripgrep for strings in files
44map("n", "<Leader>p", ":Rg<CR>", {silent = true})
45
46-- move visual selection down
47map("v", "J", ":m '>+1<CR>gv=gv")
48-- move visual selection up
49map("v", "K", ":m '<-2<CR>gv=gv")
50
51-- use Tab to trigger completion for the currently selected completion
52local opts = {silent = true, noremap = true, expr = true, replace_keycodes = false}
53map("i", "<TAB>", 'coc#pum#visible() ? coc#pum#confirm() : v:lua.check_back_space() ? "<TAB>" : coc#refresh()', opts)

Cosmetic ###

We will now configure fzf and ripgrep to show a preview and use a bigger window. To apply this config add the following highlighted lines to your nvim/lua/plugin-options.lua:

 1-- set colorsheme
 2vim.cmd([[colorscheme tokyonight-night]])
 3
 4-- define fzf config, window size, preview using bat, etc
 5vim.cmd([[let g:fzf_layout = {'window': {'width': 0.9, 'height': 0.9}}]])
 6vim.cmd([[let $FZF_DEFAULT_OPTS="--preview 'bat --color=always {}'"]])
 7
 8-- bufferline config
 9require("bufferline").setup{
10    options = {
11        -- only display tabs, hide buffers
12        mode = "tabs",
13
14        -- style for kitty terminal
15        separator_style = "slant",
16
17        -- display coc diagnostics
18        diagnostics = "coc"
19    }
20}
21
22-- nvim tree setup
23require("nvim-tree").setup()
24
25-- lualine setup
26require("lualine").setup{
27    options = {
28        theme = "palenight"
29    }
30}
31
32-- registering :Prettier command
33vim.cmd([[command! -nargs=0 Prettier :CocCommand prettier.forceFormatDocument]])

Screenshots: ##

fzf:

fzf showcase

ripgrep:

ripgrep showcase