Neovim: Add hotkey to grep for selected text

This commit is contained in:
Tony Grosinger 2024-05-30 21:14:26 -07:00
parent 85f3dbc61d
commit 5ff61f8757

View File

@ -147,6 +147,22 @@ vim.keymap.set('n', '<C-l>', '<C-w><C-l>', { desc = 'Move focus to the right win
vim.keymap.set('n', '<C-j>', '<C-w><C-j>', { desc = 'Move focus to the lower window' })
vim.keymap.set('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper window' })
-- [[ Utility functions ]]
-- Retreive selected text from the editor.
function vim.getVisualSelection()
vim.cmd 'noau normal! "vy"'
local text = vim.fn.getreg 'v'
vim.fn.setreg('v', {})
text = string.gsub(text, '\n', '')
if #text > 0 then
return text
else
return ''
end
end
-- [[ Basic Autocommands ]]
-- See `:help lua-guide-autocommands`
@ -316,6 +332,13 @@ require('lazy').setup({
}
end, { desc = '[S]earch Recent Files in CWD' })
vim.keymap.set('v', '<leader>g', function()
builtin.grep_string {
search = vim.getVisualSelection(),
prompt_title = 'Grep all files',
}
end, { desc = '[G]rep everywhere' })
-- Slightly advanced example of overriding default behavior and theme
vim.keymap.set('n', '<leader>/', function()
-- You can pass additional configuration to Telescope to change the theme, layout, etc.