Skip to content

Instantly share code, notes, and snippets.

@jsit
Last active September 18, 2019 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsit/f83dd2994b47925f5a6cb7c8d301e69a to your computer and use it in GitHub Desktop.
Save jsit/f83dd2994b47925f5a6cb7c8d301e69a to your computer and use it in GitHub Desktop.
Native Vim search with Ag, including second parameter for filename match pattern
function! ParseAgList(...)
let l:string = join(a:000)
" http://vpaste.net/qTrzV
" Thanks irc://chat.freenode.net/m_ben,isnick
let l:pat = '\v%([^\\]\\)@<!\s+|([''"])%(.{-})\\@<!\1\zs\s*'
let l:args = split(l:string, pat)
if len(l:args) > 1
" sanitize
try
let l:first = eval(join(l:args[0:-2], " "))
catch
let l:first = join(l:args[0:-2], " ")
endtry
let l:return = [l:first, " -G " . l:args[-1]]
else
" sanitize
try
let l:return = [eval(l:string), ""]
catch
let l:return = [l:string, ""]
endtry
endif
return l:return
endfunction
" All of the following should work:
" :Ag foo
" :Ag 'foo'
" :Ag --foo
" :Ag '--foo'
" :Ag 'foo bar'
" :Ag 'foo--bar'
" :Ag foo ./*.baz
" :Ag 'foo' ./*.baz
" :Ag --foo ./*.baz
" :Ag '--foo' ./*.baz
" :Ag 'foo bar' ./*.baz
" :Ag 'foo--bar' ./*.baz
if executable("ag")
set grepprg=ag\ -S\ --vimgrep
command! -nargs=+ -bar Ag exec "silent! grep! '" . escape(ParseAgList(<f-args>)[0], '-') . "' " . ParseAgList(<f-args>)[1]|cwindow|redraw!
nnoremap <leader>gg :Ag<space>
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment