config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

bashrc (5416B)


      1 # Ignore non-interactive shells.
      2 [[ $- != *i* ]] && return
      3 
      4 # Variables
      5 black="\[\e[0;30m\\]"
      6 red="\[\e[0;31m\\]"
      7 green="\[\e[0;32m\\]"
      8 yellow="\[\e[0;33m\\]"
      9 blue="\[\e[0;34m\\]"
     10 magenta="\[\e[0;35m\\]"
     11 cyan="\[\e[0;36m\\]"
     12 white="\[\e[0;37m\\]"
     13 
     14 bold_black="\[\e[01;30m\\]"
     15 bold_red="\[\e[01;31m\\]"
     16 bold_green="\[\e[01;32m\\]"
     17 bold_yellow="\[\e[01;33m\\]"
     18 bold_blue="\[\e[01;34m\\]"
     19 bold_magenta="\[\e[01;35m\\]"
     20 bold_cyan="\[\e[01;36m\\]"
     21 bold_white="\[\e[01;37m\\]"
     22 
     23 icon_checkmark="\342\234\223"
     24 icon_xmark="\342\234\227"
     25 no_color="\[\e[00m\\]"
     26 
     27 # Aliases
     28 alias adb="HOME=\"${XDG_DATA_HOME}\"/android adb"
     29 alias aoeu="setxkbmap us"
     30 alias asdf="setxkbmap dvorak; xmodmap ~/.config/X11/xmodmap"
     31 alias cal="cal --monday --iso --week"
     32 alias caps="xdotool key Caps_Lock"
     33 alias cp="cp -i"
     34 alias cpwd="pwd | tr -d '\n' | xclip"
     35 alias d="datetime"
     36 alias df="df -h"
     37 alias diff="diff --color=auto"
     38 alias dirdu="du -cksh * | sort -hr"
     39 alias e="emacsclient -t"
     40 alias gcc="gcc -O3 -Wall -Wextra -Wstrict-prototypes -std=c17 -pedantic"
     41 alias grep="grep --color=auto"
     42 alias hist="history -a && history -n"
     43 alias hr='printf $(printf "\e[91;1m%%%ds\e[0m\n" $(tput cols)) | tr " " ='
     44 alias j="journalctl"
     45 alias km="xmodmap ~/.config/X11/xmodmap"
     46 alias l="brightnessctl s"
     47 alias la="ls -A"
     48 alias ll="ls -lhF --group-directories-first"
     49 alias lll="ls -alhF --group-directories-first"
     50 alias locks="i3lock -c ffffff -f; systemctl suspend"
     51 alias ls="ls --color=auto"
     52 alias lss="ls -lShr"
     53 alias mic="pactl set-source-mute @DEFAULT_SOURCE@ toggle"
     54 alias mkdir="mkdir -p"
     55 alias mv="mv -i"
     56 alias open="xdg-open"
     57 alias pacman="doas pacman"
     58 alias qd="diff --side-by-side --suppress-common-lines"
     59 alias rb=". ~/.bashrc"
     60 alias rgf='rg --files | rg'
     61 alias rm="rm -I"
     62 alias s="systemctl"
     63 alias scrcpy="scrcpy --stay-awake --turn-screen-off"
     64 alias se="sync-email"
     65 alias sp="tmux new-session -A -s scratch"
     66 alias t="tree -CL 1"
     67 alias tt="tree -CL 2"
     68 alias ttt="tree -CL 3"
     69 alias timers="systemctl --user -all list-timers"
     70 alias unread="notmuch search tag:unread"
     71 alias wt="world-time"
     72 
     73 # Functions
     74 ## fd: find directories
     75 fd() {
     76   find . -type d -iname '*'"$*"'*' -ls ;
     77 }
     78 
     79 ## ff: find files
     80 ff() {
     81   find . -type f -iname '*'"$*"'*' -ls ;
     82 }
     83 
     84 repeat() {
     85     local c max
     86     max="$1"; shift;
     87     for ((c=1; c <= max ; c++)); do
     88         "$@";
     89     done
     90 }
     91 
     92 ts() {
     93   local session="$1"
     94   if [[ -z "$session" ]]; then
     95     session="main"
     96   fi
     97 
     98   tmux new-session -A -s "$session"
     99 }
    100 
    101 vterm_printf() {
    102     if [ -n "$TMUX" ] && ([ "${TERM%%-*}" = "tmux" ] || [ "${TERM%%-*}" = "screen" ]); then
    103         # Tell tmux to pass the escape sequences through
    104         printf "\ePtmux;\e\e]%s\007\e\\" "$1"
    105     elif [ "${TERM%%-*}" = "screen" ]; then
    106         # GNU screen (screen, screen-256color, screen-256color-bce)
    107         printf "\eP\e]%s\007\e\\" "$1"
    108     else
    109         printf "\e]%s\e\\" "$1"
    110     fi
    111 }
    112 
    113 if [[ "$INSIDE_EMACS" = 'vterm' ]]; then
    114     function clear() {
    115         vterm_printf "51;Evterm-clear-scrollback";
    116         tput clear;
    117     }
    118 fi
    119 
    120 # Options
    121 complete -cf doas
    122 
    123 set -o emacs
    124 set -o notify
    125 set -o noclobber
    126 
    127 shopt -s cdspell
    128 shopt -s checkjobs
    129 shopt -s checkwinsize
    130 shopt -s cmdhist
    131 shopt -s complete_fullquote
    132 shopt -s dirspell
    133 shopt -s dotglob
    134 shopt -s expand_aliases
    135 shopt -s extquote
    136 shopt -s force_fignore
    137 shopt -s histappend
    138 shopt -s histreedit
    139 shopt -s histverify
    140 shopt -s hostcomplete
    141 shopt -s interactive_comments
    142 shopt -s nullglob
    143 shopt -s progcomp
    144 shopt -s promptvars
    145 shopt -s sourcepath
    146 
    147 # Prompt
    148 timer_now() {
    149   date +%s%N
    150 }
    151 
    152 timer_start() {
    153   timer_start=${timer_start:-$(timer_now)}
    154 }
    155 
    156 timer_stop() {
    157   local delta_us=$((($(timer_now) - timer_start) / 1000))
    158   local us=$((delta_us % 1000))
    159   local ms=$(((delta_us / 1000) % 1000))
    160   local s=$(((delta_us / 1000000) % 60))
    161   local m=$(((delta_us / 60000000) % 60))
    162   local h=$((delta_us / 3600000000))
    163   # Goal: always show around 3 digits of accuracy
    164   if ((h > 0)); then timer_show=${h}h${m}m
    165   elif ((m > 0)); then timer_show=${m}m${s}s
    166   elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
    167   elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
    168   elif ((ms >= 100)); then timer_show=${ms}ms
    169   elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
    170   else timer_show=${us}us
    171   fi
    172   unset timer_start
    173 }
    174 
    175 set_prompt () {
    176   # First, get the exit code of the last command.
    177   last_command=$?
    178 
    179   # Stop the timer.
    180   timer_stop
    181 
    182   # Set the current UTC time.
    183   PS1="$bold_black$(date -u +"%H:%M:%S") "
    184 
    185   # Set the user; red if root, green otherwise.
    186   if [[ "$EUID" == 0 ]]; then
    187     PS1+="$bold_red\\u"
    188   else
    189     PS1+="$bold_green\\u"
    190   fi
    191 
    192   # Set the "@".
    193   PS1+="$bold_green@"
    194 
    195   # Set the host; green for local, yellow for SSH, red for unsecured connection.
    196   if [[ -n "${SSH_CONNECTION}" ]]; then
    197     PS1+="$bold_yellow\\H "
    198   elif [[ "${DISPLAY%%:0*}" != "" ]]; then
    199     PS1+="$bold_red\\H "
    200   else
    201     PS1+="$bold_green\\H "
    202   fi
    203 
    204   # Set the exit code.
    205   PS1+="$bold_black\$last_command "
    206 
    207   # Set a check mark for an exit code of 0 (success).
    208   # Otherwise, set an X mark.
    209   if [[ $last_command == 0 ]]; then
    210     PS1+="$bold_green$icon_checkmark "
    211   else
    212     PS1+="$bold_red$icon_xmark "
    213   fi
    214 
    215   # Set the elapsed time.
    216   # Inherit the color from the preceding mark.
    217   PS1+="($timer_show) "
    218 
    219   # Set the working directory and prompt marker.
    220   # Finally, reset to no color.
    221   PS1+="$bold_blue\\w \\\$$no_color "
    222 }
    223 
    224 trap "timer_start" DEBUG
    225 PROMPT_COMMAND="set_prompt"