├── README.md ├── LICENSE └── plugin └── vim-ripgrep.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-ripgrep 2 | 3 | :Rg 4 | 5 | Word under cursor will be searched if no argument is passed to `Rg` 6 | 7 | ## configuration 8 | 9 | 10 | | Setting | Default | Details 11 | | ---------------------|---------------------------|---------- 12 | | g:rg_binary | rg | path to rg 13 | | g:rg_format | %f:%l:%c:%m | value of grepformat 14 | | g:rg_command | g:rg_binary --vimgrep | search command 15 | | g:rg_highlight | false | true if you want matches highlighted 16 | | g:rg_derive_root | false | true if you want to find project root from cwd 17 | | g:rg_root_types | ['.git'] | list of files/dir found in project root 18 | | g:rg_window_location | botright | quickfix window location 19 | 20 | ## misc 21 | 22 | Show root search dir 23 | 24 | :RgRoot 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2014 thoughtbot, inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /plugin/vim-ripgrep.vim: -------------------------------------------------------------------------------- 1 | if exists('g:loaded_rg') || &cp 2 | finish 3 | endif 4 | 5 | let g:loaded_rg = 1 6 | 7 | if !exists('g:rg_binary') 8 | let g:rg_binary = 'rg' 9 | endif 10 | 11 | if !exists('g:rg_format') 12 | let g:rg_format = "%f:%l:%c:%m" 13 | endif 14 | 15 | if !exists('g:rg_command') 16 | let g:rg_command = g:rg_binary . ' --vimgrep' 17 | endif 18 | 19 | if !exists('g:rg_root_types') 20 | let g:rg_root_types = ['.git'] 21 | endif 22 | 23 | if !exists('g:rg_window_location') 24 | let g:rg_window_location = 'botright' 25 | endif 26 | 27 | fun! g:RgVisual() range 28 | call s:RgGrepContext(function('s:RgSearch'), '"' . s:RgGetVisualSelection() . '"') 29 | endfun 30 | 31 | fun! s:Rg(txt) 32 | call s:RgGrepContext(function('s:RgSearch'), s:RgSearchTerm(a:txt)) 33 | endfun 34 | 35 | fun! s:RgGetVisualSelection() 36 | " Why is this not a built-in Vim script function?! 37 | let [line_start, column_start] = getpos("'<")[1:2] 38 | let [line_end, column_end] = getpos("'>")[1:2] 39 | let lines = getline(line_start, line_end) 40 | if len(lines) == 0 41 | return '' 42 | endif 43 | let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)] 44 | let lines[0] = lines[0][column_start - 1:] 45 | return join(lines, "\n") 46 | endfun 47 | 48 | fun! s:RgSearchTerm(txt) 49 | if empty(a:txt) 50 | return expand("") 51 | else 52 | return a:txt 53 | endif 54 | endfun 55 | 56 | fun! s:RgSearch(txt) 57 | let l:rgopts = ' ' 58 | if &ignorecase == 1 59 | let l:rgopts = l:rgopts . '-i ' 60 | endif 61 | if &smartcase == 1 62 | let l:rgopts = l:rgopts . '-S ' 63 | endif 64 | silent! exe 'grep! ' . l:rgopts . a:txt 65 | if len(getqflist()) 66 | exe g:rg_window_location 'copen' 67 | redraw! 68 | if exists('g:rg_highlight') 69 | call s:RgHighlight(a:txt) 70 | endif 71 | else 72 | cclose 73 | redraw! 74 | echo "No match found for " . a:txt 75 | endif 76 | endfun 77 | 78 | fun! s:RgGrepContext(search, txt) 79 | let l:grepprgb = &grepprg 80 | let l:grepformatb = &grepformat 81 | let &grepprg = g:rg_command 82 | let &grepformat = g:rg_format 83 | let l:te = &t_te 84 | let l:ti = &t_ti 85 | let l:shellpipe_bak=&shellpipe 86 | set t_te= 87 | set t_ti= 88 | if !has("win32") 89 | let &shellpipe="&>" 90 | endif 91 | 92 | if exists('g:rg_derive_root') 93 | call s:RgPathContext(a:search, a:txt) 94 | else 95 | call a:search(a:txt) 96 | endif 97 | 98 | let &shellpipe=l:shellpipe_bak 99 | let &t_te=l:te 100 | let &t_ti=l:ti 101 | let &grepprg = l:grepprgb 102 | let &grepformat = l:grepformatb 103 | endfun 104 | 105 | fun! s:RgPathContext(search, txt) 106 | let l:cwdb = getcwd() 107 | exe 'lcd '.s:RgRootDir() 108 | call a:search(a:txt) 109 | exe 'lcd '.l:cwdb 110 | endfun 111 | 112 | fun! s:RgHighlight(txt) 113 | let @/=escape(substitute(a:txt, '"', '', 'g'), '|') 114 | call feedkeys(":let &hlsearch=1\", 'n') 115 | endfun 116 | 117 | fun! s:RgRootDir() 118 | let l:cwd = getcwd() 119 | let l:dirs = split(getcwd(), '/') 120 | 121 | for l:dir in reverse(copy(l:dirs)) 122 | for l:type in g:rg_root_types 123 | let l:path = s:RgMakePath(l:dirs, l:dir) 124 | if s:RgHasFile(l:path.'/'.l:type) 125 | return l:path 126 | endif 127 | endfor 128 | endfor 129 | return l:cwd 130 | endfun 131 | 132 | fun! s:RgMakePath(dirs, dir) 133 | return '/'.join(a:dirs[0:index(a:dirs, a:dir)], '/') 134 | endfun 135 | 136 | fun! s:RgHasFile(path) 137 | return filereadable(a:path) || isdirectory(a:path) 138 | endfun 139 | 140 | fun! s:RgShowRoot() 141 | if exists('g:rg_derive_root') 142 | echo s:RgRootDir() 143 | else 144 | echo getcwd() 145 | endif 146 | endfun 147 | 148 | command! -nargs=* -complete=file Rg :call s:Rg() 149 | command! RgRoot :call s:RgShowRoot() 150 | --------------------------------------------------------------------------------