Skip to content

Instantly share code, notes, and snippets.

Forked from acepukas/.zshrc
Created January 12, 2022 17:26
What would you like to do?
Convenient commands for ripgrep and fzf.vim integration
# RIPGREP config (this is the environment variable that vim is reading when building the rg command)
# In this case the shell is zsh. Adjust accordingly based on preferred shell and needs.
export RG_COMMAND_BASE='rg --ignore-file ~/.ignore --hidden --follow'
# Minimal FZF config. Shares the RG_COMMAND_BASE env var.
[ -f $HOME/.fzf.zsh ] && source $HOME/.fzf.zsh
" set up an rg command with options for reuse. Refer to the other gist file (.zshrc) for details on $RG_COMMAND_BASE
let g:rg_command = $RG_COMMAND_BASE . ' --column --line-number --no-heading --color "always"'
" concatenate together all the necessary options for the final call to the rg command
fun! BuildRgCommand(opts, qargs)
let l:list = [g:rg_command] + a:opts + ['--', shellescape(a:qargs)]
return join(l:list, ' ')
" construct the rg command and pass it to the fzf grep command with all necessary options
fun! Fzf_grep(opts, qargs, bang) abort
let l:rg = BuildRgCommand(a:opts, a:qargs)
call fzf#vim#grep(l:rg, 1, {}, a:bang)
" custom commands
" Search literal string recursive ignoring case
command! -bang -nargs=* RG call Fzf_grep(['--ignore-case', '--fixed-strings'], <q-args>, <bang>0)
" Search literal string recursive case sensitive
command! -bang -nargs=* RGS call Fzf_grep(['--fixed-strings'], <q-args>, <bang>0)
" Search recursive case sensitive as RegExp (using ripgrep RegExp engine, _not_ vim RegExp engine)
command! -bang -nargs=* RGX call Fzf_grep([], <q-args>, <bang>0)
" Seach literal string recursive case sensitive with word boundaries
command! -bang -nargs=* RGSW call Fzf_grep(['-w', '--fixed-strings'], <q-args>, <bang>0)
" Search files for word under cursor
nnoremap <leader>* "zyiw :let cmd = 'RGSW ' . @z <bar> call histadd("cmd", cmd) <bar> execute cmd<cr>
" Search files for visually selected text
xnoremap <leader>* "zy :let cmd = 'RGS ' . @z <bar> call histadd("cmd", cmd) <bar> execute cmd <cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment