Last active
January 12, 2022 17:26
-
-
Save acepukas/7600c0677d77b2aeda999fd3c3a24379 to your computer and use it in GitHub Desktop.
Convenient commands for ripgrep and fzf.vim integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
export FZF_DEFAULT_COMMAND="$RG_COMMAND_BASE --files" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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, ' ') | |
endfun | |
" 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) | |
endfun | |
" 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
Thanks Aaron. No problem.
Will check if I can figure it out. It is not urgent now.
Good day