diff --git a/lua/plugins/telescope.lua b/lua/plugins/telescope.lua index d73b1f4..76ede66 100644 --- a/lua/plugins/telescope.lua +++ b/lua/plugins/telescope.lua @@ -36,7 +36,6 @@ return { vim.keymap.set('n', 'gF', builtin.find_files, opts) vim.keymap.set('n', 'gf', builtin.git_files, opts) vim.keymap.set('n', 'gg', builtin.live_grep, opts) - vim.keymap.set('n', 'rg', builtin.grep_string, opts) vim.keymap.set('n', 'gb', builtin.buffers, opts) vim.keymap.set('n', 'gh', builtin.help_tags, opts) vim.keymap.set('n', 'bl', builtin.current_buffer_fuzzy_find, opts) diff --git a/plugin/commands.lua b/plugin/commands.lua index 04a020a..eec8d28 100644 --- a/plugin/commands.lua +++ b/plugin/commands.lua @@ -116,16 +116,31 @@ end, { complete = 'file', }) --- :Rg grep for the given string and fuzzy find the results, bang to enable --- regex in given string --- FIXME: Support visual select modes +-- :Rg grep for the given string, word under the cursor, or visual selection +-- then fuzzy find the results. Bang to enable regex in given string. vim.api.nvim_create_user_command('Rg', function(opts) - local grep_opts = { search = opts.args } + print(vim.inspect(opts)) + local search = opts.args + if opts.range == 2 then + local lines = vim.fn.getline(vim.fn.line("'<"), vim.fn.line("'>")) + local start_col = vim.fn.col("'<") + local end_col = vim.fn.col("'>") + if #lines == 1 then + lines[1] = string.sub(lines[1], start_col, end_col) + else + lines[1] = string.sub(lines[1], start_col) + lines[#lines] = string.sub(lines[#lines], 1, end_col) + end + search = table.concat(lines, "\n") + elseif #search == 0 then + search = vim.fn.expand('') + end + local grep_opts = { search = search} if opts.bang then grep_opts['use_regex'] = true end require('telescope.builtin').grep_string(grep_opts) -end, { bang = true, nargs = '*' }) +end, { bang = true, range = true, nargs = '*' }) vim.api.nvim_create_user_command('PreProcIfWrap', function(opts) local buffer = vim.api.nvim_get_current_buf() diff --git a/plugin/mappings.lua b/plugin/mappings.lua index 75ffc17..0f31448 100644 --- a/plugin/mappings.lua +++ b/plugin/mappings.lua @@ -43,3 +43,7 @@ vim.keymap.set('t', '', ':Gdb:startinsert', opts) vim.keymap.set('t', '', ':Program', opts) vim.keymap.set('t', '', ':Source', opts) vim.keymap.set('t', '', ':Asm', opts) + +-- Mappings to grep for then fuzzy find the word under the cursor or visual selection +vim.keymap.set('n', 'rg', ':Rg', opts) +vim.keymap.set('v', 'rg', ":'<,'>Rg", opts)