From 01c11860b9c1538a2cbe73af20d42f15bbaf8a0c Mon Sep 17 00:00:00 2001 From: liuzhao11 Date: Fri, 8 Oct 2021 18:03:50 +0800 Subject: [PATCH] Sub: Add doc info & mappings to open file in search results Body: Fix: No ==== End ==== --- doc/vim-ripgrep.txt | 16 ++++++++++++++++ plugin/vim-ripgrep.vim | 28 ++++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 doc/vim-ripgrep.txt diff --git a/doc/vim-ripgrep.txt b/doc/vim-ripgrep.txt new file mode 100644 index 0000000..5903f5f --- /dev/null +++ b/doc/vim-ripgrep.txt @@ -0,0 +1,16 @@ +*vim-ripgrep.txt* Plugin that integrates rg with Vim +============================================================================== +INTRODUCTION *rg* + +This plugin is a front for ripgrep: rg. Rg can be used as a replacement for ack +or ag. This plugin will allow you to run rg from vim, and shows the results in +a split window. + +:Rg[!] [options] {pattern} [{directory}] *:Rg* + + Search recursively in {directory} (which defaults to the current + directory) for the {pattern}. Behaves just like the |:grep| command, but + will open the |Quickfix| window for you. If [!] is not given the first + error is jumped to. + + vim:tw=78:fo=tcq2:ft=help:norl: diff --git a/plugin/vim-ripgrep.vim b/plugin/vim-ripgrep.vim index 9bc029d..2c5aed9 100644 --- a/plugin/vim-ripgrep.vim +++ b/plugin/vim-ripgrep.vim @@ -33,16 +33,16 @@ fun! s:Rg(txt) endfun fun! s:RgGetVisualSelection() - " Why is this not a built-in Vim script function?! - let [line_start, column_start] = getpos("'<")[1:2] - let [line_end, column_end] = getpos("'>")[1:2] - let lines = getline(line_start, line_end) - if len(lines) == 0 - return '' - endif - let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)] - let lines[0] = lines[0][column_start - 1:] - return join(lines, "\n") + " Why is this not a built-in Vim script function?! + let [line_start, column_start] = getpos("'<")[1:2] + let [line_end, column_end] = getpos("'>")[1:2] + let lines = getline(line_start, line_end) + if len(lines) == 0 + return '' + endif + let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)] + let lines[0] = lines[0][column_start - 1:] + return join(lines, "\n") endfun fun! s:RgSearchTerm(txt) @@ -61,10 +61,18 @@ fun! s:RgSearch(txt) if &smartcase == 1 let l:rgopts = l:rgopts . '-S ' endif + silent! exe 'grep! ' . l:rgopts . a:txt if len(getqflist()) exe g:rg_window_location 'copen' redraw! + " Map commands in quickfix window to open files in search results + nnoremap h K + nnoremap H Kb + nnoremap o + nnoremap t T + nnoremap T TgT + nnoremap v HbJt if exists('g:rg_highlight') call s:RgHighlight(a:txt) endif