view .bashrc @ 951:cd7f2ae9c8fd default tip

Replace TERM=mintty with "xterm" as remote Debian doesn't have "mintty" definition.
author Oleksandr Gavenko <gavenkoa@gmail.com>
date Sat, 21 Nov 2020 20:40:18 +0200
parents 6f2350c15506
children
line wrap: on
line source


# set -x
# set -u

# XXX
# [[ -f /etc/bashrc ]] && . /etc/bashrc

if [[ -f ~/.env ]]; then
  . ~/.env
fi

shopt -s checkwinsize
shopt -s promptvars

_my_ruler_handler() {
  local ruler='================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================'
  if [[ -x /usr/bin/tput ]]; then
    local size=`/usr/bin/tput cols`
  elif [[ -x /bin/stty ]]; then
    local size=`/bin/stty size | { read x y; echo $y; }`
  elif [[ -x /usr/bin/stty ]]; then
    local size=`/usr/bin/stty size | { read x y; echo $y; }`
  else
    local size=80
  fi
  ruler=${ruler:0:${size}}
  _my_ruler=$ruler
}
_my_ruler_handler
trap '_my_ruler_handler' SIGWINCH

PS1='\n${_my_ruler}\r$?|\u@\h \w \nbash# '
PS2='> '

case "$TERM" in
  xterm*|eterm-color|screen|linux|mintty*|cygwin)
    PS1=$'\n\e[34m\e[1m${_my_ruler}\r$?|\e[31m\u\e[35m@\h\e[0m\e[36m \w \n\[\e[1m\e[31m\]bash#\[\e[0m\] '
    PS2=$'\[\e[32m\]> \[\e[0m\]'
  ;;
esac
# Set current directory in terminal emulator title.
case "$TERM" in
  xterm*|screen|linux|mintty*|cygwin)
    PS1=$'\e]0;bash \u@\h \w\a'"$PS1"
  ;;
esac

if command -v dircolors >/dev/null 2>&1 && [[ -f ~/.dircolors ]]; then
  eval "`dircolors -b ~/.dircolors`"
fi

# Can be XXX /visible. I like bell.
# set bell-style visible

# Don't store duplicate adjacent items in the history.
#   ignorespace     do not save lines that start with space
#   erasedups       all previous lines matching the current line to be removed from
#                   the history list before that line is saved
HISTCONTROL=ignorespace:erasedups

# Don't store noisy/dumb items.
# TIP: MC command start with space.
HISTIGNORE=\
" cd *":\
"vlc *":ls:pwd

# To these options assigned default value, as they satisfy my needs I comment them.
# HISTFILE=~/.bash_history  # where is command history stored
# HISTFILESIZE=500          # how many lines been in $HISTFILE
# HISTSIZE=500              # how many lines been stored in bash process

HISTTIMEFORMAT="%F %T "

# Make Bash append rather than overwrite the history.
shopt -s histappend

shopt -s histreedit
shopt -s histverify

# Technique to share command history across Bash secctions.
# Just run:
#   history -n
# to reload history.
# This setting automatically update history file, but ignore HISTCONTROL=erasedups settings.
# PROMPT_COMMAND='history -a'

# Minor errors in the spelling of a directory component in a cd command will
# be corrected.
shopt -s cdspell

# Save all lines of a multiple-line command in the same history entry.
shopt -s cmdhist

# Allows files beginning with a dot ('.') to be returned in the results of path-name expansion.
shopt -s dotglob

# ksh-88 egrep-style extended pattern matching.
shopt -s extglob

# Enable '**' patttern matching. If the pattern is followed by a /, only dirs match.
shopt -s globstar

# Ignore case in glob.
shopt -s nocaseglob

# Show stopped jobs before exit. Next ^D force to leave bash...
shopt -s checkjobs

# Let me have core dumps
ulimit -c unlimited

################################################################
# shortcut aliases

alias ..="cd .."        #go to parent dir
alias ...="cd ../.."    #go to grandparent dir
alias -- -="cd -"       # go to previous dir
alias l.='ls -d .*'     #list hidden files
alias ll='ls -lhrt'     #extra info compared to "l"
alias lld='ls -lUd */'  #list directories
# I prefer EN man pages.
alias man='LANG=C man'

alias j='jobs '

# make and change to a directory
md () { mkdir -p "$1" && cd "$1"; }

myfind() { find . -iname "$1"; }

# Usage example:
#  mypath EMACSLOADPATH '*.el' \;
#  mypath TEXINPUTS "*cyr*"
#  VAR=`cygpath -p '$INCLUDE'` mypath VAR "windows*"
mypath() {
  local var="$2"
  [[ -z $var ]] && var=PATH
  local delim="$3"
  [[ -z $delim ]] && delim=:
  local split
  IFS="$delim" command eval "read -ra split <<< \"\${$var}\""
  for dir in "${split[@]}"; do
    [[ -z "$dir" ]] && continue
    if [[ -f "$dir"/"$1" ]]; then
      echo "$dir"/"$1"
    else
      ls -d "$dir"/$1 2>/dev/null | cat
    fi
  done
}
_mypath() {
  if [[ $COMP_CWORD = 2 ]]; then
    COMPREPLY=( $(compgen -v -- "${COMP_WORDS[2]}") )
  fi
}
complete -F _mypath mypath

# LANG=C for speed.
alias grep='GREP_COLOR="31;47" LANG=C grep --color=auto'
case $OSTYPE in
  linux*|cygwin) alias ls='ls --color=auto' ;;
esac
alias minicom='minicom -c on'

# Weeks start on Monday.
alias ncal='ncal -M'

# alias diff='LC_ALL=C TZ=GMT0 diff -Naur' #normalise diffs for distribution
# alias sudo='sudo env PATH=$PATH' #work around sudo built --with-secure-path (ubuntu)
# alias vim='vim -X' #don't try to contact xserver (which can hang on network issues)
# alias gdb='gdb -tui' #enable the text window interface if possible

alias dquilt="quilt --quiltrc=${HOME}/.quiltrc-dpkg"

# what most people want from od (hexdump)
# alias hd='od -Ax -tx1z -v'

case $OSTYPE in
  # CYGWIN=noglob causes https://github.com/docker/machine/issues/4516
  cygwin) alias docker-machine='CYGWIN= docker-machine' ;;
esac
case $TERM in
  mintty*) alias ssh='TERM=xterm ssh' ;;
esac

# --blank-lines-after-procedures --break-before-boolean-operator --braces-on-if-line --braces-on-struct-decl-line
# --comment-indentationn --space-after-cast --line-comments-indentationn --declaration-indentationn --honour-newlines
# --no-space-after-function-call-names --no-tabs --struct-brace-indentationn --start-left-side-of-comments
alias indent-bifit="indent -bap -bbo -br -brs -cdw -ce -cli0 -c0 -cs -d0 -di0 -hnl -l120 -lc90 -i4 -npcs -ppi 3 -npsl -nut -sbi4 -sc"

# canonicalize path (including resolving symlinks)
# alias realpath='readlink -f'

# Fix lang under X.
alias en="dbus-send --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.set_layout uint32:0"
alias ут="dbus-send --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.set_layout uint32:0"

################################################################
# Key binding.

set -o emacs

################################################################
# Completion.

if [[ -f /etc/bash_completion ]]; then
  # in Debian
  . /etc/bash_completion
  complete -F _quilt_completion dquilt
elif [[ -f /usr/share/bash-completion/bash_completion ]]; then
  # in Cygwin
  . /usr/share/bash-completion/bash_completion
elif [[ -f ~/usr/etc/bash_completion ]]; then
  . ~/usr/etc/bash_completion
fi
# Load local completions.
if [[ -f ~/.bash_completion ]]; then
  . ~/.bash_completion
fi
if [[ -d ~/.bash_completion.d ]]; then
  for f in ~/.bash_completion.d/* ~/.bash_completion.d/.*; do
    [[ -f "$f" ]] && . "$f"
  done
fi

################################################################
# Load custom settings (user editable).
# Placed to the end to allow override skel settings.

if [[ -f ~/.env.local ]]; then
  . ~/.env.local
fi

################################################################
# Useful functions.

datediff() {
  [[ -n "$1" ]] || { echo first argument is missing; return 1; }
  [[ -n "$2" ]] || { echo second argument is missing; return 1; }
  local diff=$(( $(date -d "$1" +%s) - $(date -d "$2" +%s) ))
  local sec=$(( diff % 60 ))
  diff=$(( diff / 60 ))
  local min=$(( diff % 60 ))
  diff=$(( diff / 60 ))
  local hour=$(( diff % 24 ))
  local day=$(( diff / 24 ))
  echo $day days $hour hours $min minutes $sec seconds
}

# For linguistic analysis.
myfreq() {
  ag --nofilename -C1 -- "$1"
  printf '\n\n\e[31mtotal:\e[0m '
  ag --nofilename --count -- "$1" | awk '{c += $1} END {print c}'
}

mygit-date() {
  if [[ -n "$2" ]]; then
    echo Provide only one arg.
    return 1;
  fi
  local sec=$(($RANDOM % 60))
  if [[ ${#sec} = 1 ]]; then
      sec=0$sec
  fi
  if [[ "$1" =~ ^[0-9]{2}:[0-9]{2}$ ]]; then
      local yyyymmdd=`date +%F`
      set "$yyyymmdd $1:$sec"
  elif [[ "$1" =~ ^[0-9]{2}' '[0-9]{2}:[0-9]{2}$ ]]; then
      local yyyymm=`date +%Y-%m`
      set "$yyyymm-$1:$sec"
  elif [[ "$1" =~ ^[0-9]{2}-[0-9]{2}' '[0-9]{2}:[0-9]{2}$ ]]; then
      local yyyy=`date +%Y`
      set "$yyyy-$1:$sec"
  elif [[ "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}' '[0-9]{2}:[0-9]{2}$ ]]; then
      set "$1:$sec"
  elif [[ "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}' '[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
      :
  else
    echo 'Unsupported format, try hh:mm, "dd hh:mm", "MM-dd hh:mm", "yyyy-MM-dd hh:mm", "yyyy-MM-dd hh:mm:ss"'
    return 1;
  fi
  echo GIT_COMMITTER_DATE='"'"$1"'"' git commit --amend --no-edit --date '"'"$1"'"'
  GIT_COMMITTER_DATE="$1" git commit --amend --no-edit --date "$1"
}

# https://stackoverflow.com/questions/63794347/find-every-decoration-after-given-git-commit
mygit-descendants() {
  git log --graph --decorate --oneline  --simplify-by-decoration ^"$1" $(
    git branch --all --contains "$1" --format '%(objectname)';
    git tag --contains "$1" --format '%(objectname)';
      );
}

mywsl-docker-enable() {
  if [[ -z "$1" ]]; then
    set default
  fi
  while IFS= read -r line; do
    if [[ ! "$line" =~ ^export ]]; then
      continue
    fi
    if [[ "$line" =~ DOCKER_CERT_PATH ]]; then
      local p="${line#*=}"                  # Strip leading 'export var='.
      p="${p#\"}"                           # Strip leading ".
      p="${p%\"}"                           # Strip final ".
      export DOCKER_CERT_PATH=$(wslpath -u $p)
      export DOCKER_1=123
    else
      eval "$line"
    fi
  done < <(docker-machine.exe env --shell bash "$1")
}