From b25aa8dffb08202056505ad9694dda76322c67ed Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 15 Oct 2022 21:22:42 -0400 Subject: [PATCH] zsh: fully committed to performing shell crimes --- zsh/.config/zsh/config.d/00_detect_remote.zsh | 8 ++ zsh/.config/zsh/config.d/10_aliases.zsh | 20 +++ zsh/.config/zsh/config.d/10_completions.zsh | 18 +++ zsh/.config/zsh/config.d/10_history.zsh | 6 + zsh/.config/zsh/config.d/10_wsl_display.zsh | 5 + .../zsh/config.d/20_named_directories.zsh | 6 + zsh/.config/zsh/config.d/30_prompt.zsh | 12 ++ zsh/.config/zsh/config.d/30_rprompt.zsh | 42 ++++++ zsh/.zshrc | 124 +----------------- 9 files changed, 122 insertions(+), 119 deletions(-) create mode 100644 zsh/.config/zsh/config.d/00_detect_remote.zsh create mode 100644 zsh/.config/zsh/config.d/10_aliases.zsh create mode 100644 zsh/.config/zsh/config.d/10_completions.zsh create mode 100644 zsh/.config/zsh/config.d/10_history.zsh create mode 100644 zsh/.config/zsh/config.d/10_wsl_display.zsh create mode 100644 zsh/.config/zsh/config.d/20_named_directories.zsh create mode 100644 zsh/.config/zsh/config.d/30_prompt.zsh create mode 100644 zsh/.config/zsh/config.d/30_rprompt.zsh diff --git a/zsh/.config/zsh/config.d/00_detect_remote.zsh b/zsh/.config/zsh/config.d/00_detect_remote.zsh new file mode 100644 index 0000000..5f83d78 --- /dev/null +++ b/zsh/.config/zsh/config.d/00_detect_remote.zsh @@ -0,0 +1,8 @@ +# Detect if the current session is running on a remote server +# Runs first because multiple other things rely on this +if [ -n "$IS_REMOTE_SESSION" ] || [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then + is_remote=1 +else + is_remote= +fi +export IS_REMOTE_SESSION=$is_remote diff --git a/zsh/.config/zsh/config.d/10_aliases.zsh b/zsh/.config/zsh/config.d/10_aliases.zsh new file mode 100644 index 0000000..a2cffe8 --- /dev/null +++ b/zsh/.config/zsh/config.d/10_aliases.zsh @@ -0,0 +1,20 @@ +# Aliases for color support +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' + alias diff='diff --color=auto' +fi + +# Functional aliases +alias ll='ls -laFh' +alias la='ls -aFh' +alias l='ls -CFh' +alias clip='xclip -selection clipboard' +alias copy='clip' +alias code='code -r' +function mkcd { mkdir -p $1 && cd $1 } diff --git a/zsh/.config/zsh/config.d/10_completions.zsh b/zsh/.config/zsh/config.d/10_completions.zsh new file mode 100644 index 0000000..0d1eb46 --- /dev/null +++ b/zsh/.config/zsh/config.d/10_completions.zsh @@ -0,0 +1,18 @@ +# Completion settings +# The following lines were added by compinstall + +zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate +zstyle ':completion:*' format '%B%U%d%u%b' +zstyle ':completion:*' group-name '' +zstyle ':completion:*' insert-unambiguous true +zstyle ':completion:*' list-colors ${(@s.:.)LS_COLORS} +zstyle ':completion:*' matcher-list '+' '+m:{[:lower:]}={[:upper:]}' '+r:|[._-]=** r:|=**' '+' +zstyle ':completion:*' max-errors 3 +zstyle ':completion:*' menu select=0 yes=0 +zstyle ':completion:*' select-prompt '%SScroll to view more completions (%l, %p)%s' +zstyle ':completion:*' verbose true +zstyle :compinstall filename '/home/erin/.zshrc' + +autoload -Uz compinit +compinit +# End of lines added by compinstall diff --git a/zsh/.config/zsh/config.d/10_history.zsh b/zsh/.config/zsh/config.d/10_history.zsh new file mode 100644 index 0000000..cd86b1e --- /dev/null +++ b/zsh/.config/zsh/config.d/10_history.zsh @@ -0,0 +1,6 @@ +# History management +HISTSIZE=1000 +SAVEHIST=1000 +HISTFILE="$HOME/.zhistory" +setopt INC_APPEND_HISTORY +setopt HIST_IGNORE_DUPS diff --git a/zsh/.config/zsh/config.d/10_wsl_display.zsh b/zsh/.config/zsh/config.d/10_wsl_display.zsh new file mode 100644 index 0000000..6134d72 --- /dev/null +++ b/zsh/.config/zsh/config.d/10_wsl_display.zsh @@ -0,0 +1,5 @@ +# If on WSL, define DISPLAY with the local system address from /etc/resolv.conf +# to enable GUI applications via an X server running under Windows +if [ -n "$WSL_DISTRO_NAME" ]; then + export DISPLAY="$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0" +fi diff --git a/zsh/.config/zsh/config.d/20_named_directories.zsh b/zsh/.config/zsh/config.d/20_named_directories.zsh new file mode 100644 index 0000000..6dbe370 --- /dev/null +++ b/zsh/.config/zsh/config.d/20_named_directories.zsh @@ -0,0 +1,6 @@ +# Set named directories to shorten path display on non-remote systems +if [ -z "$is_remote" ]; then + hash -d gh=~/src/github.com + hash -d gl=~/src/gitlab.com + hash -d gist=~/src/gist.github.com +fi diff --git a/zsh/.config/zsh/config.d/30_prompt.zsh b/zsh/.config/zsh/config.d/30_prompt.zsh new file mode 100644 index 0000000..175413e --- /dev/null +++ b/zsh/.config/zsh/config.d/30_prompt.zsh @@ -0,0 +1,12 @@ +# Prompt stuff +setopt PROMPT_SUBST + +# vcs_info - used for displaying VCS information in prompt +zstyle ':vcs_info:*' actionformats '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f ' +zstyle ':vcs_info:*' formats '(%b) ' +zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r' +autoload -Uz vcs_info +precmd_functions+=vcs-info + +# Prompt shows hostname if connected to a remote or if root +PROMPT="%B$([ -n "$is_remote" ] || [ "$EUID" = 0 ] && echo "%F{magenta}%m%f ")%F{$([ "$EUID" = 0 ] && echo "red" || echo "cyan")}%n%f %F{blue}%~%f%b \${vcs_info_msg_0_}%B%#%b " diff --git a/zsh/.config/zsh/config.d/30_rprompt.zsh b/zsh/.config/zsh/config.d/30_rprompt.zsh new file mode 100644 index 0000000..3d5fa1b --- /dev/null +++ b/zsh/.config/zsh/config.d/30_rprompt.zsh @@ -0,0 +1,42 @@ +# Hook preexec/precmd to dynamically set rprompt with useful info +function preexec { + # Store command execution start time + timer="${timer:-"$(date +%s.%N)"}" +} +function precmd { + vcs_info + # Start with a fresh prompt + RPROMPT="" + some_line="" + if [ -z $new_session ]; then + # If this is the first prompt in a session, there's not much to say + RPROMPT="%B%F{cyan}new session%f%b" + new_session=1 + else + # Show exit code + more info about the last command run as a separate line + some_line="exited %B%(?.%F{green}%?%f.%F{red}%?%f)%b" + if [ -z $timer ]; then + # If we never set $timer, preexec was never run (e.g. ^C at prompt) + some_line="$(print -P "%B%F{cyan}no exec%f%b, $some_line")" + else + # Calculate elapsed real time for last command + now="$(date +%s.%N)" + if [[ $(($now - $timer)) > 0.5 ]]; then + timer_show=$(($now - $timer)) + timer_show=$(printf '%.*f\n' 3 $timer_show) + some_line="took %B%F{cyan}${timer_show}s%f%b, $some_line" + fi + fi + fi + + # add current time, including timezone if remote, and collapse it if we're out of room + RPROMPT="%<#<$RPROMPT at %D{%T$([ -n "$is_remote" ] && echo ' %Z')}" + + # that's an RPROMPT! + export RPROMPT + + # always unset $timer for the next run + unset timer +} +preexec_functions+=preexec +precmd_functions+=precmd diff --git a/zsh/.zshrc b/zsh/.zshrc index 20612b8..7771c33 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -1,122 +1,8 @@ -# Completion settings -# The following lines were added by compinstall - -zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate -zstyle ':completion:*' format '%B%U%d%u%b' -zstyle ':completion:*' group-name '' -zstyle ':completion:*' insert-unambiguous true -zstyle ':completion:*' list-colors ${(@s.:.)LS_COLORS} -zstyle ':completion:*' matcher-list '+' '+m:{[:lower:]}={[:upper:]}' '+r:|[._-]=** r:|=**' '+' -zstyle ':completion:*' max-errors 3 -zstyle ':completion:*' menu select=0 yes=0 -zstyle ':completion:*' select-prompt '%SScroll to view more completions (%l, %p)%s' -zstyle ':completion:*' verbose true -zstyle :compinstall filename '/home/erin/.zshrc' - -autoload -Uz compinit -compinit -# End of lines added by compinstall - -# History management -HISTSIZE=1000 -SAVEHIST=1000 -HISTFILE="$HOME/.zhistory" -setopt INC_APPEND_HISTORY -setopt HIST_IGNORE_DUPS - -# If on WSL, define DISPLAY with the local system address from /etc/resolv.conf -# to enable GUI applications via an X server running under Windows -if [ -n "$WSL_DISTRO_NAME" ]; then - export DISPLAY="$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0" -fi - -# Aliases for color support -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' - alias diff='diff --color=auto' -fi - -# Functional aliases -alias ll='ls -laFh' -alias la='ls -aFh' -alias l='ls -CFh' -alias clip='xclip -selection clipboard' -alias copy='clip' -alias code='code -r' -function mkcd { mkdir -p $1 && cd $1 } - -# Set named directories to shorten path display on non-remote systems -if [ -z "$is_remote" ]; then - hash -d gh=~/src/github.com - hash -d gl=~/src/gitlab.com - hash -d gist=~/src/gist.github.com -fi - -# Prompt stuff -setopt PROMPT_SUBST - -# Detect if the current session is running on a remote server -if [ -n "$IS_REMOTE_SESSION" ] || [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then - is_remote=1 -else - is_remote= -fi -export IS_REMOTE_SESSION=$is_remote - -# Function to get the current branch name -function parse_git_branch { - git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' -e 's/((/(/' -e 's/))/)/' -} - -# Prompt shows hostname if connected to a remote or if root -PROMPT="%B$([ -n "$is_remote" ] || [ "$EUID" = 0 ] && echo "%F{magenta}%m%f ")%F{$([ "$EUID" = 0 ] && echo "red" || echo "cyan")}%n%f %F{blue}%~%f%b\$(parse_git_branch) %B%#%b " - -# Hook preexec/precmd to dynamically set rprompt with useful info -function preexec { - # Store command execution start time - timer="${timer:-"$(date +%s.%N)"}" -} -function precmd { - # Start with a fresh prompt - RPROMPT="" - if [ -z $new_session ]; then - # If this is the first prompt in a session, there's not much to say - RPROMPT="%B%F{cyan}new session%f%b" - new_session=1 - else - # Show exit code + more info about the last command run - RPROMPT="exited %B%(?.%F{green}%?%f.%F{red}%?%f)%b" - if [ -z $timer ]; then - # If we never set $timer, preexec was never run (e.g. ^C at prompt) - RPROMPT="%B%F{cyan}no exec%f%b, $RPROMPT" - else - # Calculate elapsed real time for last command - now="$(date +%s.%N)" - if [[ $(($now - $timer)) > 0.5 ]]; then - timer_show=$(($now - $timer)) - timer_show=$(printf '%.*f\n' 3 $timer_show) - RPROMPT="took %B%F{cyan}${timer_show}s%f%b, $RPROMPT" - fi - fi - fi - - # add current time, including timezone if remote, and collapse it if we're out of room - RPROMPT="%<#<$RPROMPT at %D{%T$([ -n "$is_remote" ] && echo ' %Z')}" - - # that's an RPROMPT! - export RPROMPT - - # always unset $timer for the next run - unset timer -} -preexec_functions+=preexec -precmd_functions+=precmd +# Load seperated config files +for conf in "$HOME/.config/zsh/config.d/"*.zsh; do + source "${conf}" +done +unset conf # mostly just adding stuff to $PATH now