From d44dd103a328dc3fad8b121f332fdee0e2aabf9d Mon Sep 17 00:00:00 2001 From: George Rawlinson Date: Fri, 1 Nov 2019 12:18:00 +1300 Subject: [PATCH] feat: initial framework --- completions/_autosource | 10 ++++++ functions/autosource | 11 ++++++ functions/sourceall | 17 ++++++++++ plugins/coloured-man | 13 ++++++++ plugins/sudo | 21 ++++++++++++ source/aliases.zsh | 74 +++++++++++++++++++++++++++++++++++++++++ source/bindkeys.zsh | 48 ++++++++++++++++++++++++++ source/colours.zsh | 15 +++++++++ source/completions.zsh | 25 ++++++++++++++ source/env.zsh | 13 ++++++++ source/gpg.zsh | 15 +++++++++ source/options.zsh | 27 +++++++++++++++ zshenv | 12 +++++++ zshrc | 54 ++++++++++++++++++++++++++++++ 14 files changed, 355 insertions(+) create mode 100644 completions/_autosource create mode 100644 functions/autosource create mode 100644 functions/sourceall create mode 100644 plugins/coloured-man create mode 100644 plugins/sudo create mode 100644 source/aliases.zsh create mode 100644 source/bindkeys.zsh create mode 100644 source/colours.zsh create mode 100644 source/completions.zsh create mode 100644 source/env.zsh create mode 100644 source/gpg.zsh create mode 100644 source/options.zsh create mode 100644 zshenv create mode 100644 zshrc diff --git a/completions/_autosource b/completions/_autosource new file mode 100644 index 0000000..fbb90bc --- /dev/null +++ b/completions/_autosource @@ -0,0 +1,10 @@ +#compdef autosource + +_autosource() { + _alternative \ + 'plugin:filename:{_path_files -W spath}' +} + +_autosource + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/functions/autosource b/functions/autosource new file mode 100644 index 0000000..b990d4b --- /dev/null +++ b/functions/autosource @@ -0,0 +1,11 @@ +#!/bin/zsh +local f= +local p= + +for f; do + for p in $spath; do + [[ -r $p/$f ]] && . "$p/$f" && break + done +done + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/functions/sourceall b/functions/sourceall new file mode 100644 index 0000000..2d731eb --- /dev/null +++ b/functions/sourceall @@ -0,0 +1,17 @@ +#!/bin/zsh +# usage: +# `sourceall zsh` will source *.zsh, otherwise * + +local ext= +local f= +local p= + +(( $# > 0 )) && ext=".$1" +for p in $apath; do + [[ -d $p ]] || continue + for f in $p/*$ext(.N); do + [[ -r $f ]] && . $f + done +done + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/plugins/coloured-man b/plugins/coloured-man new file mode 100644 index 0000000..dc6d01f --- /dev/null +++ b/plugins/coloured-man @@ -0,0 +1,13 @@ +# colour output in man +# source: https://wiki.archlinux.org/index.php/Color_output_in_console#man +man() { + LESS_TERMCAP_md=$'\e[01;31m' \ + LESS_TERMCAP_me=$'\e[0m' \ + LESS_TERMCAP_se=$'\e[0m' \ + LESS_TERMCAP_so=$'\e[01;44;33m' \ + LESS_TERMCAP_ue=$'\e[0m' \ + LESS_TERMCAP_us=$'\e[01;32m' \ + command man "$@" +} + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/plugins/sudo b/plugins/sudo new file mode 100644 index 0000000..e9401b9 --- /dev/null +++ b/plugins/sudo @@ -0,0 +1,21 @@ +# sudo will be inserted before the current command +sudo-command-line() { + [[ -z $BUFFER ]] && zle up-history + if [[ $BUFFER == sudo\ * ]]; then + LBUFFER="${LBUFFER#sudo }" + elif [[ $BUFFER == $EDITOR\ * ]]; then + LBUFFER="${LBUFFER#$EDITOR }" + LBUFFER="sudoedit $LBUFFER" + elif [[ $BUFFER == sudoedit\ * ]]; then + LBUFFER="${LBUFFER#sudoedit }" + LBUFFER="$EDITOR $LBUFFER" + else + LBUFFER="sudo $LBUFFER" + fi +} + +zle -N sudo-command-line +# defined shortcut keys: [Esc] [Esc] +bindkey "\e\e" sudo-command-line + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/source/aliases.zsh b/source/aliases.zsh new file mode 100644 index 0000000..b7ed845 --- /dev/null +++ b/source/aliases.zsh @@ -0,0 +1,74 @@ +# common aliases +alias grep='grep --color=auto' +alias diff='diff --color=auto' +alias history='fc -il 1' # redirect history to STDOUT with correct timestamps + +# safe(r) interactive commands +alias rm='rm -i' +alias cp='cp -i' +alias mv='mv -i' + +# ls +alias ls='ls --color=auto --human-readable --group-directories-first' +alias l='ls --classify -l' +alias la='ls --classify -la' +alias lr='ls --classify -tR' + +# directories +setopt AUTO_CD # prefix command with 'cd' if it cannot be executed, e.g. '..' becomes 'cd ..' +alias ...='../..' +alias ....='../../..' +alias .....='../../../..' +alias ......='../../../../..' + +# git +if command -v git &> /dev/null; then + # status + alias gst='git status' + + # add/commit + alias gaa='git add --all' + alias gcmsg='git commit -m' + + # log + alias glg='git log --stat' + alias glgp='git log --stat -p' + alias glgg='git log --graph' + alias glgga='git log --graph --decorate --all' + alias glgm='git log --graph --max-count=10' + + # push/pull + alias gp='git push' + alias gl='git pull' +fi + +# pacman +if command -v pacman &> /dev/null; then + # source: https://wiki.archlinux.org/index.php/Pacman_Tips + alias pacupg='sudo pacman -Syu' + alias pacin='sudo pacman -S' + alias pacins='sudo pacman -U' + alias pacre='sudo pacman -R' + alias pacrem='sudo pacman -Rns' + alias pacrep='pacman -Si' + alias pacreps='pacman -Ss' + alias pacloc='pacman -Qi' + alias paclocs='pacman -Qs' + alias pacinsd='sudo pacman -S --asdeps' + alias pacmir='sudo pacman -Syy' + alias paclsorphans='sudo pacman -Qdt' + alias pacrmorphans='sudo pacman -Rs $(pacman -Qtdq)' + alias pacfileupg='sudo pacman -Fy' + alias pacfiles='pacman tFs' + + # list all explicitly installed packages + # source: https://bbs.archlinux.org/viewtopic.php?id=93683 + paclist() { + LC_ALL=C pacman -Qei $(pacman -Qu | cut -d " " -f 1) | \ + awk 'BEGIN {FS=":"} /^Name/{printf("\033[1;36m%s\033[1;37m", $2)} /^Description/{print $2}' + } + + export CHROOT="${XDG_CACHE_HOME}/chroot" +fi + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/source/bindkeys.zsh b/source/bindkeys.zsh new file mode 100644 index 0000000..387786c --- /dev/null +++ b/source/bindkeys.zsh @@ -0,0 +1,48 @@ +# key bindings + +# create a zkbd compatible hash; +# to add other keys to this hash, see: man 5 terminfo +typeset -g -A key + +#key[Home]="${terminfo[khome]}" +#key[End]="${terminfo[kend]}" +#key[Insert]="${terminfo[kich1]}" +key[Backspace]="${terminfo[kbs]}" +key[Delete]="${terminfo[kdch1]}" +key[Up]="${terminfo[kcuu1]}" +key[Down]="${terminfo[kcud1]}" +key[Left]="${terminfo[kcub1]}" +key[Right]="${terminfo[kcuf1]}" +key[PageUp]="${terminfo[kpp]}" +key[PageDown]="${terminfo[knp]}" +key[ShiftTab]="${terminfo[kcbt]}" + +# setup history search +autoload -Uz up-line-or-beginning-search down-line-or-beginning-search +zle -N up-line-or-beginning-search +zle -N down-line-or-beginning-search + +# key bindings +[[ -n "${key[Backspace]}" ]] && bindkey -- "${key[Backspace]}" backward-delete-char +[[ -n "${key[Delete]}" ]] && bindkey -- "${key[Delete]}" delete-char +[[ -n "${key[Up]}" ]] && bindkey -- "${key[Up]}" up-line-or-beginning-search +[[ -n "${key[Down]}" ]] && bindkey -- "${key[Down]}" down-line-or-beginning-search +[[ -n "${key[PageUp]}" ]] && bindkey -- "${key[PageUp]}" up-line-or-history +[[ -n "${key[PageDown]}" ]] && bindkey -- "${key[PageDown]}" down-line-or-history +[[ -n "${key[ShiftTab]}" ]] && bindkey -- "${key[ShiftTab]}" reverse-menu-complete + +# make sure the terminal is in application mode when zle is active +# only then are the values from $terminfo valid +if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then + autoload -Uz add-zle-hook-widget + zle_application_mode_start() { + echoti smkx + } + zle_application_mode_stop() { + echoti rmkx + } + add-zle-hook-widget -Uz zle-line-init zle_application_mode_start + add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop +fi + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/source/colours.zsh b/source/colours.zsh new file mode 100644 index 0000000..c16d7fe --- /dev/null +++ b/source/colours.zsh @@ -0,0 +1,15 @@ +# +# colours +# + +autoload -U colors && colors + +# setup LS_COLORS +if [[ -z "$LS_COLORS" ]]; then + (( $+commands[dircolors] )) && eval "$(dircolors -b)" +fi + +# use LS_COLORS for completion +zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/source/completions.zsh b/source/completions.zsh new file mode 100644 index 0000000..cd584ef --- /dev/null +++ b/source/completions.zsh @@ -0,0 +1,25 @@ +# completion configuration + +# have the menu highlight as we cycle through options +zstyle ':completion:*' menu select +# case insensitive (all), partial-word and substring completion +zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|=*' 'l:|=* r:|=*' +# allow completion from within a word/phrase +setopt COMPLETE_IN_WORD +# when completing from the middle of a word, move cursor to end of word +setopt ALWAYS_TO_END +# turn on completion for aliases as well +setopt COMPLETE_ALIASES +# cycle through menus horizontally instead of vertically +#setopt LIST_ROWS_FIRST +# when using auto-complete, put the first option on the line immediately +#setopt MENU_COMPLETE + +# complete . and .. special directories +zstyle ':completion:*' special-dirs true + +# use caching so that commands like apt and dpkg complete are useable +zstyle ':completion::complete:*' use-cache 1 +zstyle ':completion::complete:*' cache-path "{$ZCACHEDIR}" + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/source/env.zsh b/source/env.zsh new file mode 100644 index 0000000..856ae38 --- /dev/null +++ b/source/env.zsh @@ -0,0 +1,13 @@ +# locale [en_NZ] +export LANG='en_NZ.UTF-8' +export LC_ALL='en_NZ.UTF-8' + +# set default values +export EDITOR='nvim' +export PAGER='less' +export LESS='-R' + +# less - disable histfile generation +export LESSHISTFILE='/dev/null' + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/source/gpg.zsh b/source/gpg.zsh new file mode 100644 index 0000000..8c1a33d --- /dev/null +++ b/source/gpg.zsh @@ -0,0 +1,15 @@ +# From gpg-agent(1): +# +# You should always add the following lines to your .bashrc or whatever +# initialization file is used for all shell invocations: +# +# GPG_TTY=$(tty) +# export GPG_TTY +# +# It is important that this environment variable always reflects the out‐ +# put of the tty command. + +GPG_TTY=$(tty) +export GPG_TTY + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/source/options.zsh b/source/options.zsh new file mode 100644 index 0000000..c461bcb --- /dev/null +++ b/source/options.zsh @@ -0,0 +1,27 @@ +# general +setopt NO_BEEP # disable terminal beep +setopt MULTI_OS # pipe to multiple outputs +setopt INTERACTIVE_COMMENTS # allow comments in interactive shell +setopt VI # vi-like keybindings +#setopt KSH_ARRAYS # whoever thought non-0 indices needs to be shot + +# globbing +setopt NO_CASE_GLOB # case insensitive globbing +setopt EXTENDED_GLOB # enable zsh globbing features +setopt NUMERIC_GLOB_SORT # sort globs numerically, not by letter + +# history +HISTSIZE=100000 # set history size to 100K +SAVEHIST="${HISTSIZE}" # cap history size at 100K +setopt EXTENDED_HISTORY # record timestamp on command in HISTFILE +setopt HIST_EXPIRE_DUPS_FIRST # delete duplicates first when HISTFILE > HISTSIZE +setopt APPEND_HISTORY # ignore duplicate commands in history list +setopt HIST_FIND_NO_DUPS # when searching history don't display results already cycled through twice +setopt HIST_IGNORE_SPACE # ignore commands that start with space +setopt HIST_VERIFY # show command with history expansion to user before running it +setopt SHARE_HISTORY # shares history across multiple zsh sessions, in real time +setopt HIST_IGNORE_ALL_DUPS # do not write commands to history that are duplicates of the previous command +setopt HIST_REDUCE_BLANKS # remove erroneous blanks from commands before appending to history + +# create history file +[ -z "${HISTFILE}" ] && HISTFILE="${ZCACHEDIR}/zhistory" diff --git a/zshenv b/zshenv new file mode 100644 index 0000000..9bf2f10 --- /dev/null +++ b/zshenv @@ -0,0 +1,12 @@ +# set environment variable $ZDOTDIR to $HOME/.config/zsh +export ZDOTDIR="${HOME}/.config/zsh" +# set environment variable $ZCACHEDIR to $HOME/.cache/zsh +export ZCACHEDIR="${HOME}/.cache/zsh" +# set environment variable $ZSYSDIR to /usr/share/zsh-grawlinson +export ZSYSDIR="/usr/share/zsh-grawlinson" + +# create missing folders +if [ ! -d "${ZDOTDIR}" ]; then mkdir -p "${ZDOTDIR}"; fi +if [ ! -d "${ZCACHEDIR}" ]; then mkdir -p "${ZCACHEDIR}"; fi + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2 diff --git a/zshrc b/zshrc new file mode 100644 index 0000000..b4753a9 --- /dev/null +++ b/zshrc @@ -0,0 +1,54 @@ +# setup umask [0755] +umask 0022 + +# spath -> plugins; apath -> drop-in path +typeset -T SPATH spath +typeset -T APATH apath +typeset -U PATH path # discard duplicates + +# example: append to path, then make available to child processes +# path+=('$VAR'); export PATH + +# default path values +spath=( + "$ZDOTDIR/plugins" + "$ZSYSDIR/plugins" +) +apath=( + "$ZDOTDIR/source" + "$ZSYSDIR/source" +) + + +# user stuff comes first +# completions come after the functions they complete +fpath+=( + "$ZDOTDIR/functions" + "$ZDOTDIR/completions" + "$ZDOTDIR/prompts" + "$ZSYSDIR/functions" + "$ZSYSDIR/completions" + "$ZSYSDIR/prompts" +) + +# source every zsh file in every APATH directory +autoload sourceall +sourceall zsh + +# plugins +autoload autosource +autosource coloured-man +autosource sudo + +# setup ys prompt +autoload -Uz promptinit && promptinit +prompt ys + +# source local zshrc +[[ -f "$ZDOTDIR/zshrc.local" ]] && . "$ZDOTDIR/zshrc.local" + +# setup compinit +autoload -U compinit +compinit -i -d "${ZCACHEDIR}/zcompdump-${ZSH_VERSION}" + +# vim: ft=zsh expandtab tabstop=2 shiftwidth=2