From a54125ec2d5a43a6f3e578681f0862114c3b8745 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Wed, 28 Feb 2018 19:49:47 +0000 Subject: Adding tmux, zsh and X config --- zsh/.zsh/export.zsh | 7 ++ zsh/.zsh/function.zsh | 14 +++ zsh/.zsh/fzf_completion.zsh | 199 ++++++++++++++++++++++++++++++++++++++++++ zsh/.zsh/fzf_key_bindings.zsh | 78 +++++++++++++++++ zsh/.zsh/minimal.zsh | 181 ++++++++++++++++++++++++++++++++++++++ zsh/.zsh/startup.zsh | 3 + zsh/.zshrc | 27 ++++++ 7 files changed, 509 insertions(+) create mode 100644 zsh/.zsh/export.zsh create mode 100644 zsh/.zsh/function.zsh create mode 100644 zsh/.zsh/fzf_completion.zsh create mode 100644 zsh/.zsh/fzf_key_bindings.zsh create mode 100644 zsh/.zsh/minimal.zsh create mode 100644 zsh/.zsh/startup.zsh create mode 100644 zsh/.zshrc (limited to 'zsh') diff --git a/zsh/.zsh/export.zsh b/zsh/.zsh/export.zsh new file mode 100644 index 0000000..07e1b56 --- /dev/null +++ b/zsh/.zsh/export.zsh @@ -0,0 +1,7 @@ +export GITHUB_HOME=$HOME/Github +export ALTERNATE_EDITOR="" +export EDITOR="nvim" +export VISUAL="nvim" + +export PATH="{$PATH}:${HOME}/.gem/ruby/2.5.0/bin:/usr/local/bin" +export PATH="{$PATH}:${HOME}/.local/bin" diff --git a/zsh/.zsh/function.zsh b/zsh/.zsh/function.zsh new file mode 100644 index 0000000..30e24aa --- /dev/null +++ b/zsh/.zsh/function.zsh @@ -0,0 +1,14 @@ +alias fdisk='fdisk --color=always' +alias grep='grep --color=always' +alias ls='ls --color=always' +alias l='ls -la --color=always' +alias vim='nvim' +# alias lspasscp='lpass show -c --password $(lpass ls | fzf | awk '{print $(NF)}' | sed 's/\\]//g')' + +# fd - cd to selected directory +fd() { + local dir + dir=$(find ${1:-.} -path '*/\.*' -prune \ + -o -type d -print 2> /dev/null | fzf +m) && + cd "$dir" +} diff --git a/zsh/.zsh/fzf_completion.zsh b/zsh/.zsh/fzf_completion.zsh new file mode 100644 index 0000000..d3e64d8 --- /dev/null +++ b/zsh/.zsh/fzf_completion.zsh @@ -0,0 +1,199 @@ +#!/bin/zsh +# ____ ____ +# / __/___ / __/ +# / /_/_ / / /_ +# / __/ / /_/ __/ +# /_/ /___/_/-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 + # (Q) flag removes a quoting level: "foo\ bar" => "foo bar" + base=${(Q)1} + lbuf=$2 + compgen=$3 + fzf_opts=$4 + suffix=$5 + tail=$6 + fzf="$(__fzfcmd_complete)" + + setopt localoptions nonomatch + dir="$base" + while [ 1 ]; do + if [[ -z "$dir" || -d ${~dir} ]]; then + leftover=${base/#"$dir"} + leftover=${leftover/#\/} + [ -z "$dir" ] && dir='.' + [ "$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' | 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=$(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/.zsh/fzf_key_bindings.zsh b/zsh/.zsh/fzf_key_bindings.zsh new file mode 100644 index 0000000..7064611 --- /dev/null +++ b/zsh/.zsh/fzf_key_bindings.zsh @@ -0,0 +1,78 @@ +# 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 + +# 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 reset-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 -l 1 | + FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tac -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/.zsh/minimal.zsh b/zsh/.zsh/minimal.zsh new file mode 100644 index 0000000..cb814bc --- /dev/null +++ b/zsh/.zsh/minimal.zsh @@ -0,0 +1,181 @@ +# Switches +MINIMAL_PROMPT="${MINIMAL_PROMPT:-yes}" +MINIMAL_RPROMPT="${MINIMAL_RPROMPT:-yes}" +MINIMAL_MAGIC_ENTER="${MINIMAL_MAGIC_ENTER:-yes}" + +# Parameters +MINIMAL_OK_COLOR="${MINIMAL_OK_COLOR:-2}" +MINIMAL_USER_CHAR="${MINIMAL_USER_CHAR:-λ}" +MINIMAL_INSERT_CHAR="${MINIMAL_INSERT_CHAR:-›}" +MINIMAL_NORMAL_CHAR="${MINIMAL_NORMAL_CHAR:-·}" +MINIMAL_PWD_LEN="${MINIMAL_PWD_LEN:-2}" +MINIMAL_PWD_CHAR_LEN="${MINIMAL_PWD_CHAR_LEN:-10}" +MINIMAL_MAGIC_ENTER_MARGIN="${MINIMAL_MAGIC_ENTER_MARGIN:- | }" + +# check if function exists +function _isfn { + type -w "$1" | grep -wq function +} + +# Extensions +if ! _isfn minimal_magic_output; then + function minimal_magic_output { + minimal_magic_output_base + } +fi + +if ! _isfn minimal_vcs; then + function minimal_vcs { + # git + local statc="%{\e[0;3${MINIMAL_OK_COLOR}m%}" # assumes is clean + local bname="$(git rev-parse --abbrev-ref HEAD 2> /dev/null)" + + if [ -n "$bname" ]; then + [ -n "$(git status --porcelain 2> /dev/null)" ] &&\ + statc="%{\e[0;31m%}" + echo -n " $statc$bname%{\e[0m%}" + fi + } +fi + +if ! _isfn minimal_env; then + function minimal_env { + # python virtual env + if [ -n "$VIRTUAL_ENV" ]; then + _venv="$(basename $VIRTUAL_ENV)" + echo -n "${_venv%%.*} " + fi + } +fi + +# Setup +autoload -U colors && colors +setopt prompt_subst + +_grey="\e[38;5;244m" +_greyp="%{$_grey%}" + +# Left Prompt +function minimal_lprompt { + local user_status="%{\e[%(1j.4.0);3%(0?.$MINIMAL_OK_COLOR.1)m%}\ +%(!.#.$MINIMAL_USER_CHAR)" + local kmstatus="$MINIMAL_INSERT_CHAR" + [ "$KEYMAP" = 'vicmd' ] && kmstatus="$MINIMAL_NORMAL_CHAR" + + echo -n "$user_status%{\e[0m%} $kmstatus" +} + +function minimal_ps2 { + local kmstatus="$MINIMAL_INSERT_CHAR" + local offset="$((${#_venv} + 2))" + [ "$KEYMAP" = 'vicmd' ] && kmstatus="$MINIMAL_NORMAL_CHAR" + + printf " %.0s" {1..$offset} + echo -n "$kmstatus" +} + +# Right Prompt +function minimal_path { + local w="%{\e[0m%}" + local cwd="%${MINIMAL_PWD_LEN}~" + local pi="" + local len="$MINIMAL_PWD_CHAR_LEN" + [ "$len" -lt 4 ] && len=4 + local hlen=$((len / 2 - 1)) + cwd="${(%)cwd}" + cwd=("${(@s:/:)cwd}") + + for i in {1..${#cwd}}; do + pi="$cwd[$i]" + [ "${#pi}" -gt "$len" ] && cwd[$i]="${pi:0:$hlen}$w..$_greyp${pi: -$hlen}" + done + + echo -n "$_greyp${(j:/:)cwd//\//$w/$_greyp}$w" +} + +# Magic Enter +function minimal_infoline { + local last_err="$1" + local w="\e[0m" + local rn="\e[0;31m" + local rb="\e[1;31m" + + local user_host_pwd="$_grey%n$w@$_grey%m$w:$_grey%~$w" + user_host_pwd="${${(%)user_host_pwd}//\//$w/$_grey}" + + local v_files="$(ls -1 | sed -n '$=')" + local h_files="$(ls -1A | sed -n '$=')" + + local job_n="$(jobs | sed -n '$=')" + + local iline="[$user_host_pwd] [$_grey${v_files:-0}$w ($_grey${h_files:-0}$w)]" + [ "$job_n" -gt 0 ] && iline="$iline [$_grey$job_n$w&]" + + if [ "$last_err" != "0" ]; then + iline="$iline \e[1;31m[\e[0;31m$last_err\e[1;31m]$w" + fi + + echo "$iline" +} + +function minimal_magic_output_base { + local margin="${#MINIMAL_MAGIC_ENTER_MARGIN}" + + if [ "$(dirs -p | wc -l)" -gt 1 ]; then + local stack="$(dirs)" + echo "[${_grey}dirs\e[0m - ${_grey}${stack//\//\e[0m/$_grey}\e[0m]" + fi + + if [ "$(uname)" = "Darwin" ] && ! ls --version &> /dev/null; then + COLUMNS=$((COLUMNS - margin)) CLICOLOR_FORCE=1 ls -C -G + else + ls -C --color="always" -w $((COLUMNS - margin)) + fi + + git -c color.status=always status -sb 2> /dev/null +} + +function minimal_wrap_output { + local output="$1" + local output_len="$(echo "$output" | sed -n '$=')" + if [ -n "$output" ]; then + if [ "$output_len" -gt "$((LINES - 2))" -a -n "$PAGER" ]; then + printf "$output\n" | "$PAGER" -R + else + printf "$output\n" | sed "s/^/$MINIMAL_MAGIC_ENTER_MARGIN/" + fi + fi +} + +function minimal_magic_enter { + local last_err="$?" # I need to capture this ASAP + + if [ -z "$BUFFER" ]; then + minimal_infoline $last_err + minimal_wrap_output "$(minimal_magic_output)" + zle redisplay + else + zle accept-line + fi +} + +# Apply Switches +if [ "$MINIMAL_PROMPT" = "yes" ]; then + # prompt redraw on vimode change + function reset_prompt { + zle reset-prompt + } + + zle -N zle-line-init reset_prompt + zle -N zle-keymap-select reset_prompt + + PROMPT='$(minimal_env)$(minimal_lprompt) ' + PS2='$(minimal_ps2) ' + [ "$MINIMAL_RPROMPT" = "yes" ] && RPROMPT='$(minimal_path)$(minimal_vcs)' +fi + +if [ "$MINIMAL_MAGIC_ENTER" = "yes" ]; then + zle -N minimal-magic-enter minimal_magic_enter + bindkey -M main "^M" minimal-magic-enter + bindkey -M vicmd "^M" minimal-magic-enter +fi diff --git a/zsh/.zsh/startup.zsh b/zsh/.zsh/startup.zsh new file mode 100644 index 0000000..f09fbfd --- /dev/null +++ b/zsh/.zsh/startup.zsh @@ -0,0 +1,3 @@ +# Startup archey3 +archey3 +cd diff --git a/zsh/.zshrc b/zsh/.zshrc new file mode 100644 index 0000000..5cd69b5 --- /dev/null +++ b/zsh/.zshrc @@ -0,0 +1,27 @@ +# Lines configured by zsh-newuser-install +HISTFILE=~/.histfile +HISTSIZE=100000 +SAVEHIST=1000000 +setopt appendhistory notify +bindkey -e +# End of lines configured by zsh-newuser-install +# The following lines were added by compinstall + +zstyle ':completion:*' completer _expand _complete _ignored _approximate +zstyle ':completion:*' list-colors '' +zstyle ':completion:*' matcher-list '' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' +zstyle ':completion:*' max-errors 10 +zstyle ':completion:*' prompt ' ' +zstyle :compinstall filename '/home/yannherklotz/.zshrc' + +autoload -Uz compinit +compinit +# End of lines added by compinstall + +# Sourcing everything +source /usr/share/fzf/key-bindings.zsh +source /usr/share/fzf/completion.zsh +source $HOME/.zsh/export.zsh +source $HOME/.zsh/minimal.zsh +source $HOME/.zsh/function.zsh +source $HOME/.zsh/startup.zsh -- cgit