Variables in --replace string, e.g., ${filename}, ${linenum}, etc #2253
-
Are there variables other than the ones available through PCRE2 that we can use in the --replace string? # Running this in vscode terminal: for each match, ripgrep outputs a clickable
# path:linenumber, then it shows the match below that line
rg -P '^.*func(.*)$' --replace=$'${relpath}:${linenum}\n$0' Please don't reply to this as "XY problem, do this instead, closed". Although there might be easier solutions for what got me here, I'm genuinely interested in knowing what we're allowed to use besides PCRE2 variables. This would be quite useful, as we'd no longer depend as much on the existing set of output options, or other external tools for small changes (sed/awk/tr... etc). Since ripgrep has file<->search information, it doesn't seem absurd to think we could use this when formatting the output. I marked this as Q&A, since I don't know if it exists. I couldn't find it here nor anywhere else with gh search or google. It could be moved to ideas if it isn't a thing yet. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
No, there aren't. The documentation is correct. And I don't have any plans to add any. ripgrep is a search tool. I do not want to turn it into replacement tool or a sed-like replacement. The FAQ lists other tools for that. Also, do note that the variables currently available are not really "PCRE2 variables." They are capture group variables. The replacement code is implemented as generic interpolation that works for any regex engine. See: https://docs.rs/grep-matcher/latest/grep_matcher/trait.Captures.html#method.interpolate |
Beta Was this translation helpful? Give feedback.
No, there aren't. The documentation is correct. And I don't have any plans to add any. ripgrep is a search tool. I do not want to turn it into replacement tool or a sed-like replacement. The FAQ lists other tools for that.
Also, do note that the variables currently available are not really "PCRE2 variables." They are capture group variables. The replacement code is implemented as generic interpolation that works for any regex engine. See: https://docs.rs/grep-matcher/latest/grep_matcher/trait.Captures.html#method.interpolate