150 lines
4.4 KiB
Lua
150 lines
4.4 KiB
Lua
local fzf_lua = require("fzf-lua")
|
||
|
||
fzf_lua.setup({
|
||
winopts = {
|
||
height = 0.85,
|
||
width = 0.80,
|
||
},
|
||
fzf_opts = {
|
||
['--layout'] = 'reverse-list',
|
||
},
|
||
files = {
|
||
fd_opts = "--color=never --type f",
|
||
rg_opts = "--color=never --files",
|
||
},
|
||
keymap = {
|
||
builtin = {
|
||
["<C-j>"] = "down",
|
||
["<C-k>"] = "up",
|
||
["<S-tab>"] = "toggle+up",
|
||
["<tab>"] = "toggle+down",
|
||
},
|
||
},
|
||
})
|
||
|
||
-- Glob pattern searcher
|
||
local function live_grep_with_glob()
|
||
vim.ui.input({
|
||
prompt = "Glob pattern: ",
|
||
default = "/home/user/Work/root/ivideon/audio_storage/",
|
||
}, function(input)
|
||
if input then
|
||
fzf_lua.live_grep({
|
||
cwd = input,
|
||
})
|
||
end
|
||
end)
|
||
end
|
||
|
||
local function live_grep_in_current_dir()
|
||
local current_file = vim.fn.expand('%:p')
|
||
local current_dir = current_file:match("(.*/)")
|
||
|
||
vim.ui.input({
|
||
prompt = "Glob pattern: ",
|
||
default = current_dir or vim.fn.getcwd(),
|
||
}, function(input)
|
||
if input then
|
||
fzf_lua.live_grep({
|
||
cwd = input,
|
||
})
|
||
end
|
||
end)
|
||
end
|
||
|
||
local function copy_current_file_path()
|
||
local filepath = vim.fn.expand('%:p')
|
||
vim.fn.setreg("+", filepath)
|
||
print("Copied: " .. filepath)
|
||
end
|
||
|
||
local function copy_file_path_and_position()
|
||
local filepath = vim.fn.expand('%:p')
|
||
local cursor_pos = vim.api.nvim_win_get_cursor(0)
|
||
local line = cursor_pos[1]
|
||
local col = cursor_pos[2] + 1 -- fzf-lua uses 1-indexed columns
|
||
|
||
local output = string.format("%s:%d:%d", filepath, line, col)
|
||
vim.fn.setreg("+", output)
|
||
print("Copied: " .. output)
|
||
end
|
||
|
||
local function live_grep_in_quick_fix_list_files()
|
||
local qf_items = vim.fn.getqflist()
|
||
local files = {}
|
||
|
||
for _, item in ipairs(qf_items) do
|
||
if item.bufnr > 0 then
|
||
local filename = vim.api.nvim_buf_get_name(item.bufnr)
|
||
if filename ~= "" then
|
||
table.insert(files, filename)
|
||
end
|
||
end
|
||
end
|
||
|
||
if #files > 0 then
|
||
fzf_lua.live_grep({
|
||
search_dirs = files,
|
||
})
|
||
else
|
||
print("No files in quickfix list")
|
||
end
|
||
end
|
||
|
||
local function find_all_files_including_hidden()
|
||
fzf_lua.files(
|
||
{
|
||
fd_opts = "--type f --hidden --no-ignore"
|
||
}
|
||
)
|
||
end
|
||
|
||
-- Function for getting filename and copy it to + buffer
|
||
local function copy_filename()
|
||
local filepath = vim.fn.expand('%:t')
|
||
local output = string.format("%s", filepath)
|
||
vim.fn.setreg("+", output)
|
||
print("Copied: " .. output)
|
||
end
|
||
|
||
vim.api.nvim_create_user_command('FzfLuaGrepGlob', live_grep_with_glob, {})
|
||
|
||
local keymap = vim.keymap.set
|
||
|
||
|
||
keymap("n", "<leader>ff", function() fzf_lua.files() end, { desc = "FZF: find files in cwd" })
|
||
keymap("n", "<leader>fg", function() fzf_lua.live_grep() end, { desc = "FZF: find string in cwd" })
|
||
keymap("n", "<leader>fs", function() fzf_lua.grep_cword() end, { desc = "FZF: find string under cursor" })
|
||
keymap("n", "<leader>fb", function() fzf_lua.buffers() end, { desc = "FZF: display opened buffers" })
|
||
keymap("n", "<leader>fh", function() fzf_lua.help_tags() end, { desc = "FZF: display help tags" })
|
||
|
||
-- Your custom commands
|
||
keymap("n", "<leader>fa", find_all_files_including_hidden, { desc = "FZF: Find all files" })
|
||
keymap('n', '<leader>fp', live_grep_with_glob, { desc = 'Live grep with glob pattern' })
|
||
keymap('n', '<leader>fcp', live_grep_in_current_dir, { desc = 'Live grep in current directory' })
|
||
keymap('n', '<leader>ccp', copy_file_path_and_position, { desc = "Copy cursor position" })
|
||
keymap('n', '<leader>ccd', copy_current_file_path, { desc = 'Copy current file path' })
|
||
keymap('n', '<leader>ccf', copy_filename, { desc = 'Copy current file path' })
|
||
|
||
keymap('n', '<leader>fq', live_grep_in_quick_fix_list_files, { desc = "Live grep in quickfix files" })
|
||
|
||
keymap('n', '<leader>fcf', function()
|
||
fzf_lua.live_grep({
|
||
cwd = vim.fn.expand('%:p:h'),
|
||
search = '',
|
||
fzf_cli_args = '--no-sort',
|
||
})
|
||
end, { desc = "Live grep in current file" })
|
||
|
||
keymap("n", "<leader>fml", function() fzf_lua.marks() end, { desc = "FZF: find marks list" })
|
||
keymap("n", "<leader>fmp", function() fzf_lua.man_pages() end, { desc = "FZF: find available man pages" })
|
||
keymap("n", "<leader>fds", function() fzf_lua.lsp_document_symbols() end, { desc = "Grep: Document symbols" })
|
||
keymap("n", "<leader>ft", function() fzf_lua.tabs() end, { desc = "Pick opened tabs" })
|
||
|
||
keymap("n", "<leader>fn", function()
|
||
fzf_lua.grep({
|
||
prompt = 'Todo❯ ',
|
||
search = 'TODO|FIXME|XXX|HACK|BUG|NOTE|PERF|OPTIMIZE',
|
||
no_esc = true,
|
||
})
|
||
end, { desc = "FZF: display TODO comments" })
|