Ripgrep: Use environment variables inside .rgrc

Created on 29 May 2018  路  3Comments  路  Source: BurntSushi/ripgrep

What version of ripgrep are you using?

ripgrep 0.8.1 (rev c8e9f25b85)
+SIMD -AVX

How did you install ripgrep?

Using binary release

What operating system are you using ripgrep on?

Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial

Describe your question, feature request, or bug.

I think environment variables used inside RIPGREP_CONFIG_PATH file should be expanded.

I have a file in my home (.rgrc):

--smart-case
--hidden
--ignore-file
$HOME/.rgignore

But when I use rg I have this message:

$ export RIPGREP_CONFIG_PATH=$HOME/.rgrc
$ rg any > /dev/null
$HOME/.rgignore: No such file or directory (os error 2)

I would like to be able to reuse those files (.rgrc and .rgingnore) across multiple accounts, this I source my zsh configuration from a git repo.

Do you think it would be possible ?

question wontfix

Most helpful comment

Here's a zsh script you could use if you were committed to the idea:

 #!/usr/bin/env zsh

rgrc=${RIPGREP_CONFIG_PATH:-$HOME/.rgrc}

if [[ -e $rgrc ]]; then
  trap 'rm -f -- $tmp' EXIT INT
  tmp=$( mktemp )
  print -r - ${(Xe)"$( < $rgrc )"} > $tmp
  rgrc=$tmp
else
  rgrc=
fi

RIPGREP_CONFIG_PATH=$rgrc rg "$@"

It takes your config file and runs it through zsh's (e) expansion flag (which performs parameter expansion and command substitution on the input), then dumps the evaluated version to a temp file, and passes that as the config file to rg.

Obviously some security concerns here, since the script can execute arbitrary commands in the config file. It also adds probably 2 to 5 ms of over-head to each rg call. You could make it faster by caching the evaluated result. Not sure about making it safer, i don't think zsh has an option to do parameter expansion without command substitution.

All 3 comments

No, sorry.

ripgrep's configuration file is a thin veneer over specifying the argv sent to ripgrep's process, at which point, the argv parser merges all of the configuration directly. There are only two simple rules to follow: each line is a single argv parameter (after trimming ASCII whitespace) and lines beginning with a # are ignored. Adding environment variable interpolation means changing those rules to something quite a bit more complex, which includes the ability to escape environment variables so that folks can use them literally as well. Moreover, this sounds like a feature that begets even more features. Do we also need to support the ${...} syntax? What about ${name:-default} syntax?

You have many alternative choices available to you:

  • Use two different config files on your machines with different home directories.
  • Add your config that uses environment variables to an alias wrapper around rg.
  • Add your config that uses environment variables to a shell script wrapper around rg.

Here's a zsh script you could use if you were committed to the idea:

 #!/usr/bin/env zsh

rgrc=${RIPGREP_CONFIG_PATH:-$HOME/.rgrc}

if [[ -e $rgrc ]]; then
  trap 'rm -f -- $tmp' EXIT INT
  tmp=$( mktemp )
  print -r - ${(Xe)"$( < $rgrc )"} > $tmp
  rgrc=$tmp
else
  rgrc=
fi

RIPGREP_CONFIG_PATH=$rgrc rg "$@"

It takes your config file and runs it through zsh's (e) expansion flag (which performs parameter expansion and command substitution on the input), then dumps the evaluated version to a temp file, and passes that as the config file to rg.

Obviously some security concerns here, since the script can execute arbitrary commands in the config file. It also adds probably 2 to 5 ms of over-head to each rg call. You could make it faster by caching the evaluated result. Not sure about making it safer, i don't think zsh has an option to do parameter expansion without command substitution.

Thanks for your reply.

I was hoping that something like https://crates.io/crates/shellexpand could be used to do the job, but it introduces a regression for people with $ in their configuration as you pointed out.

I will do it differently then.

Was this page helpful?
0 / 5 - 0 ratings