config

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

bashrc (5606B)


      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=c23 -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="systemctl is-active --quiet keyd && doas systemctl stop keyd || doas systemctl start keyd"
     46 alias l="brightnessctl s"
     47 alias la="ls -A"
     48 alias less="less --RAW-CONTROL-CHARS"
     49 alias ll="ls -lhF --group-directories-first"
     50 alias lll="ls -alhF --group-directories-first"
     51 alias locks="i3lock -c ffffff -f; systemctl suspend"
     52 alias ls="ls --color=auto"
     53 alias lss="ls -lShr"
     54 alias mic="pactl set-source-mute @DEFAULT_SOURCE@ toggle"
     55 alias mkdir="mkdir -p"
     56 alias morning="redshift -x"
     57 alias mv="mv -i"
     58 alias night="redshift -P -O 1600"
     59 alias open="xdg-open"
     60 alias pacman="doas pacman"
     61 alias qd="diff --side-by-side --suppress-common-lines"
     62 alias rb=". ~/.bashrc"
     63 alias rgf='rg --files | rg'
     64 alias rm="rm -I"
     65 alias s="systemctl"
     66 alias scrcpy="scrcpy --stay-awake --turn-screen-off"
     67 alias se="sync-email"
     68 alias sp="tmux new-session -A -s scratch"
     69 alias t="tree -CL 1"
     70 alias tt="tree -CL 2"
     71 alias ttt="tree -CL 3"
     72 alias timers="systemctl --user -all list-timers"
     73 alias unlock="killall i3lock"
     74 alias unread="notmuch search tag:unread"
     75 alias wt="world-time"
     76 
     77 # Functions
     78 ## fd: find directories
     79 fd() {
     80   find . -type d -iname '*'"$*"'*' -ls ;
     81 }
     82 
     83 ## ff: find files
     84 ff() {
     85   find . -type f -iname '*'"$*"'*' -ls ;
     86 }
     87 
     88 repeat() {
     89     local c max
     90     max="$1"; shift;
     91     for ((c=1; c <= max ; c++)); do
     92         "$@";
     93     done
     94 }
     95 
     96 ts() {
     97   local session="$1"
     98   if [[ -z "$session" ]]; then
     99     session="main"
    100   fi
    101 
    102   tmux new-session -A -s "$session"
    103 }
    104 
    105 vterm_printf() {
    106     if [ -n "$TMUX" ] && ([ "${TERM%%-*}" = "tmux" ] || [ "${TERM%%-*}" = "screen" ]); then
    107         # Tell tmux to pass the escape sequences through
    108         printf "\ePtmux;\e\e]%s\007\e\\" "$1"
    109     elif [ "${TERM%%-*}" = "screen" ]; then
    110         # GNU screen (screen, screen-256color, screen-256color-bce)
    111         printf "\eP\e]%s\007\e\\" "$1"
    112     else
    113         printf "\e]%s\e\\" "$1"
    114     fi
    115 }
    116 
    117 if [[ "$INSIDE_EMACS" = 'vterm' ]]; then
    118     function clear() {
    119         vterm_printf "51;Evterm-clear-scrollback";
    120         tput clear;
    121     }
    122 fi
    123 
    124 # Options
    125 complete -cf doas
    126 
    127 set -o emacs
    128 set -o notify
    129 set -o noclobber
    130 
    131 shopt -s cdspell
    132 shopt -s checkjobs
    133 shopt -s checkwinsize
    134 shopt -s cmdhist
    135 shopt -s complete_fullquote
    136 shopt -s dirspell
    137 shopt -s dotglob
    138 shopt -s expand_aliases
    139 shopt -s extquote
    140 shopt -s force_fignore
    141 shopt -s histappend
    142 shopt -s histreedit
    143 shopt -s histverify
    144 shopt -s hostcomplete
    145 shopt -s interactive_comments
    146 shopt -s nullglob
    147 shopt -s progcomp
    148 shopt -s promptvars
    149 shopt -s sourcepath
    150 
    151 # Prompt
    152 timer_now() {
    153   date +%s%N
    154 }
    155 
    156 timer_start() {
    157   timer_start=${timer_start:-$(timer_now)}
    158 }
    159 
    160 timer_stop() {
    161   local delta_us=$((($(timer_now) - timer_start) / 1000))
    162   local us=$((delta_us % 1000))
    163   local ms=$(((delta_us / 1000) % 1000))
    164   local s=$(((delta_us / 1000000) % 60))
    165   local m=$(((delta_us / 60000000) % 60))
    166   local h=$((delta_us / 3600000000))
    167   # Goal: always show around 3 digits of accuracy
    168   if ((h > 0)); then timer_show=${h}h${m}m
    169   elif ((m > 0)); then timer_show=${m}m${s}s
    170   elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
    171   elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
    172   elif ((ms >= 100)); then timer_show=${ms}ms
    173   elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
    174   else timer_show=${us}us
    175   fi
    176   unset timer_start
    177 }
    178 
    179 set_prompt () {
    180   # First, get the exit code of the last command.
    181   last_command=$?
    182 
    183   # Stop the timer.
    184   timer_stop
    185 
    186   # Set the current UTC time.
    187   PS1="$bold_black$(date -u +"%H:%M:%S") "
    188 
    189   # Set the user; red if root, green otherwise.
    190   if [[ "$EUID" == 0 ]]; then
    191     PS1+="$bold_red\\u"
    192   else
    193     PS1+="$bold_green\\u"
    194   fi
    195 
    196   # Set the "@".
    197   PS1+="$bold_green@"
    198 
    199   # Set the host; green for local, yellow for SSH, red for unsecured connection.
    200   if [[ -n "${SSH_CONNECTION}" ]]; then
    201     PS1+="$bold_yellow\\H "
    202   elif [[ "${DISPLAY%%:0*}" != "" ]]; then
    203     PS1+="$bold_red\\H "
    204   else
    205     PS1+="$bold_green\\H "
    206   fi
    207 
    208   # Set the exit code.
    209   PS1+="$bold_black\$last_command "
    210 
    211   # Set a check mark for an exit code of 0 (success).
    212   # Otherwise, set an X mark.
    213   if [[ $last_command == 0 ]]; then
    214     PS1+="$bold_green$icon_checkmark "
    215   else
    216     PS1+="$bold_red$icon_xmark "
    217   fi
    218 
    219   # Set the elapsed time.
    220   # Inherit the color from the preceding mark.
    221   PS1+="($timer_show) "
    222 
    223   # Set the working directory and prompt marker.
    224   # Finally, reset to no color.
    225   PS1+="$bold_blue\\w \\\$$no_color "
    226 }
    227 
    228 trap "timer_start" DEBUG
    229 PROMPT_COMMAND="set_prompt"