From 705e3f52fdf5f382a23fb7786351e5abd08facc0 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 11 May 2023 17:31:02 +0300 Subject: [PATCH] add greping with selected text --- lua/config/telescope.lua | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lua/config/telescope.lua b/lua/config/telescope.lua index df715f3..2b151b1 100644 --- a/lua/config/telescope.lua +++ b/lua/config/telescope.lua @@ -86,6 +86,23 @@ if fzfPlugin and fzfPlugin.loaded then telescope.load_extension('fzf') end +local function get_vtext() + local prev_regv = vim.fn.getreg('v') + vim.cmd('noau normal! "vy"') + local text = vim.fn.getreg('v') + vim.fn.setreg("v", prev_regv) + + text = string.gsub(text, "\n", "") + if #text > 0 then + return text + else + return "" + end +end + +local function escape_sed_symbols(text) + return text:gsub("[.()%[%]]", "\\%1") +end local keymaps = { -- Search project files @@ -98,7 +115,13 @@ local keymaps = { {"ce", edit_config, description = "Edit neovim config" }, -- Grep string - { "fw", function() builtin.live_grep() end, description = "Grep" }, + { "fw", { + n = function() builtin.live_grep() end, + v = function(a) + local text = escape_sed_symbols(get_vtext()) + builtin.live_grep{ default_text = text } + end + }, description = "Grep" }, -- Change colorscheme { "cs", function() builtin.colorscheme() end, description = "Change colorscheme" },