From 3b54637ca7ca1b70cbb6767110eef01f1fced1b2 Mon Sep 17 00:00:00 2001 From: James Patrick Date: Tue, 30 Apr 2019 23:29:37 -0400 Subject: [PATCH] Adding basic zsh configuration. At this point the following doesn't work. - prompt - history - most autocomplete Additionally with the tmux script getting loaded zplug we are getting a double render issue, which is increasing the start up time. I'll probably move this before the .zshrc set to short circuit having to set up all zplug script twice before having a useable shell. --- zsh/env/os/darwin/zprofile | 2 + zsh/makefile | 40 ++++++ zsh/modules/README.md | 8 ++ zsh/modules/fzf/README.md | 1 + zsh/modules/fzf/completion.zsh | 197 ++++++++++++++++++++++++++++++ zsh/modules/fzf/fzf-tmux | 201 +++++++++++++++++++++++++++++++ zsh/modules/fzf/fzf.zsh | 11 ++ zsh/modules/fzf/key-bindings.zsh | 88 ++++++++++++++ zsh/modules/tmux/init.zsh | 28 +++++ zsh/transient/.keep | 0 zsh/zlogin | 16 +++ zsh/zplug | 55 +++++++++ zsh/zprofile | 3 + zsh/zshenv | 37 ++++++ zsh/zshrc | 5 + 15 files changed, 692 insertions(+) create mode 100644 zsh/env/os/darwin/zprofile create mode 100644 zsh/makefile create mode 100644 zsh/modules/README.md create mode 100644 zsh/modules/fzf/README.md create mode 100644 zsh/modules/fzf/completion.zsh create mode 100755 zsh/modules/fzf/fzf-tmux create mode 100644 zsh/modules/fzf/fzf.zsh create mode 100644 zsh/modules/fzf/key-bindings.zsh create mode 100644 zsh/modules/tmux/init.zsh create mode 100644 zsh/transient/.keep create mode 100644 zsh/zlogin create mode 100644 zsh/zplug create mode 100644 zsh/zprofile create mode 100644 zsh/zshenv create mode 100644 zsh/zshrc diff --git a/zsh/env/os/darwin/zprofile b/zsh/env/os/darwin/zprofile new file mode 100644 index 0000000..3c1b90a --- /dev/null +++ b/zsh/env/os/darwin/zprofile @@ -0,0 +1,2 @@ + +export BROWSER='open' diff --git a/zsh/makefile b/zsh/makefile new file mode 100644 index 0000000..cc3155a --- /dev/null +++ b/zsh/makefile @@ -0,0 +1,40 @@ +SRC := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) + +include ../lib/shared.mk +ZSH_DIR := ~/.zsh +ZPLUG_DIR := ~/.zplug + +install: | init update + +init: + $(info => Setting up zsh) +ifeq ($(shell [ ! -e $(ZPLUG_DIR) ] $(return_val_truthy) ), true) + export ZPLUG_HOME=$(ZPLUG_DIR) \ + ; git clone https://github.com/zplug/zplug $ZPLUG_HOME \ + ; $(report) "zplug installed" +endif + $(mk_link) $(SRC) ~/.zsh + $(mk_link) $(ZSH_DIR)/zshrc ~/.zshrc + $(mk_link) $(ZSH_DIR)/zlogin ~/.zlogin + $(mk_link) $(ZSH_DIR)/zprofile ~/.zprofile + $(mk_link) $(ZSH_DIR)/zshenv ~/.zshenv + + +update: +ifeq ($(shell [ -e $(ZPLUG_DIR) ] $(return_val_truthy) ), true) + echo "=> Updating zplug" + source $(ZPLUG_DIR)/init.zsh ; zplug install ; zplug update +else + $(report) warn "No zplug install detected. Skipping." +endif + +remove: +ifeq ($(shell [ -e $(ZPLUG_DIR) ] $(return_val_truthy) ), true) + $(report) warn "Leaving $(ZPLUG_DIR) directory." +endif + $(info => Removing zsh) + $(rm_link) ~/.zprofile + $(rm_link) ~/.zlogin + $(rm_link) ~/.zshrc + $(rm_link) ~/.zsh + $(rm_link) ~/.zshenv diff --git a/zsh/modules/README.md b/zsh/modules/README.md new file mode 100644 index 0000000..59ff279 --- /dev/null +++ b/zsh/modules/README.md @@ -0,0 +1,8 @@ +This directory for custom modules loaded by [zplug](https://github.com/zplug/zplug). + +TODO +```shell +example goes here +``` + +As a general rule is preferable to use someone elses script if possible, with obvious concessions to security and conveince. diff --git a/zsh/modules/fzf/README.md b/zsh/modules/fzf/README.md new file mode 100644 index 0000000..2120318 --- /dev/null +++ b/zsh/modules/fzf/README.md @@ -0,0 +1 @@ +These are generally included with the fzf install. Since I'm using the `fzf-bin` plug these files are not included. These were copied off my Mac (zu). diff --git a/zsh/modules/fzf/completion.zsh b/zsh/modules/fzf/completion.zsh new file mode 100644 index 0000000..4df21b3 --- /dev/null +++ b/zsh/modules/fzf/completion.zsh @@ -0,0 +1,197 @@ +# ____ ____ +# / __/___ / __/ +# / /_/_ / / /_ +# / __/ / /_/ __/ +# /_/ /___/_/-completion.zsh +# +# - $FZF_TMUX (default: 0) +# - $FZF_TMUX_HEIGHT (default: '40%') +# - $FZF_COMPLETION_TRIGGER (default: '**') +# - $FZF_COMPLETION_OPTS (default: empty) + +# To use custom commands instead of find, override _fzf_compgen_{path,dir} +if ! declare -f _fzf_compgen_path > /dev/null; then + _fzf_compgen_path() { + echo "$1" + command find -L "$1" \ + -name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \ + -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@' + } +fi + +if ! declare -f _fzf_compgen_dir > /dev/null; then + _fzf_compgen_dir() { + command find -L "$1" \ + -name .git -prune -o -name .svn -prune -o -type d \ + -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@' + } +fi + +########################################################### + +__fzfcmd_complete() { + [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] && + echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" +} + +__fzf_generic_path_completion() { + local base lbuf compgen fzf_opts suffix tail fzf dir leftover matches + base=$1 + lbuf=$2 + compgen=$3 + fzf_opts=$4 + suffix=$5 + tail=$6 + fzf="$(__fzfcmd_complete)" + + setopt localoptions nonomatch + eval "base=$base" + [[ $base = *"/"* ]] && dir="$base" + while [ 1 ]; do + if [[ -z "$dir" || -d ${dir} ]]; then + leftover=${base/#"$dir"} + leftover=${leftover/#\/} + [ -z "$dir" ] && dir='.' + [ "$dir" != "/" ] && dir="${dir/%\//}" + matches=$(eval "$compgen $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" ${=fzf} ${=fzf_opts} -q "$leftover" | while read item; do + echo -n "${(q)item}$suffix " + done) + matches=${matches% } + if [ -n "$matches" ]; then + LBUFFER="$lbuf$matches$tail" + fi + zle redisplay + typeset -f zle-line-init >/dev/null && zle zle-line-init + break + fi + dir=$(dirname "$dir") + dir=${dir%/}/ + done +} + +_fzf_path_completion() { + __fzf_generic_path_completion "$1" "$2" _fzf_compgen_path \ + "-m" "" " " +} + +_fzf_dir_completion() { + __fzf_generic_path_completion "$1" "$2" _fzf_compgen_dir \ + "" "/" "" +} + +_fzf_feed_fifo() ( + command rm -f "$1" + mkfifo "$1" + cat <&0 > "$1" & +) + +_fzf_complete() { + local fifo fzf_opts lbuf fzf matches post + fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$" + fzf_opts=$1 + lbuf=$2 + post="${funcstack[2]}_post" + type $post > /dev/null 2>&1 || post=cat + + fzf="$(__fzfcmd_complete)" + + _fzf_feed_fifo "$fifo" + matches=$(cat "$fifo" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" ${=fzf} ${=fzf_opts} -q "${(Q)prefix}" | $post | tr '\n' ' ') + if [ -n "$matches" ]; then + LBUFFER="$lbuf$matches" + fi + zle redisplay + typeset -f zle-line-init >/dev/null && zle zle-line-init + command rm -f "$fifo" +} + +_fzf_complete_telnet() { + _fzf_complete '+m' "$@" < <( + command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' | + awk '{if (length($2) > 0) {print $2}}' | sort -u + ) +} + +_fzf_complete_ssh() { + _fzf_complete '+m' "$@" < <( + command cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host ' | command grep -v '[*?]' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}') \ + <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \ + <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') | + awk '{if (length($2) > 0) {print $2}}' | sort -u + ) +} + +_fzf_complete_export() { + _fzf_complete '-m' "$@" < <( + declare -xp | sed 's/=.*//' | sed 's/.* //' + ) +} + +_fzf_complete_unset() { + _fzf_complete '-m' "$@" < <( + declare -xp | sed 's/=.*//' | sed 's/.* //' + ) +} + +_fzf_complete_unalias() { + _fzf_complete '+m' "$@" < <( + alias | sed 's/=.*//' + ) +} + +fzf-completion() { + local tokens cmd prefix trigger tail fzf matches lbuf d_cmds + setopt localoptions noshwordsplit noksh_arrays noposixbuiltins + + # http://zsh.sourceforge.net/FAQ/zshfaq03.html + # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags + tokens=(${(z)LBUFFER}) + if [ ${#tokens} -lt 1 ]; then + zle ${fzf_default_completion:-expand-or-complete} + return + fi + + cmd=${tokens[1]} + + # Explicitly allow for empty trigger. + trigger=${FZF_COMPLETION_TRIGGER-'**'} + [ -z "$trigger" -a ${LBUFFER[-1]} = ' ' ] && tokens+=("") + + tail=${LBUFFER:$(( ${#LBUFFER} - ${#trigger} ))} + # Kill completion (do not require trigger sequence) + if [ $cmd = kill -a ${LBUFFER[-1]} = ' ' ]; then + fzf="$(__fzfcmd_complete)" + matches=$(command ps -ef | sed 1d | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 15 --reverse $FZF_DEFAULT_OPTS --preview 'echo {}' --preview-window down:3:wrap $FZF_COMPLETION_OPTS" ${=fzf} -m | awk '{print $2}' | tr '\n' ' ') + if [ -n "$matches" ]; then + LBUFFER="$LBUFFER$matches" + fi + zle redisplay + typeset -f zle-line-init >/dev/null && zle zle-line-init + # Trigger sequence given + elif [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then + d_cmds=(${=FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}) + + [ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}} + [ -z "${tokens[-1]}" ] && lbuf=$LBUFFER || lbuf=${LBUFFER:0:-${#tokens[-1]}} + + if eval "type _fzf_complete_${cmd} > /dev/null"; then + eval "prefix=\"$prefix\" _fzf_complete_${cmd} \"$lbuf\"" + elif [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then + _fzf_dir_completion "$prefix" "$lbuf" + else + _fzf_path_completion "$prefix" "$lbuf" + fi + # Fall back to default completion + else + zle ${fzf_default_completion:-expand-or-complete} + fi +} + +[ -z "$fzf_default_completion" ] && { + binding=$(bindkey '^I') + [[ $binding =~ 'undefined-key' ]] || fzf_default_completion=$binding[(s: :w)2] + unset binding +} + +zle -N fzf-completion +bindkey '^I' fzf-completion diff --git a/zsh/modules/fzf/fzf-tmux b/zsh/modules/fzf/fzf-tmux new file mode 100755 index 0000000..3233bd5 --- /dev/null +++ b/zsh/modules/fzf/fzf-tmux @@ -0,0 +1,201 @@ +#!/usr/bin/env bash +# fzf-tmux: starts fzf in a tmux pane +# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS] + +fail() { + >&2 echo "$1" + exit 2 +} + +fzf="$(command -v fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf" +[[ -x "$fzf" ]] || fail 'fzf executable not found' + +args=() +opt="" +skip="" +swap="" +close="" +term="" +[[ -n "$LINES" ]] && lines=$LINES || lines=$(tput lines) || lines=$(tmux display-message -p "#{pane_height}") +[[ -n "$COLUMNS" ]] && columns=$COLUMNS || columns=$(tput cols) || columns=$(tmux display-message -p "#{pane_width}") + +help() { + >&2 echo 'usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS] + + Layout + -u [HEIGHT[%]] Split above (up) + -d [HEIGHT[%]] Split below (down) + -l [WIDTH[%]] Split left + -r [WIDTH[%]] Split right + + (default: -d 50%) +' + exit +} + +while [[ $# -gt 0 ]]; do + arg="$1" + shift + [[ -z "$skip" ]] && case "$arg" in + -) + term=1 + ;; + --help) + help + ;; + --version) + echo "fzf-tmux (with fzf $("$fzf" --version))" + exit + ;; + -w*|-h*|-d*|-u*|-r*|-l*) + if [[ "$arg" =~ ^.[lrw] ]]; then + opt="-h" + if [[ "$arg" =~ ^.l ]]; then + opt="$opt -d" + swap="; swap-pane -D ; select-pane -L" + close="; tmux swap-pane -D" + fi + else + opt="" + if [[ "$arg" =~ ^.u ]]; then + opt="$opt -d" + swap="; swap-pane -D ; select-pane -U" + close="; tmux swap-pane -D" + fi + fi + if [[ ${#arg} -gt 2 ]]; then + size="${arg:2}" + else + if [[ "$1" =~ ^[0-9]+%?$ ]]; then + size="$1" + shift + else + continue + fi + fi + + if [[ "$size" =~ %$ ]]; then + size=${size:0:((${#size}-1))} + if [[ -n "$swap" ]]; then + opt="$opt -p $(( 100 - size ))" + else + opt="$opt -p $size" + fi + else + if [[ -n "$swap" ]]; then + if [[ "$arg" =~ ^.l ]]; then + max=$columns + else + max=$lines + fi + size=$(( max - size )) + [[ $size -lt 0 ]] && size=0 + opt="$opt -l $size" + else + opt="$opt -l $size" + fi + fi + ;; + --) + # "--" can be used to separate fzf-tmux options from fzf options to + # avoid conflicts + skip=1 + continue + ;; + *) + args+=("$arg") + ;; + esac + [[ -n "$skip" ]] && args+=("$arg") +done + +if [[ -z "$TMUX" || "$opt" =~ ^-h && "$columns" -le 40 || ! "$opt" =~ ^-h && "$lines" -le 15 ]]; then + "$fzf" "${args[@]}" + exit $? +fi + +# --height option is not allowed +args+=("--no-height") + +# Handle zoomed tmux pane by moving it to a temp window +if tmux list-panes -F '#F' | grep -q Z; then + zoomed=1 + original_window=$(tmux display-message -p "#{window_id}") + tmp_window=$(tmux new-window -d -P -F "#{window_id}" "bash -c 'while :; do for c in \\| / - '\\;' do sleep 0.2; printf \"\\r\$c fzf-tmux is running\\r\"; done; done'") + tmux swap-pane -t $tmp_window \; select-window -t $tmp_window +fi + +set -e + +# Clean up named pipes on exit +id=$RANDOM +argsf="${TMPDIR:-/tmp}/fzf-args-$id" +fifo1="${TMPDIR:-/tmp}/fzf-fifo1-$id" +fifo2="${TMPDIR:-/tmp}/fzf-fifo2-$id" +fifo3="${TMPDIR:-/tmp}/fzf-fifo3-$id" +cleanup() { + \rm -f $argsf $fifo1 $fifo2 $fifo3 + + # Restore tmux window options + if [[ "${#tmux_win_opts[@]}" -gt 0 ]]; then + eval "tmux ${tmux_win_opts[@]}" + fi + + # Remove temp window if we were zoomed + if [[ -n "$zoomed" ]]; then + tmux display-message -p "#{window_id}" > /dev/null + tmux swap-pane -t $original_window \; \ + select-window -t $original_window \; \ + kill-window -t $tmp_window \; \ + resize-pane -Z + fi + + if [ $# -gt 0 ]; then + trap - EXIT + exit 130 + fi +} +trap 'cleanup 1' SIGUSR1 +trap 'cleanup' EXIT + +envs="env TERM=$TERM " +[[ -n "$FZF_DEFAULT_OPTS" ]] && envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")" +[[ -n "$FZF_DEFAULT_COMMAND" ]] && envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")" + +mkfifo -m o+w $fifo2 +mkfifo -m o+w $fifo3 + +# Build arguments to fzf +opts="" +for arg in "${args[@]}"; do + arg="${arg//\\/\\\\}" + arg="${arg//\"/\\\"}" + arg="${arg//\`/\\\`}" + arg="${arg//$/\\$}" + opts="$opts \"$arg\"" +done + +pppid=$$ +echo -n "trap 'kill -SIGUSR1 -$pppid' EXIT SIGINT SIGTERM;" > $argsf +close="; trap - EXIT SIGINT SIGTERM $close" + +tmux_win_opts=( $(tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/') ) + +if [[ -n "$term" ]] || [[ -t 0 ]]; then + cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" >> $argsf + TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\ + set-window-option remain-on-exit off \;\ + split-window $opt "$envs bash -c 'cd $(printf %q "$PWD"); exec -a fzf bash $argsf'" $swap \ + > /dev/null 2>&1 +else + mkfifo $fifo1 + cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" >> $argsf + TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\ + set-window-option remain-on-exit off \;\ + split-window $opt "$envs bash -c 'exec -a fzf bash $argsf'" $swap \ + > /dev/null 2>&1 + cat <&0 > $fifo1 & +fi +cat $fifo2 +exit "$(cat $fifo3)" + diff --git a/zsh/modules/fzf/fzf.zsh b/zsh/modules/fzf/fzf.zsh new file mode 100644 index 0000000..bcb693d --- /dev/null +++ b/zsh/modules/fzf/fzf.zsh @@ -0,0 +1,11 @@ +# Setup fzf +# --------- +THIS_DIR="${${funcsourcetrace[1]%/*}%?}" + +# Auto-completion +# --------------- +[[ $- == *i* ]] && source "${THIS_DIR}/completion.zsh" 2> /dev/null + +# Key bindings +# ------------ +source "${THIS_DIR}/key-bindings.zsh" diff --git a/zsh/modules/fzf/key-bindings.zsh b/zsh/modules/fzf/key-bindings.zsh new file mode 100644 index 0000000..c25368c --- /dev/null +++ b/zsh/modules/fzf/key-bindings.zsh @@ -0,0 +1,88 @@ +# Key bindings +# ------------ +if [[ $- == *i* ]]; then + +# CTRL-T - Paste the selected file path(s) into the command line +__fsel() { + local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ + -o -type f -print \ + -o -type d -print \ + -o -type l -print 2> /dev/null | cut -b3-"}" + setopt localoptions pipefail 2> /dev/null + eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do + echo -n "${(q)item} " + done + local ret=$? + echo + return $ret +} + +__fzf_use_tmux__() { + [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] +} + +__fzfcmd() { + __fzf_use_tmux__ && + echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" +} + +fzf-file-widget() { + LBUFFER="${LBUFFER}$(__fsel)" + local ret=$? + zle redisplay + typeset -f zle-line-init >/dev/null && zle zle-line-init + return $ret +} +zle -N fzf-file-widget +bindkey '^T' fzf-file-widget + +# Ensure precmds are run after cd +fzf-redraw-prompt() { + local precmd + for precmd in $precmd_functions; do + $precmd + done + zle reset-prompt +} +zle -N fzf-redraw-prompt + +# ALT-C - cd into the selected directory +fzf-cd-widget() { + local cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ + -o -type d -print 2> /dev/null | cut -b3-"}" + setopt localoptions pipefail 2> /dev/null + local dir="$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m)" + if [[ -z "$dir" ]]; then + zle redisplay + return 0 + fi + cd "$dir" + local ret=$? + zle fzf-redraw-prompt + typeset -f zle-line-init >/dev/null && zle zle-line-init + return $ret +} +zle -N fzf-cd-widget +bindkey '\ec' fzf-cd-widget + +# CTRL-R - Paste the selected command from history into the command line +fzf-history-widget() { + local selected num + setopt localoptions noglobsubst noposixbuiltins pipefail 2> /dev/null + selected=( $(fc -rl 1 | + FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) ) + local ret=$? + if [ -n "$selected" ]; then + num=$selected[1] + if [ -n "$num" ]; then + zle vi-fetch-history -n $num + fi + fi + zle redisplay + typeset -f zle-line-init >/dev/null && zle zle-line-init + return $ret +} +zle -N fzf-history-widget +bindkey '^R' fzf-history-widget + +fi diff --git a/zsh/modules/tmux/init.zsh b/zsh/modules/tmux/init.zsh new file mode 100644 index 0000000..2330bc9 --- /dev/null +++ b/zsh/modules/tmux/init.zsh @@ -0,0 +1,28 @@ +# vim: syn=zsh + +# Return if tmux isn't installed. +if (( ! $+commands[tmux] )); then + return 1 +fi + +# If this this terminal meets any of these return. +env_array=($TMUX $EMACS $VIM $INSODE_EMACS $VSCODE) +if [[ -n $env_array ]]; then + return 0 +fi + +if ( [[ -n "$SSH_TTY" ]] && zstyle -t ':tmux:auto-start' remote ) || \ + ( [[ -z "$SSH_TTY" ]] && zstyle -t ':tmux:auto-start' local ) \ + ; then + zstyle -s ':tmux:auto-start' name tmux_session + tmux_session=${tmux_session:-tmux} + echo $tmux_session + tmux start-server + if ! tmux has-session 2> /dev/null; then + zstyle -s ':tmux:auto-start' name tmux_session + tmux \ + new-session -d -s "${tmux_session:-tmux}" \; \ + set-option -t "${tmux_session:-tmux}" destroy-unattached off &> /dev/null + fi + tmux attach -t $tmux_session +fi diff --git a/zsh/transient/.keep b/zsh/transient/.keep new file mode 100644 index 0000000..e69de29 diff --git a/zsh/zlogin b/zsh/zlogin new file mode 100644 index 0000000..955f4ee --- /dev/null +++ b/zsh/zlogin @@ -0,0 +1,16 @@ +# vim: syn=zsh + +# Executes commands at login post-zshrc. +# +# Execute code that does not affect the current session in the background. + +( autoload -Uz colors && colors ) &! +{ + # Compile the completion dump to increase startup speed. + # + zcompdump="${ZDIR}/transient/.zcompdump" + if [[ -s "$zcompdump" && (! -s "${zcompdump}.zwc" || "$zcompdump" -nt "${zcompdump}.zwc") ]]; then + zcompile "$zcompdump" + fi +} &! +env_loader zlogin diff --git a/zsh/zplug b/zsh/zplug new file mode 100644 index 0000000..17d91a6 --- /dev/null +++ b/zsh/zplug @@ -0,0 +1,55 @@ +# vim: syn=zsh + +source ${ZPLUG_HOME}/init.zsh + +zstyle ':tmux:auto-start' local 'yes' +zstyle ':tmux:auto-start' name 'tmux' +zplug "${ZDIR}/modules/tmux/", \ + from:local, \ + defer:0, \ + as:plugin + +zplug "zdharma/fast-syntax-highlighting", \ + as:plugin, \ + defer:2 + +zplug "hlissner/zsh-autopair", \ + as:plugin, \ + defer:2 + +zplug "zsh-users/zsh-history-substring-search", \ + as:plugin, \ + defer:2 + +zplug "zpm-zsh/autoenv" + +zplug "zsh-users/zsh-autosuggestions", \ + as:plugin, \ + defer:2 + +zplug "junegunn/fzf-bin", \ + from:gh-r, \ + as:command, \ + rename-to:fzf + +zplug "zdharma/zsh-diff-so-fancy", \ + as:command, \ + use:"bin/{git-dsf,diff-so-fancy}" + +FZF_TMUX=1 +FZF_TMUX_HEIGHT=30% +zplug "${ZDIR}/modules/fzf/", \ + from:local, \ + use:"fzf.zsh", \ + as:plugin +zplug "${ZDIR}/modules/fzf/", \ + from:local, \ + use:"fzf-tmux", \ + as:command + + + +env_loader zplug + +zplug load + diff --git a/zsh/zprofile b/zsh/zprofile new file mode 100644 index 0000000..0dc52be --- /dev/null +++ b/zsh/zprofile @@ -0,0 +1,3 @@ +# vim: syn=zsh + +env_loader zprofile \ No newline at end of file diff --git a/zsh/zshenv b/zsh/zshenv new file mode 100644 index 0000000..93902da --- /dev/null +++ b/zsh/zshenv @@ -0,0 +1,37 @@ +# vim: syn=zsh + +# Language +# TODO: LANG is getting set to "C" somewhere. Until I figure that out, I'm hard coding it. +#export LANG=${LANG:-en_US.UTF-8} +export LANG=en_US.UTF-8 + +export ZDOTDIR=${ZDOTDIR:-$HOME} +export ZDIR=${ZDIR:-$HOME/.zsh} +export ZPLUG_HOME=${ZPLUG_HOME:-$HOME/.zplug} + +export EDITOR='vim' +export VISUAL='vim' +export PAGER='less' +# Less +# Set the default Less options. +# Mouse-wheel scrolling has been disabled by -X (disable screen clearing). +# Remove -X and -F (exit if the content fits on one screen) to enable it. +export LESS='-F -g -i -M -R -S -w -X -z-4' + +# This will load the specified zsh files if they exist in the $ZDIR/env directory. +# It will load based on hostname & uname. + +env_loader(){ + [ -e ${ZDIR}/env/$OSTYPE/$1 ] && source ${ZDIR}/env/$OSTYPE/$1 + [ -e ${ZDIR}/env/$HOST/$1 ] && source ${ZDIR}/env/$HOST/$1 +} + +user_script_dir="~/.scripts" +if [ -d $user_script_dir ] ; then + PATH="$user_script_dir:$PATH" +fi + +env_loader zshenv + +# Dedup path. +typeset -gU path diff --git a/zsh/zshrc b/zsh/zshrc new file mode 100644 index 0000000..aa5dbb5 --- /dev/null +++ b/zsh/zshrc @@ -0,0 +1,5 @@ +# vim: syn=zsh + +source $ZDIR/zplug + +env_loader zshrc