summaryrefslogtreecommitdiffstats
path: root/plugin/50-fzf.lua
blob: debe428e2c468f21a018f4ed0c6f50cd3b98b5f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
--
-- 50-fzf.lua
--
-- * Configures fzf.
--
-- Keymaps:
--   `<Leader>ff`: File picker
--   `<Leader>fg`: Grep picker
--   `<Leader>fb`: Buffer picker
--   `<Leader>fh`: Help tag picker
--   `<Leader>ft`: Tag picker
--   `<Leader>fr`: Resume last picker

-- Avoid conflicts with the already existing fzf configuration at ~/.config/fzf/fzf.bash
vim.env.FZF_DEFAULT_OPTS = nil

local fzf = require("fzf-lua")

-- Copy selected entries' paths to the clipboard (icon-stripped via entry_to_file).
local function copy_to_clipboard(selected, opts)
	local entry_to_file = require("fzf-lua.path").entry_to_file
	vim.fn.setreg(
		"+",
		table.concat(
			vim.tbl_map(function(sel)
				return entry_to_file(sel, opts).path
			end, selected),
			"\n"
		)
	)
end

fzf.setup({
	winopts = {
		preview = {
			layout = "flex",
			flip_columns = 120,
			vertical = "down:60%",
		},
	},
	keymap = {
		fzf = {
			true, -- inherit defaults
			["tab"] = "down",
			["btab"] = "up",
			["ctrl-y"] = "toggle",
		},
		builtin = {
			true, -- inherit defaults
			["<M-/>"] = "toggle-preview",
			["<C-n>"] = "preview-half-page-down",
			["<C-p>"] = "preview-half-page-up",
		},
	},
	actions = {
		files = { true, ["alt-y"] = copy_to_clipboard },
	},
	-- Sticky footer of keybinding hints, like --footer in ~/.config/fzf/fzf.bash.
	fzf_opts = {
		["--footer"] = table.concat({
			"Alt+Y      copy to clipboard",
			"Ctrl+Y     toggle selection",
			"Alt+/      show/hide preview",
		}, "\n"),
	},
})

----------------------------------------------------------------------------------------------------

vim.keymap.set("n", "<Leader>ff", fzf.files, { desc = "Fuzzy-find files" })
vim.keymap.set("n", "<Leader>fg", fzf.live_grep, { desc = "Fuzzy-find files content" })
vim.keymap.set("n", "<Leader>fb", fzf.buffers, { desc = "Fuzzy-find buffers" })
vim.keymap.set("n", "<Leader>fh", fzf.helptags, { desc = "Fuzzy-find help tags" })
vim.keymap.set("n", "<Leader>ft", fzf.tags, { desc = "Fuzzy-find tags" })
vim.keymap.set("n", "<Leader>fr", fzf.resume, { desc = "Resume last fuzzy-find picker" })