Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fuzzy find all files containing specific text using ripgrep and fzf and open it VSCode

I have the following command to fuzzy find files in the command line and to open the selected file in VSCode.:

fzf --print0 -e | xargs -0 -r code

Now I want to be able to search also file contents for a string. I am able to find the searched string in the command line:

rg . | fzf --print0 -e

vscode_console_ripgrep_fzf_string_search.png

but now it does not work anymore to open the file in VSCode using this command:

rg . | fzf --print0 -e | xargs -0 -r code

because to VSCode is passed a file name which contains the file name itself and the search string which is of course an empty file.

How can I combine to two above commands to pass the file name to VSCode which contains the searched string?

like image 999
BuZZ-dEE Avatar asked May 11 '20 23:05

BuZZ-dEE


1 Answers

The --vimgrep option to rg returns just what the doctor ordered. That's what I used in the vscode extension issue request you put in:

https://github.com/rlivings39/vscode-fzf-quick-open/commit/101a6d8e44b707d11e661ca10aaf37102373c644

It returns data like:

$ rg --vimgrep
extension.ts:5:5:let fzfTerminal: vscode.Terminal | undefined = undefined;
extension.ts:6:5:let fzfTerminalPwd: vscode.Terminal | undefined = undefined;

Then you can cut out the first 3 fields and pass them to code -g:

rg --vimgrep --color ansi | fzf --ansi --print0 | cut -z -d : -f 1-3 | xargs -0 -r code -g
like image 134
Ryan Livingston Avatar answered Oct 02 '22 19:10

Ryan Livingston