config/nvim/lua/fzy/buffers.lua (view raw)
1local fn = vim.fn
2local cmd = vim.cmd
3local M = {}
4
5function M.fzy_buffers()
6 local buffers = {}
7
8 -- list of buffers
9 local nbuf = fn.range(1, fn.bufnr('$'))
10
11 -- filter out buffers that don't have 'buflisted' set
12 for _, n in ipairs(nbuf) do
13 if fn.buflisted(n) then
14 buffers[fn.bufname(n)] = n
15 end
16 end
17
18 -- write buffer names to file to feed to fzy
19 local buffile = fn.tempname()
20 local f = io.open(buffile, "a")
21 for b, _ in pairs(buffers) do
22 f:write(b, '\n')
23 end
24 f:close()
25
26 local fzy_cmd = {
27 'fzy -p "buf > " ',
28 '< ' .. buffile,
29 }
30
31 require('fzy/fzy').fzy_search(table.concat(fzy_cmd), function(stdout)
32 -- strip '\n'
33 local selected, _ = stdout:gsub('\n', '')
34 cmd('bd!')
35
36 cmd('buffer ' .. buffers[ selected ])
37 -- housekeeping
38 os.remove(buffile)
39 end
40 )
41
42end
43
44return M