Dup Ver Goto 📝

my_aliases_and_functions

PT2/linux/shell bash bashrc linux gnu shell does not exist
To
429 lines, 1209 words, 8910 chars Page 'my_aliases_and_functions' does not exist.

See my bashrc.

I organise my bashrc stuff into .stuff files. Some of these define functions and aliases. (For reference, the command I use to compile the following is

for s in *.stuff; do echo -e "\`$s\`\n\`\`\`bash"; cat "$s"; echo -e '```' "\n"; done | pc

where pc is my os-specific 'copy to clipboard' command.

aliases.stuff

alias n='nvim'

antlr.stuff

export CLASSPATH=".:/usr/local/lib/antlr-4.11.1-complete.jar:$CLASSPATH"
alias antlr4='java -jar /usr/local/lib/antlr-4.11.1-complete.jar'
alias grun='java org.antlr.v4.gui.TestRig'

browser.stuff

export BROWSER=google-chrome

cd.stuff

alias cd..='cd ..'
alias cd...='cd ../..'
alias cd....='cd ../../..'
alias cd..3='cd ../../..'
alias cd..4='cd ../../../..'
alias cd..5='cd ../../../../..'
alias ..='cd ..'
alias ...='cd ../..'
alias ..3='cd ../../..'
alias ..4='cd ../../../..'
alias ..5='cd ../../../../..'
alias ls..='ls ..'
up() { local I="$1"; ((I=I)); while ((I>0)); do cd ..; ((I--)); done; }
pcwd() { pwd | pc; }
alias ppd=pcwd
cdpp() { cd "$(pp)"; }
cdt() { 
  local today="$(today)"
  echo "$today";
  if [ -d "$today" ]; then
    cd "$today"
  fi
}
mkt() {
  local today="$(today)"
  echo "$today";
  mkdir -p "$today"
}
today() { 
  local A="${1-/}"
  date +"%Y$A%m$A%d"
}

completion.stuff

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi
. tmx_completion.stuff
. newhtml_completion.stuff
complete -c viw catw batw lessw

conda.stuff

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/john/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/john/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/john/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/john/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

cygwin.stuff

# nothing to do on *nix

dircolors.stuff

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    eval "$(dircolors -b)"
    test -r /etc/dircolors.system && eval "$(dircolors -b /etc/dircolors.system)"
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)"
    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'
fi

dot_commands.stuff

alias .w=cmdwhich
cmdwhich() {
  # .w cat a b c # becomes cat $(which a) $(which b) $(which c)
  # .w head -n1 -- a b c # becomes head -n1 $(which a) $(which b) $(which c)
  # in amy case, switches are passed through if they don't match a command
  [ -z "$1" ] && { echo "cmdwhich CMD [cmd cmd ...] where CMD must be nonempty" ; return ; }
  local A=()
  local B=()
  local S=n
  local CMD="$1"
  shift
  for s; do
    #echo "s=$s"
    if [ "$s" = "--" ]; then
      echo "=="
      S=y
      B=("${A[@]}")
      A=()
      continue
    fi
    if [ "${s:0:1}" = "-" ]; then
      A+=("$s")
    else
      local W="$(which "$s")"
      if [ -x "$W" ]; then
        A+=("$W")
      else
        echo "$1 not found"
      fi
    fi
  done
  "$CMD" "${B[@]}" "${A[@]}"
}

filecmds.stuff

# I have rs aliased to "rsync -haux --progress" and on more than one occasion I've typed
# rm list-of-files-to-sync target:dir/
# and deleted what I wanted to sync. Making the remove command "remove" prevents this sort of accident.
alias remove=/bin/rm
rm() {
  echo "To delete for real, use"
  echo "remove $*"
}
mvt() {
  local T="$1"
  shift
  if [ ! -d "$T" ]; then
    echo "$T" is not a directory
    return 1
  fi
  mv -t "$T" "$@"
}

hist.stuff

histv() { shopt -s histverify; }
nohistv() { shopt -u histverify; }
histv
shopt -s histappend


# utility for putting markers in history
setmark() { if [ -n "$1" ]; then export MARK="$1"; elif [ -n "$MARK" ]; then echo "Mark: $MARK"; else echo "No mark defined"; fi; }
histmark() { 
  if [ -n "$MARK" ]; then
    history | awk "BEGIN { x = 0; } /===$MARK===/ { print \"Mark Found\n\"; x = 1; } { if( x == 1) print; }"
  else
    echo "No mark defined"
  fi
}
mark() {
  if [ -n "$1" ]; then setmark "$1"; fi
  if [ -n "$MARK" ]; then
    history -s "echo '===$MARK==='"
  else
    echo "No mark defined"
  fi
}
h() { history | grep "$@"; }

mpd.stuff

setmus() {
  local H="${1-t420b}" P="${2-6600}"
  if [ -n "$H" ]; then
    export MPD_HOST="$H"
    export MPD_PORT="$P"
  fi
}
setmus

mvdesc.stuff

findmnt_target() {
  tgt="$(findmnt -n -o target "$1")"
}
findmnt_of() {
  local a="$(readlink -f "$1")"
  local tgt
  findmnt_target "$a"
  while [ "$tgt" = "" ]; do
    a="$(dirname "$a")"
    findmnt_target "$a"
  done
  echo "$tgt"
}
mvdesc() {
  local r d mnt
  r="$(findmnt_of .)"
  if [ "$r" = "/" ]; then
    d="$HOME/descriptions"
  else
    d="$r/descriptions"
  fi
  echo "Moving descriptions to $d"
  if exists *.description; then
    mkdir -p "$d"
    mv -nv -t "$d" *.description 
  fi
}

my.stuff

if [ -f "$HOME/bin/my_stuff" ]; then
  . "$HOME/bin/my_stuff"
elif [ -d "$HOME/bin/my_stuff" ]; then
  for s in "$HOME/bin/my_stuff/*.stuff"; do
    . "$s"
  done
fi

newhtml_completion.stuff

#!/bin/bash
_newhtml_completions()
{
  COMPREPLY=()
  local CURWORD="${COMP_WORDS[COMP_CWORD]}"
  local WORDS=()
  while read -r line
  do
    WORDS+=("$line")
  done < <(newhtml -l)
  #echo "\n\nCURWORD=$CURWORD\nCWORD=$COMP_CWORD\nWORDS=${WORDS[@]}\n\n"
  if ((COMP_CWORD == 1)); then
    COMPREPLY=($(compgen -W "${WORDS[*]}" -- "$CURWORD"))
  else
    COMPREPLY=()
  fi
}
complete -F _newhtml_completions newhtml

nvm.stuff

NVM_DIR="$HOME/.nvm"
if [ -d "$HOME/.nvm" ]; then
  export NVM_DIR
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
  [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
else
  unset NVM_DIR
fi

pathif.stuff

pathif() { 
  local APPEND=n
  for s; do
    if [ "$s" = "-a" ]; then
      APPEND=y
    elif [ "$s" = "-p" ]; then
      APPEND=n
    fi
    if [ ! -d "$s" ]; then continue; fi
    if [ "$APPEND" = "y" ]; then
      PATH="$PATH:$s"
    else
      PATH="$s:$PATH"
    fi
  done
  local TMPATH="$PATH"
  [ -x /usr/local/bin/tidypath ] && PATH="$(/usr/local/bin/tidypath -c "$PATH")"
  export PATH
}

perl.stuff

unset PERL5LIB;
unset PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"/home/john/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/john/perl5"; export PERL_MM_OPT;

python.stuff

alias p='python'
alias pip='python -m pip'

shopt.stuff

shopt -s extglob
set -o noclobber
shopt -s histverify

stuff_completion.stuff

#!/bin/bash
_stuff_completions()
{
  COMPREPLY=()
  #echo
  #echo "COMP_WORDS=" "${COMP_WORDS[@]}"
  #echo "COMP_CWORD=" "${COMP_CWORD}"
  #echo "COMP_LINE=" "${COMP_LINE}"
  local CURWORD="${COMP_WORDS[COMP_CWORD]}"
  local WORDS=()
  while read -r line
  do
    WORDS+=("$line")
  done < <(cd /usr/jda/stuff; ls *.stuff | cut -f1 -d.)
  COMPREPLY=($(compgen -W "${WORDS[*]}" -- "$CURWORD"))
}
complete -F _stuff_completions .stuff
complete -F _stuff_completions .s

sudo.stuff

alias setfacl='sudo /usr/bin/setfacl'
alias getfacl='sudo /usr/bin/getfacl'
reload() { sudo systemctl reload "$@"; }

tmx_completion.stuff

#!/bin/bash
_tmx_completions()
{
  COMPREPLY=()
  local CURWORD="${COMP_WORDS[COMP_CWORD]}"
  local WORDS=()
  if tmux list-sessions >& /dev/null; then
    while read -r line
    do
      WORDS+=("$line")
    done < <(tmux list-sessions | cut -f1 -d:)
    COMPREPLY=($(compgen -W "${WORDS[*]}" -- "$CURWORD"))
  else
    echo "No tmux sessions"
  fi
}
complete -F _tmx_completions tmx

x11.stuff

x11() {
  local D="$1"
  if [ "$D" = "-x" ]; then
    unset DISPLAY
  elif [ -n "$D" ]; then
    export DISPLAY="$D":0
  else
    if [ -n "SSH_CLIENT" ]; then
      echo "Getting IP from SSH_CLIENT"
      IP="${SSH_CLIENT%% *}"
      echo "  IP=$IP"
      export DISPLAY="$IP:0"
    else
      echo "x11 <host> # to set display"
      echo "x11 -x # to unset display"
    fi
  fi
}

x11s() {
  local D="$1"
  if [ -n "SSH_CLIENT" ]; then
    echo "Getting IP from SSH_CLIENT"
    IP="${SSH_CLIENT%% *}"
    echo "  IP=$IP"
    export DISPLAY="$IP:$D"
  else
    echo "x11 <host> # to set display"
    echo "x11 -x # to unset display"
  fi
}

x11d() {
  local D="$1"
  if [[ $D =~ : ]]; then
    echo colon
  else
    echo no colon
  fi
}