🎨

ripgrep のパスが紫色で見づらいので変えた話

2022/07/12に公開

問題

rg した結果のパスの色が紫で、自分のターミナルのコントラストと合わず見ずらい。ので色を変えたい。



要約

  • 環境変数に RIPGREP_CONFIG_PATH=$HOME/.ripgreprc を追加
  • $HOME/.ripgreprc--colors=path:fg:cyan と書く
  • 注意点は $HOME/.ripgreprc を置くだけではダメで環境変数が必要

調査手順

"ripgrep color change" でググると FAQ がマッチ。

How do I configure ripgrep's colors?

--colors '{type}:{attribute}:{value}'

  • {type} should be one of path, line, column or match. Each of these correspond to the four different types of things that ripgrep will add color to in its output. Select the type whose color you want to change.
  • {attribute} should be one of fg, bg or style, corresponding to foreground color, background color, or miscellaneous styling (such as whether to bold the output or not).
  • {value} is determined by the value of {attribute}. If {attribute} is style, then {value} should be one of nobold, bold, nointense, intense, nounderline or underline. If {attribute} is fg or bg, then {value} should be a color.

A color is specified by either one of eight of English names, a single 256-bit number or an RGB triple (with over 16 million possible values, or "true color").

The color names are red, blue, green, cyan, magenta, yellow, white or black.

設定は楽しいので無駄に underline や bold などを試すけど、最終的にはシンプルに紫を水色に変更。試行錯誤は実際に引数を指定して実行。

$ rg --colors='path:fg:cyan' RIPGREP_CONFIG_PATH crates/

毎回 --colors するのは面倒なので設定ファイルに書く。

Configuration file

Setting up a configuration file is simple. ripgrep will not look in any predetermined directory for a config file automatically. Instead, you need to set the RIPGREP_CONFIG_PATH environment variable to the file path of your config file. Once the environment variable is set, open the file and just type in the flags you want set automatically. There are only two rules for describing the format of the config file:

一般的に .bashrc や .netrc、.yarnrc などの *rc は $HOME に置くだけで機能するけど .ripgreprc は環境変数 RIPGREP_CONFIG_PATH が必要。環境変数に書かず $HOME に置くだけでは機能しない。

確かに $HOME を見ていないし、カレントディレクトリ内にある .ripgreprc を見る気配もない。

$ rg -C 2 RIPGREP_CONFIG_PATH crates/
crates/core/config.rs
17-/// Return a sequence of arguments derived from ripgrep rc configuration files.
18-pub fn args() -> Vec<OsString> {
19:    let config_path = match env::var_os("RIPGREP_CONFIG_PATH") {
20-        None => return vec![],
21-        Some(config_path) => {
--
30-        Err(err) => {
31-            message!(
32:                "failed to read the file specified in RIPGREP_CONFIG_PATH: {}",
33-                err
34-            );

crates/core/app.rs
1980-        "\
1981-Never read configuration files. When this flag is present, ripgrep will not
1982:respect the RIPGREP_CONFIG_PATH environment variable.
1983-
1984-If ripgrep ever grows a feature to automatically read configuration files in

なので環境変数と .ripgreprc を設定。

$ grep RIPGREP_CONFIG_PATH .bashrc
export RIPGREP_CONFIG_PATH=$HOME/.ripgreprc
$ cat $HOME/.ripgreprc
--colors=path:fg:cyan

おかげで見やすくなりました。ripgrep、いつもありがとう!

Discussion