-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(alias): ls alias lx is an illegal option on darwin bsd style ls #1752
fix(alias): ls alias lx is an illegal option on darwin bsd style ls #1752
Conversation
👍 Edit: correct PR number. |
modules/utility/init.zsh
Outdated
@@ -118,7 +118,9 @@ alias ll='ls -lh' # Lists human readable sizes. | |||
alias lr='ll -R' # Lists human readable sizes, recursively. | |||
alias la='ll -A' # Lists human readable sizes, hidden files. | |||
alias lm='la | "$PAGER"' # Lists human readable sizes, hidden files through pager. | |||
alias lx='ll -XB' # Lists sorted by extension (GNU only). | |||
if [[ "$OSTYPE" != darwin* ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this to the top? Makes the code more maintainable in the long run. (this can be considered as a nitpick :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can do. But I just considered to keep changes at a minimum.
But regarding readability I also had something in mind, and that is why I did #1793 with helpers. Presuming that helper would exist, would that calm your nit'ish feeling a little? 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1793 is highly optional. I was just mentioning it, because I interpreted your comment as a readability one.
Will add belak then... thx for the hint 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment is totality a readability one 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this code now. Now I check for the capability of ls instead of the OS running.
Just the fact that you're running on darwin doesn't prove your having a BSD style ls
.
Since I'm aware of its existence I myself have the gnu coreutils installed here, so for myself this fix is not needed any more. 😉
Added a comment |
I'm not sure the best way to do this, but this alias should be enabled only if ls is coreutils based. I believe this will also be broken on BSD. It's good to know though. I'd be happy to merge this with a slightly better check. |
What would you think if I checked for ls running with code 0 with This way it would really only depend on ls's capabilities, and not on the OS at all. if ls -XB &> /dev/null; then
alias lx='ll -XB' # Lists sorted by extension (GNU only).
fi Haven't tried if that would work well everywhere. The idea just came up when you asked for a better check... |
018a6df
to
cb76c48
Compare
The option `-X` lets MacOS (and probably also other BSD-ish OS) ls complain about an illegal option: ``` ls -lhXB ls: illegal option -- X usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...] ```
cb76c48
to
cd12f76
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, other than the way to test for gnu coreutils.
alias lk='ll -Sr' # Lists sorted by size, largest last. | ||
alias lt='ll -tr' # Lists sorted by date, most recent last. | ||
alias lc='lt -c' # Lists sorted by date, most recent last, shows change time. | ||
alias lu='lt -u' # Lists sorted by date, most recent last, shows access time. | ||
alias sl='ls' # I often screw this up. | ||
|
||
# Only make alias if ls supports -XB (darwins BSD style ls doesn't) | ||
if ls -XB &> /dev/null; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if you would consider if is-callable 'dircolors'
instead.
ls -XB
might be expensive depending on the directory on which it is executed.
These things keep on adding to the startup time (unless we memoize it on first call).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could use ls /dev/null
since that should only be a single file and should be fast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, ls -XB /dev/null
would be a great idea! That said, would be nice to be consistently using one way to detect BSD/GNU versions. Turns out we use is-callable 'dircolors'
to detect this already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just detect the options via --help
like so:
if grep -q '\-X' <(ls --help 2>&1); then
# ...
fi
We do similar thing in rsync
module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@indrajitr I wasn't aware of the performance penalty my idea could introduce, and your concern makes quite a lot of sense.
On the part of finding a better solution:
At first glance the most straight forward solution @belak has suggested ls -XB /dev/null
seems feasible, but in the end the callable 'dircolors'
appears to be the most efficient and elegant alternative.
But regarding the test with dircolors, I have one question: Is there an unlikeley case I might not see, where dircolors is not callable and gnu-ls is present or the other way around?
Fixes illegal option
-X
on MacOSThe option
-X
gets MacOS'ls
(and probably also other BSD-ish OS) to complain about an illegal option:user@host ~ $ ls -lhXB ls: illegal option -- X usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]
Proposed Changes
lx
with option-X
on machines that are not DarwinI would also exclude other BSD like OS but since I do not have such a host at hand I cannot verify if that is even really needed for them.