z
z copied to clipboard
Support smart case
Hi,
Would it be good idea to support "smart case"? I.e. do case insensitive matching if the query is all lowercase, and do the current case sensitive matching only if the query contains upper-case? This is similar to what e.g. vim
and fzf
does by default.
It could be enabled with e.g. _Z_SMART_CASE
if there are people who don't like it.
I'll submit a PR if this sounds acceptable.
current behaviour is to attempt case sensitive match, if not found, we try case insensitive.
i have considered the approach of case-sensitive-if-there's-an-upper-case-letter. I think the code would be cleaner. haven't done it cause current behaviour seems to work pretty well ...
I have the habit of rarely having upper case in queries. Tend to also not remember case of directories. This throws me in the wrong directory from time to time.
I also have a local script where I pipe the output of z -l
to fzf
which presents an interactive selector if the top matches have similar score. For this use case I want case insensitive matches included.
For the normal usage of z
(jump straight to best match) I agree smart case might be more arguable..
I also first though smart-case would make the user experience better. But:
Consider these directories with their respective "frecencies":
3 /tmp/foo/bar
5 /tmp/foo/Bar
8 /tmp/Bar
13 /tmp/bar
21 /tmp
This would be the result of different queries with the two approaches:
query | smart-case | prefer case sensitive |
---|---|---|
tm | /tmp | /tmp |
Tm | (nothing) | /tmp |
ba | /tmp/bar | /tmp/bar |
Ba | /tmp/Bar | /tmp/Bar |
fo ba | /tmp/foo/Bar | /tmp/foo/bar |
fo Ba | /tmp/foo/Bar | /tmp/foo/Bar |
In the case of two directories with names with just different cases, the smart-case matching would "hide" the all-lowercase one if it has a lower raking than a mixed-case sibling.
Agree for the normal use case. I was mainly looking for a way to get case insensitive matches included in the output of z -l
(list all matches with scores). That would be quite useful in combination with fzf
.
Maybe z
could give users the option to use either case sensitive, insensitive, or smart-case matching, having one of these as default.
@rupa, thoughts?
This is such a good idea
@ericbn I think it's rare to have two directories of same name but different case as siblings. Just provide a switch to force case-sensitivity, I'm certain that smart case matching would be the best default.
Created PR #221 changing the default matching strategy to smart case, and adding two new parameters to force case insensitive (-i
) or case sensitive (-s
) matching. Solution is just 1 LOC bigger than the original implementation. Also updated the man page and the README.