From 22def9e8ad936ec0397de566f9abe2f873a297f3 Mon Sep 17 00:00:00 2001 From: t0xa Date: Sat, 1 Nov 2025 13:58:26 +0300 Subject: [PATCH] Telescope: Add bindings for postitions in file --- lua/custom/telescope.lua | 56 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/lua/custom/telescope.lua b/lua/custom/telescope.lua index 8e61d80..74b5f74 100644 --- a/lua/custom/telescope.lua +++ b/lua/custom/telescope.lua @@ -1,5 +1,14 @@ require("telescope").setup { defaults = { + vimgrep_arguments = { + "rg", + "--color=never", + "--no-heading", + "--with-filename", + "--line-number", + "--column", + "--smart-case", + }, path_display = { shorten = 4 }, }, } @@ -11,7 +20,7 @@ pcall(require("telescope").load_extension, "fzf") local function live_grep_with_glob() vim.ui.input({ prompt = "Glob pattern: ", - default = "/home/user/Work/root/ivideon/audio_saver", -- Optional default value + default = "/home/user/Work/root/ivideon/audio_storage/", -- Optional default value }, function(input) if input then require('telescope.builtin').live_grep({ @@ -22,6 +31,46 @@ local function live_grep_with_glob() end) end +local function live_grep_in_current_dir() + -- Get currentp path + local search_dirs = { vim.fn.expand('%:p') } + -- Extract path from table + local current_dir = search_dirs[1] + -- Get path, cutting last entry (current file) + local result = current_dir:match(".*/") + vim.ui.input({ + prompt = "Glob pattern: ", + default = result, -- Optional default value + }, function(input) + if input then + require('telescope.builtin').live_grep({ + search_dirs = { input } + }) + end + end) +end + +-- Function for copying current file path +local function copy_current_file_path() + local filepath = vim.fn.expand('%:p') + vim.fn.setreg("+", filepath) + print("Copied: " .. filepath) +end + +-- Function for gettings snapshot of current cursor position +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] + + local output = string.format("%s:%d:%d", filepath, line, col) + + vim.fn.setreg("+", output) + + print("Copied: " .. output) +end + local builtin = require("telescope.builtin") local keymap = vim.keymap @@ -39,9 +88,12 @@ keymap.set("n", "fh", builtin.help_tags, { desc = "FZF: display help tag keymap.set("n", "ft", "TodoTelescope", { desc = "FZF: display TODO comments" }) keymap.set("n", "fds", builtin.lsp_document_symbols, { desc = "Grep: Document symbols" }) keymap.set('n', 'fp', live_grep_with_glob, { desc = 'Live grep with glob pattern' }) +keymap.set('n', 'fcp', live_grep_in_current_dir, { desc = 'Live grep in current directory' }) +keymap.set('n', 'ccp', copy_file_path_and_position, {desc = "Copy cursor position"}) +keymap.set('n', 'ccd', copy_current_file_path, { desc = 'Live grep in current directory' }) -- Live grep in current file -vim.keymap.set('n', 'fc', function() +vim.keymap.set('n', 'fcf', function() require('telescope.builtin').live_grep({ search_dirs = { vim.fn.expand('%:p') }, -- Current file's full path -- disable_coordinates = true, -- Hide line/column numbers