From b9e3d4d14316054a306914e710157d3e03b5aa25 Mon Sep 17 00:00:00 2001 From: Matt Singleton Date: Tue, 9 Feb 2021 19:26:11 -0600 Subject: refactor zsh --- .gitignore | 2 +- config/zsh/.zshrc | 7 ++-- config/zsh/20-appearance.zsh | 73 ++++++++++++++++++++++++++++++++++++ config/zsh/30-behavior.zsh | 65 ++++++++++++++++++++++++++++++++ config/zsh/40-key-bindings.zsh | 6 +++ config/zsh/50-completion.zsh | 52 +++++++++++++++++++++++++ config/zsh/99-function-overrides.zsh | 8 ++++ config/zsh/appearance.zsh | 73 ------------------------------------ config/zsh/behavior.zsh | 65 -------------------------------- config/zsh/completion.zsh | 52 ------------------------- config/zsh/function-overrides.zsh | 8 ---- config/zsh/key-bindings.zsh | 6 --- config/zsh/zshrc | 17 --------- deploy.sh | 4 +- local/local.zsh | 6 +++ 15 files changed, 217 insertions(+), 227 deletions(-) create mode 100644 config/zsh/20-appearance.zsh create mode 100644 config/zsh/30-behavior.zsh create mode 100644 config/zsh/40-key-bindings.zsh create mode 100644 config/zsh/50-completion.zsh create mode 100644 config/zsh/99-function-overrides.zsh delete mode 100644 config/zsh/appearance.zsh delete mode 100644 config/zsh/behavior.zsh delete mode 100644 config/zsh/completion.zsh delete mode 100644 config/zsh/function-overrides.zsh delete mode 100644 config/zsh/key-bindings.zsh delete mode 100644 config/zsh/zshrc diff --git a/.gitignore b/.gitignore index 88aee5d..bdd02a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ vim/ dotfiles-backup -config/zsh/local.zsh +config/zsh/00-local.zsh config/git/config-local diff --git a/config/zsh/.zshrc b/config/zsh/.zshrc index ee3ebdd..f33ca44 100644 --- a/config/zsh/.zshrc +++ b/config/zsh/.zshrc @@ -1,3 +1,4 @@ -# just a shim to make the real zshrc not hidden -# since zsh doesn't support renaming the config -source "$ZDOTDIR/zshrc" +for f in $ZDOTDIR/*.zsh +do + source "${f}" +done diff --git a/config/zsh/20-appearance.zsh b/config/zsh/20-appearance.zsh new file mode 100644 index 0000000..e7132a8 --- /dev/null +++ b/config/zsh/20-appearance.zsh @@ -0,0 +1,73 @@ +# +# colorize shell programs + +if ls --color -d . &>/dev/null 2>&1; then + alias ls='ls --color=tty' + eval `dircolors ${XDG_CONFIG_HOME:-${HOME}/.config}/dircolors.conf` +else + alias ls='ls -G' + export LSCOLORS="exgxbxdxcxegedxbxgxcxd" +fi + +# grep +export GREP_COLOR='1;32' +alias grep='grep --color=auto' +alias egrep='egrep --color=auto' +alias fgrep='fgrep --color=auto' + +# less +export LESS_TERMCAP_mb=$'\e[0;31m' # begin blinking - red +export LESS_TERMCAP_md=$'\e[0;34m' # begin bold - blue +export LESS_TERMCAP_me=$'\e[0m' # end mode +export LESS_TERMCAP_so=$'\e[30;46m' # begin standout mode - black on cyan +export LESS_TERMCAP_se=$'\e[0m' # end standout mode +export LESS_TERMCAP_us=$'\e[4;33m' # begin underline - yellow underline +export LESS_TERMCAP_ue=$'\e[0m' # end underline + +# +# make a sweet prompt + +autoload colors; colors; +setopt prompt_subst # expansion of color codes, etc. in the prompt + +# print the fully resolved shell command with time stamp +# to be run from zsh's builtin 'preexec' with all arguments passed through ($*) +function theme_preexec () { + echo "($fg[magenta]`date +%r`$reset_color) $fg[cyan]$3$reset_color" +} + +# print the prompt char in red if the last command exited non-zero +function prompt_char { + echo "%(?.$.%{$fg[red]%}$%{$reset_color%})" +} + +function repo_prompt_info { + # git + ref=$(git symbolic-ref HEAD 2> /dev/null || git rev-parse --short HEAD 2> /dev/null) + if [ $? -eq 0 ]; then + #repo=$(basename $(git rev-parse --show-toplevel)) + echo "%{$fg[cyan]%}%{\e[1m%}${ref#refs/heads/}%{$reset_color%}" + return + fi +} + +function workspace_prompt_info { + repo=$(repo_prompt_info) + if [ "$repo" ]; then + echo "[$repo] " + return + fi +} + +# print the hostname in green if local, else red +function hostname_info { + if [[ $ZSH_LOCAL_ENV == "true" ]]; then + echo "%{$fg[green]%}%{$ZSH_HOST_PREFIX%}%m%{$ZSH_HOST_SUFFIX%}%{$reset_color%}" + else + echo "%{$fg[red]%}%{$ZSH_HOST_PREFIX%}%m%{$ZSH_HOST_SUFFIX%}%{$reset_color%}" + fi +} + +# a colorful multiline prompt using the above defined functions +PROMPT=$'%{$fg[yellow]%}%n%{$reset_color%}@$(hostname_info):%{$fg[blue]%}%~%{$reset_color%} +$(workspace_prompt_info)$(prompt_char)%{$reset_color%} ' diff --git a/config/zsh/30-behavior.zsh b/config/zsh/30-behavior.zsh new file mode 100644 index 0000000..e7e6b81 --- /dev/null +++ b/config/zsh/30-behavior.zsh @@ -0,0 +1,65 @@ +# +# execution + +setopt rm_star_wait # wait 10 seconds before accepting 'rm *' confirmation +export REPORTTIME=5 # report timing for any command longer than 5 seconds +unsetopt flowcontrol # disable ^s from freezing the terminal + +# +# history + +HISTFILE="${XDG_DATA_HOME:-${HOME}/.local/share}/zsh/history" +HISTSIZE=10000 +SAVEHIST=10000 + +setopt hist_ignore_all_dups # ignore dups including non-sequential ones +setopt share_history # share command history data between sessions +setopt hist_verify # load hist into command buffer rather than exec immediately + +# +# term support + +# set term title appropriately based on term type +# user@host:current_dir (current_command) +case "$TERM" in + xterm*|rxvt*|screen*|alacritty*) + term_preexec () { + printf '\e]0;%s (%s)\a' ${(%):-'%n@%m:%~'} "$1" + } + term_precmd () { + printf '\e]0;%s\a' ${(%):-'%n@%m:%~'} + } + ;; +esac + +# +# virtualenv + +if [ -e /usr/local/bin/virtualenvwrapper_lazy.sh ]; then + vew=/usr/local/bin/virtualenvwrapper_lazy.sh +elif [ -e /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh ]; then + vew=/usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh +fi + +if [ -n "$vew" ]; then + export VIRTUAL_ENV_DISABLE_PROMPT=true + export WORKON_HOME=~/.virtualenv/envs + source $vew +fi + +# +# draw a horizontal separator + +function hr { + printf "$bg[red]${(l:$COLUMNS:: :)}$reset_color\n" + printf "$bg[yellow]${(l:$COLUMNS:: :)}\n" + printf "$bg[green]${(l:$COLUMNS:: :)}\n" + printf "$bg[cyan]${(l:$COLUMNS:: :)}\n" + printf "$bg[blue]${(l:$COLUMNS:: :)}\n" + printf "$bg[magenta]${(l:$COLUMNS:: :)}$reset_color\n" +} + +# +# less + +export LESS=-i diff --git a/config/zsh/40-key-bindings.zsh b/config/zsh/40-key-bindings.zsh new file mode 100644 index 0000000..4deac62 --- /dev/null +++ b/config/zsh/40-key-bindings.zsh @@ -0,0 +1,6 @@ +bindkey -e # use emacs mode +bindkey '^r' history-incremental-search-backward # ctrl-r +bindkey '^[[Z' reverse-menu-complete # shift-tab + +bindkey '\e[3~' delete-char # make sure delete key works +bindkey ' ' magic-space # also do history expansion on space diff --git a/config/zsh/50-completion.zsh b/config/zsh/50-completion.zsh new file mode 100644 index 0000000..740e9a3 --- /dev/null +++ b/config/zsh/50-completion.zsh @@ -0,0 +1,52 @@ +# initialize the completion system +autoload -U compinit +zmodload zsh/complist +compinit -d "${XDG_CACHE_HOME:-${HOME}/.cache}/zsh/zcompdump-${ZSH_VERSION}" + +# complete only after the second consecutive tab +setopt auto_menu + +# use GNU ls color specification for completion menu +zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS} + +# use menu selection by default +zstyle ':completion:*:default' menu select + +# custom completion list for processes (just current user's) +zstyle ':completion:*:*:*:*:processes' command "ps -u `whoami` -o pid,user,comm -w -w" +# special colors for kill completion listing +zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01' + +# disable named-directories autocompletion +zstyle ':completion:*:cd:*' tag-order local-directories directory-stack path-directories +cdpath=(.) + +# use /etc/hosts and known_hosts for hostname completion +[ -r ~/.ssh/known_hosts ] && _ssh_hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=() +[ -r /etc/hosts ] && : ${(A)_etc_hosts:=${(s: :)${(ps:\t:)${${(f)~~"$( /dev/null && term_preexec $* + type theme_preexec &> /dev/null && theme_preexec $* +} + +precmd () { + type term_precmd &> /dev/null && term_precmd +} diff --git a/config/zsh/appearance.zsh b/config/zsh/appearance.zsh deleted file mode 100644 index e7132a8..0000000 --- a/config/zsh/appearance.zsh +++ /dev/null @@ -1,73 +0,0 @@ -# -# colorize shell programs - -if ls --color -d . &>/dev/null 2>&1; then - alias ls='ls --color=tty' - eval `dircolors ${XDG_CONFIG_HOME:-${HOME}/.config}/dircolors.conf` -else - alias ls='ls -G' - export LSCOLORS="exgxbxdxcxegedxbxgxcxd" -fi - -# grep -export GREP_COLOR='1;32' -alias grep='grep --color=auto' -alias egrep='egrep --color=auto' -alias fgrep='fgrep --color=auto' - -# less -export LESS_TERMCAP_mb=$'\e[0;31m' # begin blinking - red -export LESS_TERMCAP_md=$'\e[0;34m' # begin bold - blue -export LESS_TERMCAP_me=$'\e[0m' # end mode -export LESS_TERMCAP_so=$'\e[30;46m' # begin standout mode - black on cyan -export LESS_TERMCAP_se=$'\e[0m' # end standout mode -export LESS_TERMCAP_us=$'\e[4;33m' # begin underline - yellow underline -export LESS_TERMCAP_ue=$'\e[0m' # end underline - -# -# make a sweet prompt - -autoload colors; colors; -setopt prompt_subst # expansion of color codes, etc. in the prompt - -# print the fully resolved shell command with time stamp -# to be run from zsh's builtin 'preexec' with all arguments passed through ($*) -function theme_preexec () { - echo "($fg[magenta]`date +%r`$reset_color) $fg[cyan]$3$reset_color" -} - -# print the prompt char in red if the last command exited non-zero -function prompt_char { - echo "%(?.$.%{$fg[red]%}$%{$reset_color%})" -} - -function repo_prompt_info { - # git - ref=$(git symbolic-ref HEAD 2> /dev/null || git rev-parse --short HEAD 2> /dev/null) - if [ $? -eq 0 ]; then - #repo=$(basename $(git rev-parse --show-toplevel)) - echo "%{$fg[cyan]%}%{\e[1m%}${ref#refs/heads/}%{$reset_color%}" - return - fi -} - -function workspace_prompt_info { - repo=$(repo_prompt_info) - if [ "$repo" ]; then - echo "[$repo] " - return - fi -} - -# print the hostname in green if local, else red -function hostname_info { - if [[ $ZSH_LOCAL_ENV == "true" ]]; then - echo "%{$fg[green]%}%{$ZSH_HOST_PREFIX%}%m%{$ZSH_HOST_SUFFIX%}%{$reset_color%}" - else - echo "%{$fg[red]%}%{$ZSH_HOST_PREFIX%}%m%{$ZSH_HOST_SUFFIX%}%{$reset_color%}" - fi -} - -# a colorful multiline prompt using the above defined functions -PROMPT=$'%{$fg[yellow]%}%n%{$reset_color%}@$(hostname_info):%{$fg[blue]%}%~%{$reset_color%} -$(workspace_prompt_info)$(prompt_char)%{$reset_color%} ' diff --git a/config/zsh/behavior.zsh b/config/zsh/behavior.zsh deleted file mode 100644 index e7e6b81..0000000 --- a/config/zsh/behavior.zsh +++ /dev/null @@ -1,65 +0,0 @@ -# -# execution - -setopt rm_star_wait # wait 10 seconds before accepting 'rm *' confirmation -export REPORTTIME=5 # report timing for any command longer than 5 seconds -unsetopt flowcontrol # disable ^s from freezing the terminal - -# -# history - -HISTFILE="${XDG_DATA_HOME:-${HOME}/.local/share}/zsh/history" -HISTSIZE=10000 -SAVEHIST=10000 - -setopt hist_ignore_all_dups # ignore dups including non-sequential ones -setopt share_history # share command history data between sessions -setopt hist_verify # load hist into command buffer rather than exec immediately - -# -# term support - -# set term title appropriately based on term type -# user@host:current_dir (current_command) -case "$TERM" in - xterm*|rxvt*|screen*|alacritty*) - term_preexec () { - printf '\e]0;%s (%s)\a' ${(%):-'%n@%m:%~'} "$1" - } - term_precmd () { - printf '\e]0;%s\a' ${(%):-'%n@%m:%~'} - } - ;; -esac - -# -# virtualenv - -if [ -e /usr/local/bin/virtualenvwrapper_lazy.sh ]; then - vew=/usr/local/bin/virtualenvwrapper_lazy.sh -elif [ -e /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh ]; then - vew=/usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh -fi - -if [ -n "$vew" ]; then - export VIRTUAL_ENV_DISABLE_PROMPT=true - export WORKON_HOME=~/.virtualenv/envs - source $vew -fi - -# -# draw a horizontal separator - -function hr { - printf "$bg[red]${(l:$COLUMNS:: :)}$reset_color\n" - printf "$bg[yellow]${(l:$COLUMNS:: :)}\n" - printf "$bg[green]${(l:$COLUMNS:: :)}\n" - printf "$bg[cyan]${(l:$COLUMNS:: :)}\n" - printf "$bg[blue]${(l:$COLUMNS:: :)}\n" - printf "$bg[magenta]${(l:$COLUMNS:: :)}$reset_color\n" -} - -# -# less - -export LESS=-i diff --git a/config/zsh/completion.zsh b/config/zsh/completion.zsh deleted file mode 100644 index 740e9a3..0000000 --- a/config/zsh/completion.zsh +++ /dev/null @@ -1,52 +0,0 @@ -# initialize the completion system -autoload -U compinit -zmodload zsh/complist -compinit -d "${XDG_CACHE_HOME:-${HOME}/.cache}/zsh/zcompdump-${ZSH_VERSION}" - -# complete only after the second consecutive tab -setopt auto_menu - -# use GNU ls color specification for completion menu -zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS} - -# use menu selection by default -zstyle ':completion:*:default' menu select - -# custom completion list for processes (just current user's) -zstyle ':completion:*:*:*:*:processes' command "ps -u `whoami` -o pid,user,comm -w -w" -# special colors for kill completion listing -zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01' - -# disable named-directories autocompletion -zstyle ':completion:*:cd:*' tag-order local-directories directory-stack path-directories -cdpath=(.) - -# use /etc/hosts and known_hosts for hostname completion -[ -r ~/.ssh/known_hosts ] && _ssh_hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=() -[ -r /etc/hosts ] && : ${(A)_etc_hosts:=${(s: :)${(ps:\t:)${${(f)~~"$( /dev/null && term_preexec $* - type theme_preexec &> /dev/null && theme_preexec $* -} - -precmd () { - type term_precmd &> /dev/null && term_precmd -} diff --git a/config/zsh/key-bindings.zsh b/config/zsh/key-bindings.zsh deleted file mode 100644 index 4deac62..0000000 --- a/config/zsh/key-bindings.zsh +++ /dev/null @@ -1,6 +0,0 @@ -bindkey -e # use emacs mode -bindkey '^r' history-incremental-search-backward # ctrl-r -bindkey '^[[Z' reverse-menu-complete # shift-tab - -bindkey '\e[3~' delete-char # make sure delete key works -bindkey ' ' magic-space # also do history expansion on space diff --git a/config/zsh/zshrc b/config/zsh/zshrc deleted file mode 100644 index 16c4bed..0000000 --- a/config/zsh/zshrc +++ /dev/null @@ -1,17 +0,0 @@ -export EDITOR=vim -export LANG=en_US.UTF-8 - -if [ -e "$ZDOTDIR/local.zsh" ]; then - source "$ZDOTDIR/local.zsh" -fi - -source "$ZDOTDIR/appearance.zsh" -source "$ZDOTDIR/behavior.zsh" -source "$ZDOTDIR/completion.zsh" -source "$ZDOTDIR/key-bindings.zsh" - -# override builtin functions (preexec, precmd, etc.) -source "$ZDOTDIR/function-overrides.zsh" - -# user bin should take absolute priority -export PATH="$HOME/bin:$PATH" diff --git a/deploy.sh b/deploy.sh index 7908e5b..17318bd 100755 --- a/deploy.sh +++ b/deploy.sh @@ -44,8 +44,8 @@ fi vim +BundleInstall +qall vim +BundleClean +qall -if [ ! -e config/zsh/local.zsh ]; then - cp local/local.zsh config/zsh/local.zsh +if [ ! -e config/zsh/00-local.zsh ]; then + cp local/local.zsh config/zsh/00-local.zsh fi if [ ! -e config/git/config-local ]; then cp local/gitconfig-local config/git/config-local diff --git a/local/local.zsh b/local/local.zsh index 8b012e4..1b9bf6d 100644 --- a/local/local.zsh +++ b/local/local.zsh @@ -1,3 +1,6 @@ +export EDITOR=vim +export LANG=en_US.UTF-8 + # get sbin on the path #export PATH="/usr/sbin:/sbin:$PATH" @@ -15,6 +18,9 @@ # Java #export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)" +# User bin +#export PATH="$HOME/bin:$PATH" + # start the ssh agent on login and stop on logout #eval `ssh-agent -s` #trap 'eval `ssh-agent -k`' 0 -- cgit v1.2.3