diff --git a/lua/custom/fzf_lua.lua b/lua/custom/fzf_lua.lua new file mode 100644 index 0000000..9bbfc0a --- /dev/null +++ b/lua/custom/fzf_lua.lua @@ -0,0 +1,129 @@ +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 = { + [""] = "down", + [""] = "up", + [""] = "toggle+up", + [""] = "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 + +vim.api.nvim_create_user_command('FzfLuaGrepGlob', live_grep_with_glob, {}) + +local keymap = vim.keymap.set + +keymap("n", "lf", function() fzf_lua.files() end, { desc = "FZF: find files in cwd" }) +keymap("n", "lg", function() fzf_lua.live_grep() end, { desc = "FZF: find string in cwd" }) +keymap("n", "ls", function() fzf_lua.grep_cword() end, { desc = "FZF: find string under cursor" }) +keymap("n", "lb", function() fzf_lua.buffers() end, { desc = "FZF: display opened buffers" }) +keymap("n", "lh", function() fzf_lua.help_tags() end, { desc = "FZF: display help tags" }) + +-- Your custom commands +keymap('n', 'lp', live_grep_with_glob, { desc = 'Live grep with glob pattern' }) +keymap('n', 'lcp', live_grep_in_current_dir, { desc = 'Live grep in current directory' }) +-- keymap('n', 'ccp', copy_file_path_and_position, { desc = "Copy cursor position" }) +-- keymap('n', 'ccd', copy_current_file_path, { desc = 'Copy current file path' }) +keymap('n', 'lq', live_grep_in_quick_fix_list_files, { desc = "Live grep in quickfix files" }) + +keymap('n', 'lcf', 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", "lml", function() fzf_lua.marks() end, { desc = "FZF: find marks list" }) +keymap("n", "lmp", function() fzf_lua.man_pages() end, { desc = "FZF: find available man pages" }) +keymap("n", "lds", function() fzf_lua.lsp_document_symbols() end, { desc = "Grep: Document symbols" }) + +keymap("n", "lt", function() + fzf_lua.grep({ + prompt = 'Todo❯ ', + search = 'TODO|FIXME|XXX|HACK|BUG|NOTE|PERF|OPTIMIZE', + no_esc = true, + }) +end, { desc = "FZF: display TODO comments" }) diff --git a/lua/custom/plugins/fzf_lua.lua b/lua/custom/plugins/fzf_lua.lua new file mode 100644 index 0000000..e363d16 --- /dev/null +++ b/lua/custom/plugins/fzf_lua.lua @@ -0,0 +1,9 @@ +return { + "ibhagwan/fzf-lua", + dependencies = { "nvim-tree/nvim-web-devicons" }, + opts = {}, + config = function() + require "custom.fzf_lua" + end + +}