djui / alias-tips

An oh-my-zsh plugin to help remembering those aliases you defined once
781 stars 51 forks source link

grep -> ripgrep alias error #40

Closed samhh closed 6 years ago

samhh commented 7 years ago

Hi there.

I've discovered that using alias-tips alongside the following alias provides the following error:

alias grep='rg -uu'

Error parsing regex near '.\+ () {$' at character offset 14: Empty regex groups (e.g., '()') are not allowed.

djui commented 7 years ago

Is it just this alias or is it just maybe the first in your list?

The alias looks so trivial so I think it’s not the alias that’s the problem but rather the command/Regexer that does not work on your OS/env. Could you provide some more info on OS, Shell, Python version?

samhh commented 7 years ago

Sure thing.

It's this alias specifically. I have a couple dozen others (in my dotfiles repo), none of which throw this error into the console.

macOS: 10.12.6 iTerm 2: 3.1.beta.5 zsh: 5.4.1 Python 2 (default / system python): 2.7.10 Python 3: 3.6.2

djui commented 7 years ago

The problem seems to be that within alias-tips we use shell_functions=$(functions | grep '^[^'$'\t'' _-].\+ () {$') which itself uses grep which in your case is aliased to ripgrep. However, grep and ripgrep don't seem to have the same default when it comes to escaping special Regexp characters such as (, ), {: grep seems to parse the given pattern verbatim, seeing () as a string, rather than regexp special characters. While on the other hand, after using the alias, ripgrep seems to interpret them as regexp special characters and then complains that they represent an empty group.

I have no currently no certain way how to solve this. Some ideas:

  1. Don't use the aliased version of grep for alias-tips.
  2. Write the regexp pattern in a way it's compatible with grep and ripgrep.
  3. Use a different alias that does not shadow grep.
djui commented 7 years ago

I think (1) should work in a very compatible way. Will give that a try.

SimenB commented 7 years ago

However, grep and ripgrep don't seem to have the same default when it comes to escaping special Regexp characters such as

that might be worth a bug report at https://github.com/BurntSushi/ripgrep, at least to make it clear if it's on purpose or not

djui commented 7 years ago

Could you give https://github.com/djui/alias-tips/pull/41 a try before I merge it?

djui commented 7 years ago

@SimenB (@SamHH) In short, to me ripgrep is more of an drop-in replacement for egrep rather than grep. To explain a bit in more detail:

The following command lines are equivalent...

functions |  grep    '^[a-zA-Z].\+ () {$'
functions | egrep    '^[a-zA-Z].+ \(\) \{$'
functions |  grep -E '^[a-zA-Z].+ \(\) \{$'
functions |  rg      '^[a-zA-Z].+ \(\) \{$'

...but come with different trade-offs afaik:

(1) is the most compatible and available. (2) is best compatible to ripgrep's syntax (grep: extended syntax). (3) is behaving like egrep, compatible to ripgrep's regexp pattern syntax, but the -E flag is differently interpreted ("encoding") by ripgrep thus breaking an alias. (4) again, is compatible to egrep.

Given...

In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.

...we could switch to egrep (still doing \egrep in case egrep is aliased) and then use the same regexp syntax as ripgrep does.

djui commented 7 years ago

I updated #41 to reflect the mentioned strategy in the previous comment.