Skip to content

Instantly share code, notes, and snippets.

@acepukas
Last active January 12, 2022 17:26
Show Gist options
  • Save acepukas/7600c0677d77b2aeda999fd3c3a24379 to your computer and use it in GitHub Desktop.
Save acepukas/7600c0677d77b2aeda999fd3c3a24379 to your computer and use it in GitHub Desktop.
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
export FZF_DEFAULT_COMMAND="$RG_COMMAND_BASE --files"
" 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>
@gbnaidu
Copy link

gbnaidu commented Sep 8, 2020

Thanks for the reply again.
I was giving empty string when triggering, i.e "RG". Sorry for the confusion.

After giving some string, I am getting the matches correctly.

Could you let me know the usage example for the below commands:
" Search files for word under cursor
nnoremap * "zyiw :let cmd = 'RGSW ' . @z call histadd("cmd", cmd) execute cmd

" Search files for visually selected text
xnoremap * "zy :let cmd = 'RGS ' . @z call histadd("cmd", cmd) execute cmd

thanks

@gbnaidu
Copy link

gbnaidu commented Sep 8, 2020

I went through your comments little more carefully.

I can trigger the commands using * either at a word or at a selected text in visual mode.

Is my understanding correct?

@acepukas
Copy link
Author

acepukas commented Sep 8, 2020

@gbnaidu Ah yes, sorry about that. I could have described the usage a little better.

The nnoremap <leader>* mapping uses the leader key. In my case I have the leader key mapped to the Space key. By default vim maps the leader key to \.

If you use a different leader key then you'll have to hit that key for these commands to work.

So the way you would use this particular mapping is if you have your cursor over a word in insert mode, then hold the shift key down, then hit <leader> (Space key in my case) and then * in sequence, vim will execute :RGSW {word_under_cursor} for you. It's just a convenience.

The xnoremap <leader>* mapping functions the same way except it only works while in visual selection mode and doesn't restrict the search to word boundaries. So you could have something like functionName() selected, hit the key combo, and it would search for that string literally, punctuation symbols included.

Keep in mind that you can change the mapping to something more comfortable if you want. I just have it set up that way because I have so many mappings and I need to keep them from colliding. I suggest looking up the vim documentation regarding custom mappings for a better understanding of how that works.

@acepukas
Copy link
Author

acepukas commented Sep 8, 2020

@gbnaidu Yes! Correct. Sorry, missed your comment there. Well now you have a thorough explanation after you already figured it out! :)

@gbnaidu
Copy link

gbnaidu commented Sep 22, 2020

Hi Aaron,
How are you?

I came across one strange issue.

The above gist you had shared is working fine without any issue in VIM.

However, if I try "RG <pattern" in GVIM, I am getting errors.

What I am observing is GVIM doesn't seem to be able to get the files based on the pattern given. It is showing all the files in a separate window. I verified that the rg command is working correctly if I run manually.

rg command being used: rg --ignore-file ~/.ignore --hidden --follow --column --line-number --no-heading --color "always" --ignore-case --fixed-strings -- 'master'

FZF_DEFAULT_COMMAND is: rg --ignore-file ~/.ignore --hidden --follow --files

If I select one of those files, I get the below error:

Error detected while processing function Fzf_grep[3]..fzf#vim#grep[25]..42_
fzf[18]..fzf#run[63]..15_callback:
line 21:
Vim(let):E684: list index out of range: 1

Here are the details of versions:

$ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled May 20 2017 00:44:30)
Included patches: 1-600
Compiled by wirobert@linux-rhel7-64

$ gvim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled May 20 2017 00:44:30)
Included patches: 1-600
Compiled by wirobert@linux-rhel7-64

Thanks
gb

@acepukas
Copy link
Author

acepukas commented Sep 22, 2020 via email

@gbnaidu
Copy link

gbnaidu commented Sep 22, 2020

Thanks Aaron. No problem.

Will check if I can figure it out. It is not urgent now.

Good day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment