From ce1c03f5a0a0e397269e1ecfc7d680cb3e8e3620 Mon Sep 17 00:00:00 2001 From: solo Date: Fri, 11 Feb 2022 09:25:58 -0500 Subject: [PATCH 1/3] lib/bourne-shell: Do not override user-defined aliases (Fix #304) Fixes #304 by checking if the alias already exists before applying it. --- lib/bourne-shell.sh | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/bourne-shell.sh b/lib/bourne-shell.sh index 0c1121a80..c5edf297a 100644 --- a/lib/bourne-shell.sh +++ b/lib/bourne-shell.sh @@ -60,13 +60,25 @@ esac # enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" - alias ls='ls --color=auto' - #alias dir='dir --color=auto' - #alias vdir='vdir --color=auto' - - alias grep='grep --color=auto' - alias fgrep='fgrep --color=auto' - alias egrep='egrep --color=auto' + if ! alias ls >/dev/null 2>&1; then + alias ls='ls --color=auto' + fi + #if ! alias dir >/dev/null 2>&1; then + # alias dir='dir --color=auto' + #fi + #if ! alias vdir; then + # alias vdir='vdir --color=auto' + #fi + + if ! alias grep >/dev/null 2>&1; then + alias grep='grep --color=auto' + fi + if ! alias fgrep >/dev/null 2>&1; then + alias fgrep='fgrep --color=auto' + fi + if ! alias egrep >/dev/null 2>&1; then + alias egrep='egrep --color=auto' + fi fi # colored GCC warnings and errors From 0fe8c0bf0df71be38a372ee98b59790bcc086d4b Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 3 Jul 2022 13:12:07 +0900 Subject: [PATCH 2/3] lib: support OMB_DEFAULT_ALIASES to control the alias overriding --- lib/bourne-shell.sh | 33 ++++++++---------------- lib/directories.sh | 48 +++++++++++++++++------------------ lib/grep.sh | 2 +- lib/misc.sh | 8 +++--- lib/utils.sh | 12 +++++++++ templates/bashrc.osh-template | 4 +++ 6 files changed, 55 insertions(+), 52 deletions(-) diff --git a/lib/bourne-shell.sh b/lib/bourne-shell.sh index c5edf297a..4f2ac109d 100644 --- a/lib/bourne-shell.sh +++ b/lib/bourne-shell.sh @@ -60,38 +60,25 @@ esac # enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" - if ! alias ls >/dev/null 2>&1; then - alias ls='ls --color=auto' - fi - #if ! alias dir >/dev/null 2>&1; then - # alias dir='dir --color=auto' - #fi - #if ! alias vdir; then - # alias vdir='vdir --color=auto' - #fi - - if ! alias grep >/dev/null 2>&1; then - alias grep='grep --color=auto' - fi - if ! alias fgrep >/dev/null 2>&1; then - alias fgrep='fgrep --color=auto' - fi - if ! alias egrep >/dev/null 2>&1; then - alias egrep='egrep --color=auto' - fi + _omb_util_alias ls='ls --color=auto' + #_omb_util_alias ls='dir --color=auto' + #_omb_util_alias vdir='vdir --color=auto' + _omb_util_alias grep='grep --color=auto' + _omb_util_alias fgrep='fgrep --color=auto' + _omb_util_alias egrep='egrep --color=auto' fi # colored GCC warnings and errors #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' # some more ls aliases -alias ll='ls -alF' -alias la='ls -A' -alias l='ls -CF' +_omb_util_alias ll='ls -alF' +_omb_util_alias la='ls -A' +_omb_util_alias l='ls -CF' # Add an "alert" alias for long running commands. Use like so: # sleep 10; alert -alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' +_omb_util_alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' # Alias definitions. # You may want to put all your additions into a separate file like diff --git a/lib/directories.sh b/lib/directories.sh index 54641176b..cd19bfccc 100644 --- a/lib/directories.sh +++ b/lib/directories.sh @@ -1,30 +1,30 @@ #! bash oh-my-bash.module # Common directories functions -alias cd..='cd ../' # Go back 1 directory level (for fast typers) -alias ..='cd ../' # Go back 1 directory level -alias ...='cd ../../' # Go back 2 directory levels -alias .3='cd ../../../' # Go back 3 directory levels -alias .4='cd ../../../../' # Go back 4 directory levels -alias .5='cd ../../../../../' # Go back 5 directory levels -alias .6='cd ../../../../../../' # Go back 6 directory levels +_omb_util_alias cd..='cd ../' # Go back 1 directory level (for fast typers) +_omb_util_alias ..='cd ../' # Go back 1 directory level +_omb_util_alias ...='cd ../../' # Go back 2 directory levels +_omb_util_alias .3='cd ../../../' # Go back 3 directory levels +_omb_util_alias .4='cd ../../../../' # Go back 4 directory levels +_omb_util_alias .5='cd ../../../../../' # Go back 5 directory levels +_omb_util_alias .6='cd ../../../../../../' # Go back 6 directory levels -alias -- -='cd -' -alias 1='cd -' -alias 2='cd -2' -alias 3='cd -3' -alias 4='cd -4' -alias 5='cd -5' -alias 6='cd -6' -alias 7='cd -7' -alias 8='cd -8' -alias 9='cd -9' +_omb_util_alias -='cd -' +_omb_util_alias 1='cd -' +_omb_util_alias 2='cd -2' +_omb_util_alias 3='cd -3' +_omb_util_alias 4='cd -4' +_omb_util_alias 5='cd -5' +_omb_util_alias 6='cd -6' +_omb_util_alias 7='cd -7' +_omb_util_alias 8='cd -8' +_omb_util_alias 9='cd -9' -alias md='mkdir -p' -alias rd='rmdir' -alias d='dirs -v | head -10' +_omb_util_alias md='mkdir -p' +_omb_util_alias rd='rmdir' +_omb_util_alias d='dirs -v | head -10' # List directory contents -alias lsa='ls -lah' -alias l='ls -lah' -alias ll='ls -lh' -alias la='ls -lAh' +_omb_util_alias lsa='ls -lah' +_omb_util_alias l='ls -lah' +_omb_util_alias ll='ls -lh' +_omb_util_alias la='ls -lAh' diff --git a/lib/grep.sh b/lib/grep.sh index a05ef2a9f..984e44e2c 100644 --- a/lib/grep.sh +++ b/lib/grep.sh @@ -23,7 +23,7 @@ fi # export grep settings if ((${#_omb_grep_options[@]} > 0)); then - alias grep="grep ${_omb_grep_options[*]}" + _omb_util_alias grep="grep ${_omb_grep_options[*]}" fi # clean up diff --git a/lib/misc.sh b/lib/misc.sh index f91e68f41..fb5782c66 100644 --- a/lib/misc.sh +++ b/lib/misc.sh @@ -7,14 +7,14 @@ env_default PAGER 'less' env_default LESS '-R' ## super user alias -alias _='sudo' -alias please='sudo' +_omb_util_alias _='sudo' +_omb_util_alias please='sudo' ## more intelligent acking for ubuntu users if which ack-grep &> /dev/null; then - alias afind='ack-grep -il' + _omb_util_alias afind='ack-grep -il' else - alias afind='ack -il' + _omb_util_alias afind='ack -il' fi # only define LC_CTYPE if undefined diff --git a/lib/utils.sh b/lib/utils.sh index 08233a73e..ae8623017 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -388,3 +388,15 @@ _omb_util_glob_expand() { [[ :$shopt: == *:failglob:* ]] && shopt -s failglob return 0 } + +_omb_util_alias() { + case ${OMB_DEFAULT_ALIASES:-enable} in + (disable) return 0 ;; + (check) alias -- "${1%%=*}" &>/dev/null && return 0 ;; + (enable) ;; + (*) + _omb_log_error "invalid value: OMB_DEFAULT_ALIASES='${OMB_DEFAULT_ALIASES-}' (expect: enable|disable|check)" >&2 + return 2 + esac + alias -- "$1" +} diff --git a/templates/bashrc.osh-template b/templates/bashrc.osh-template index 31ba8a1f3..bf3719588 100644 --- a/templates/bashrc.osh-template +++ b/templates/bashrc.osh-template @@ -46,6 +46,10 @@ OSH_THEME="font" # The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd" # HIST_STAMPS="mm/dd/yyyy" +# Uncomment the following line if you do not want OMB to overwrite the existing +# aliases by the default OMB aliases defined in lib/*.sh +# OMB_DEFAULT_ALIASES="check" + # Would you like to use another custom folder than $OSH/custom? # OSH_CUSTOM=/path/to/new-custom-folder From 3e441aa00b07294f81b4b293d618a14f101a3b78 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 3 Jul 2022 13:18:52 +0900 Subject: [PATCH 3/3] lib/bourne-shell: Remove duplicate aliases --- lib/bourne-shell.sh | 10 +++------- lib/directories.sh | 11 ++++++++--- lib/grep.sh | 2 ++ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/bourne-shell.sh b/lib/bourne-shell.sh index 4f2ac109d..c45e2f88f 100644 --- a/lib/bourne-shell.sh +++ b/lib/bourne-shell.sh @@ -63,18 +63,14 @@ if [ -x /usr/bin/dircolors ]; then _omb_util_alias ls='ls --color=auto' #_omb_util_alias ls='dir --color=auto' #_omb_util_alias vdir='vdir --color=auto' - _omb_util_alias grep='grep --color=auto' - _omb_util_alias fgrep='fgrep --color=auto' - _omb_util_alias egrep='egrep --color=auto' + + # Note: aliases "grep", "fgrep", and "egrep" are merged in lib/grep.sh fi # colored GCC warnings and errors #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' -# some more ls aliases -_omb_util_alias ll='ls -alF' -_omb_util_alias la='ls -A' -_omb_util_alias l='ls -CF' +# ls aliases are moved to "lib/directories.sh" # Add an "alert" alias for long running commands. Use like so: # sleep 10; alert diff --git a/lib/directories.sh b/lib/directories.sh index cd19bfccc..7be070247 100644 --- a/lib/directories.sh +++ b/lib/directories.sh @@ -24,7 +24,12 @@ _omb_util_alias rd='rmdir' _omb_util_alias d='dirs -v | head -10' # List directory contents -_omb_util_alias lsa='ls -lah' -_omb_util_alias l='ls -lah' +_omb_util_alias lsa='ls -lha' +_omb_util_alias l='ls -lha' _omb_util_alias ll='ls -lh' -_omb_util_alias la='ls -lAh' +_omb_util_alias la='ls -lhA' + +# From bourne-shell.sh (unused) +#_omb_util_alias l='ls -CF' +#_omb_util_alias ll='ls -laF' +#_omb_util_alias la='ls -A' diff --git a/lib/grep.sh b/lib/grep.sh index 984e44e2c..f551ac1b4 100644 --- a/lib/grep.sh +++ b/lib/grep.sh @@ -24,6 +24,8 @@ fi # export grep settings if ((${#_omb_grep_options[@]} > 0)); then _omb_util_alias grep="grep ${_omb_grep_options[*]}" + _omb_util_alias grep="fgrep ${_omb_grep_options[*]}" + _omb_util_alias grep="egrep ${_omb_grep_options[*]}" fi # clean up