rupa / z

z - jump around
Do What The F*ck You Want To Public License
16.29k stars 1.17k forks source link

Support smart case #209

Open ptzz opened 7 years ago

ptzz commented 7 years ago

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.

rupa commented 7 years ago

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 ...

ptzz commented 7 years ago

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..

ericbn commented 6 years ago

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.

ptzz commented 6 years ago

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.

ericbn commented 6 years ago

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?

ghost commented 6 years ago

This is such a good idea

ghost commented 6 years ago

@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.

ericbn commented 6 years ago

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.