From 2a6a32e92c89783812ff9d9bae8852e5a722eed4 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sun, 24 Feb 2019 12:11:51 +0100 Subject: [PATCH] Add bash aliases to force `ls` colored output This is useful when piping the output of `ls` into `less` or another pager. However, I added new aliases because making the default ones force color would cause problems when piping into other commands or directly to files, since the terminal escape characters would then clobber the data. --- dotfiles/.bashrc | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/dotfiles/.bashrc b/dotfiles/.bashrc index 532886a..16e66ba 100644 --- a/dotfiles/.bashrc +++ b/dotfiles/.bashrc @@ -81,16 +81,25 @@ EOF } bashrc_customize_aliases() { - # Group directories first if supported by the available `ls` command. - local ls_dir_group="" + # Make `ls` group directories first if supported. if ls --group-directories-first >/dev/null 2>&1; then - ls_dir_group="--group-directories-first" + alias ls="ls -hF --group-directories-first --color=auto" # GNU + else + alias ls="ls -hF -G" # BSD + fi + + # Force `ls` to use color output (e.g. for piping into `less`). + if ls --color=auto >/dev/null 2>&1; then + alias lsc="ls --color=always" # GNU + else + alias lsc="/usr/bin/env CLICOLOR_FORCE=1 ls" # BSD fi - alias ls="ls -hF --color=auto ${ls_dir_group}" alias la="ls -a" alias ll="ls -l" + alias llc="lsc -l" alias lla="ls -la" + alias llac="lsc -la" alias grep="grep --color=auto"; alias egrep="egrep --color=auto"; alias fgrep="fgrep --color=auto";