visual-studio-codecommand-linexargsfzfripgrep

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?


Solution

  • My VSCode fzf extension does this for you when running the rg commands from the command palette.

    The extension leverages the --vimgrep option to rg that returns just what the doctor ordered. I added this capability in response to the issue 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