Last active
June 18, 2026 13:29
-
-
Save erincerys/d977824278017a60f650d041375935e9 to your computer and use it in GitHub Desktop.
Overrided eza defaults to output long-ISO timestamps, color, and puts directories first. Fork of better-ls.sh in andresgongora/synth-shell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| ## +-----------------------------------+-----------------------------------+ | |
| ## | | | |
| ## | Copyright (c) 2018-2020, Andres Gongora <mail@andresgongora.com>. | | |
| ## | | | |
| ## | This program is free software: you can redistribute it and/or modify | | |
| ## | it under the terms of the GNU General Public License as published by | | |
| ## | the Free Software Foundation, either version 3 of the License, or | | |
| ## | (at your option) any later version. | | |
| ## | | | |
| ## | This program is distributed in the hope that it will be useful, | | |
| ## | but WITHOUT ANY WARRANTY; without even the implied warranty of | | |
| ## | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | |
| ## | GNU General Public License for more details. | | |
| ## | | | |
| ## | You should have received a copy of the GNU General Public License | | |
| ## | along with this program. If not, see <http://www.gnu.org/licenses/>. | | |
| ## | | | |
| ## +-----------------------------------------------------------------------+ | |
| ## | |
| ## DESCRIPTION | |
| ## | |
| ## A fork of better-ls.sh, reworked by Claude Opus 4.8 to use eza, a Rust rewrite of ls | |
| ## (https://github.com/andresgongora/synth-shell/blob/master/synth-shell/better-ls.sh) | |
| ## | |
| ## Overrides eza with more useful/easier-to-read defaults. | |
| ## - Colorized output | |
| ## - Long timestamp (long-iso) | |
| ## - Directories grouped first | |
| ## | |
| ## HOW IT WORKS | |
| ## | |
| ## - If no arguments passed | |
| ## Shows hidden directories and files (in their own block, if any) | |
| ## Shows visible directories and files | |
| ## Falls back to listing . and .. when the folder is empty | |
| ## - If a single directory is passed | |
| ## Lists that directory using the no-argument behaviour | |
| ## - If a single file is passed | |
| ## Lists just that file in long format | |
| ## - else | |
| ## Passes the arguments straight to eza with the nicer defaults | |
| ## | |
| ## DEV NOTES | |
| ## | |
| ## local hidden=(.[^.]*) | |
| ## Globs every hidden entry while omitting '.' and '..'. With nullglob | |
| ## unset, a non-matching glob yields the literal pattern as the sole | |
| ## element, so '[ -e "${hidden[0]}" ]' is a safe "does anything match?" | |
| ## test that needs neither 'ls' nor a subshell. | |
| ## | |
| ## .[^.]* | |
| ## Anything starting with '.', followed by one char that can not be | |
| ## '.', and then as many (or any) characters as wanted. | |
| ## | |
| ## -d / --treat-dirs-as-files | |
| ## Required when we pass globbed names explicitly, otherwise eza would | |
| ## recurse into each directory and list its contents instead of the | |
| ## directory entry itself. | |
| ## | |
| # shellcheck disable=2120 | |
| function better_eza() { | |
| { local EZA; EZA="$(type -P eza)"; } | |
| if [ -z "$EZA" ]; then | |
| command ls --color=auto "$@" | |
| return | |
| fi | |
| local opts=( | |
| --colour=auto | |
| --time-style=long-iso | |
| --group-directories-first | |
| ) | |
| if [ $# -eq 0 ]; then | |
| local hidden=(.[^.]*) | |
| local visible=(*) | |
| if [ -e "${hidden[0]}" ]; then | |
| { echo; "$EZA" -ld "${opts[@]}" -- "${hidden[@]}"; } | |
| fi | |
| if [ -e "${visible[0]}" ]; then | |
| "$EZA" -ld "${opts[@]}" -- "${visible[@]}" | |
| elif [ ! -e "${hidden[0]}" ]; then | |
| "$EZA" -ld "${opts[@]}" . .. | |
| fi | |
| elif [ $# -eq 1 ] && [ -d "$1" ]; then | |
| local current_pwd="$PWD" | |
| # shellcheck disable=SC2164 | |
| { cd "$1/" && better_eza; cd "$current_pwd"; } | |
| elif [ $# -eq 1 ] && [ -f "$1" ]; then | |
| "$EZA" -l "${opts[@]}" -- "$1" | |
| else | |
| ## eza prints nothing when asked to list the *implicit* current | |
| ## directory while stdout is not a TTY e.g. when using pipes | |
| for arg in "$@"; do | |
| if [ "${arg#-}" = "$arg" ]; then | |
| "$EZA" "${opts[@]}" "$@" | |
| return | |
| fi | |
| done | |
| "$EZA" "${opts[@]}" "$@" . | |
| fi | |
| } | |
| alias eza='better_eza' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment