dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE

.zshrc (6051B)


      1 #### ZSH PERFORMANCE DEBUG
      2 debug_startup=false
      3 [ "$debug_startup" = true ] && zmodload zsh/zprof
      4 
      5 
      6 #### ZSH OPTIONS
      7 
      8 # man zshoptions
      9 setopt append_history       # zsh sessions append their history to the hist file
     10 setopt autopushd            # make cd push the old dir to the dir stack
     11 setopt extendedglob         # globs (. is file, / is dir) see `man zshexpn`
     12 setopt hist_ignore_dups     # ignore duplicate cmds in history
     13 setopt histignorespace      # don't store cmds prefixed with space in history
     14 setopt interactivecomments  # allow in-line comments in prompt
     15 setopt prompt_subst         # perform param exp, cmd subst, arith exp in prompt
     16 setopt share_history        # import new commands from the history file
     17 unsetopt autocd             # cd into directory if called by name
     18 unsetopt beep               # beep on error
     19 unsetopt hist_verify        # verify before executing cmd from history (e.g. !!)
     20 unsetopt notify             # report status of bg jobs immediately
     21 unsetopt nomatch            # show error if wildcards do not match any files
     22 
     23 autoload -Uz add-zsh-hook cdr chpwd_recent_dirs colors compinit \
     24 	edit-command-line select-bracketed select-quoted zmv
     25 
     26 compinit
     27 colors
     28 add-zsh-hook chpwd chpwd_recent_dirs
     29 
     30 zstyle completion:*:*:cdr:*:* menu selection
     31 zstyle :chpwd:* recent-dirs-insert fallback
     32 zstyle :chpwd:* recent-dirs-pushd true
     33 
     34 zle -N edit-command-line
     35 zle -N select-bracketed
     36 zle -N select-quoted
     37 zle -N fzf-history-widget
     38 zle -N history-substring-search-up
     39 zle -N history-substring-search-down
     40 
     41 HISTFILE=~/.zhistory
     42 HISTSIZE=$((2 ** 16))
     43 SAVEHIST=$((2 ** 17))
     44 
     45 #### BINDINGS
     46 
     47 set -o vi
     48 bindkey -v
     49 bindkey jk vi-cmd-mode
     50 bindkey -M vicmd v edit-command-line
     51 bindkey -a ? fzf-history-widget
     52 bindkey -a k history-substring-search-up
     53 bindkey -a j history-substring-search-down
     54 bindkey '^R' history-incremental-search-backward
     55 bindkey "^j" history-beginning-search-backward
     56 bindkey "^k" history-beginning-search-forward
     57 bindkey '^P' up-line-or-search
     58 bindkey '^N' down-line-or-search
     59 bindkey '^ ' autosuggest-accept
     60 bindkey '^_' autosuggest-execute
     61 
     62 # use Ctrl-Z as fg
     63 _foreground() {
     64 	if [[ $#BUFFER -eq 0 ]]; then
     65 		BUFFER="fg"
     66 		zle accept-line
     67 	else
     68 		zle push-input
     69 		zle clear-screen
     70 	fi
     71 }
     72 zle -N _foreground
     73 bindkey '^Z' _foreground
     74 
     75 # launch $EDITOR with Ctrl-e
     76 _editor() {
     77 	BUFFER="$EDITOR"
     78 	zle accept-line
     79 }
     80 zle -N _editor
     81 bindkey '^e' _editor
     82 
     83 # launch $EDITOR+fzf with Ctrl-f
     84 _editor_fuzzy_find() {
     85 	#BUFFER="$EDITOR -c ':fzf'"      # (n)vim
     86 	BUFFER="$EDITOR +':fzf'"         # vis
     87 	zle accept-line
     88 }
     89 zle -N _editor_fuzzy_find
     90 bindkey '^f' _editor_fuzzy_find
     91 
     92 # launch $EDITOR+fzf for recent files with Ctrl-o
     93 _editor_fuzzy_history() {
     94 	#BUFFER="$EDITOR -c ':History'"  # (n)vim
     95 	BUFFER="$EDITOR +':fzfmru'"      # vis
     96 	zle accept-line
     97 }
     98 zle -N _editor_fuzzy_history
     99 bindkey '^o' _editor_fuzzy_history
    100 
    101 # launch $EDITOR+fzf+rg with Ctrl-g
    102 _editor_fuzzy_grep() {
    103 	#BUFFER="$EDITOR -c ':Rg'"       # (n)vim
    104 	BUFFER="$EDITOR +':Rg'"          # vis
    105 	zle accept-line
    106 }
    107 zle -N _editor_fuzzy_grep
    108 bindkey '^g' _editor_fuzzy_grep
    109 
    110 # launch $TERMINAL with Alt-p
    111 _terminal() {
    112 	BUFFER="setsid $TERMINAL >/dev/null 2>&1"
    113 	zle accept-line
    114 }
    115 zle -N _terminal
    116 bindkey '\ep' _terminal
    117 
    118 # remember -n flag for dry runs
    119 alias zmv='noglob zmv'
    120 alias zcp='noglob zmv -C'
    121 alias zln='noglob zmv -L'
    122 alias zsy='noglob zmv -Ls'
    123 
    124 
    125 #### ZSH APPEARANCE
    126 
    127 # show execution time of previous command if more than 1 sec
    128 convertsecs() {
    129 	((d=${1}/3600/24))
    130 	((h=${1}/3600%24))
    131 	((m=(${1}%3600)/60))
    132 	((s=${1}%60))
    133 	if [ "$d" -gt "0" ]; then
    134 		printf " %dd%02dh%02dm%02ds" $d $h $m $s
    135 	elif [ "$h" -gt "0" ]; then
    136 		printf " %dh%02dm%02ds" $h $m $s
    137 	elif [ "$m" -gt "0" ]; then
    138 		printf " %dm%02ds" $m $s
    139 	elif [ "$s" -gt "0" ]; then
    140 		printf " %ds" $s
    141 	fi
    142 }
    143 preexec() {
    144 	timer=${timer:-$SECONDS}
    145 }
    146 precmd() {
    147 	if [ $timer ]; then
    148 		timer_show=$(convertsecs $(($SECONDS - $timer)))
    149 		export EXECTIME="${timer_show}"
    150 		unset timer
    151 	fi
    152 }
    153 
    154 _fix_cursor() {
    155 	echo -ne '\e[5 q'
    156 }
    157 
    158 precmd_functions+=(_fix_cursor)
    159 
    160 git_branch() {
    161 	branch_name=$(git symbolic-ref --short HEAD 2> /dev/null)
    162 	[ -n "$branch_name" ] && echo "$branch_name$(git_modified) " || :
    163 }
    164 
    165 git_grep_modified_files() {
    166 	grep -e "^.M" -e "^M." -e "^A." -e "^D." -e "^.D"
    167 }
    168 
    169 git_modified() {
    170 	 [ -n "$(git status --porcelain 2> /dev/null | \
    171 	 	git_grep_modified_files)" ] && echo "*" || :
    172 }
    173 
    174 function prompt_with_vimode {
    175 	echo
    176 	echo -n '%(1j.%jbg .)'  # background jobs
    177 	echo -n "$1"
    178 	[ -z "$1" ] &&  # show prompt symbol if no insert/normal mode is passed
    179 		echo -n '%(!.%{$fg_bold[red]%}#.$)' || :
    180 	echo -n '%{$reset_color%} '
    181 }
    182 
    183 insert_mode=''
    184 normal_mode='N'  # normal mode indicator
    185 
    186 rprompt() {
    187 	echo -n "%(?..%{$fg[red]%}[%?]%{$reset_color%})"  # return status
    188 	echo -n "${EXECTIME} "  # runtime of prev cmd
    189 	echo -n "$(git_branch)"
    190 	echo -n "%~ "  # pwd
    191 	[ "$USER" != "ad" ] && echo -n "%n@" || :
    192 	[ "$HOST" != "idkfa" ] && echo -n "%m" || :
    193 }
    194 
    195 set_prompt() {
    196 	PROMPT="$(prompt_with_vimode $insert_mode)"
    197 	RPROMPT="$(rprompt)"
    198 }
    199 add-zsh-hook precmd set_prompt
    200 
    201 function zle-keymap-select {
    202 	if [[ ${KEYMAP} == vicmd ]] || [[ $1 = 'block' ]]; then
    203 		echo -ne '\e[1 q'
    204 	elif [[ ${KEYMAP} == main ]] || [[ ${KEYMAP} == viins ]] ||
    205 		[[ ${KEYMAP} = '' ]] || [[ $1 = 'beam' ]]; then
    206 		echo -ne '\e[5 q'
    207 	fi
    208 }
    209 zle -N zle-keymap-select
    210 
    211 
    212 #### PLUGINS
    213 if [ -f "$HOME/.config/fffs/lib/zsh/sources" ]; then
    214 	. "$HOME/.config/fffs/lib/zsh/sources"
    215 else
    216 	fffs zsh init
    217 	. "$HOME/.config/fffs/lib/zsh/sources"
    218 fi
    219 
    220 
    221 #### EXTRA COMMANDS
    222 
    223 # pass **<tab>
    224 _fzf_complete_pass() {
    225 	_fzf_complete "--no-multi --preview-window=right:hidden" "$@" < <(
    226 	pwdir=${PASSWORD_STORE_DIR-~/.password-store/}
    227 	stringsize="${#pwdir}"
    228 	find "$pwdir" -name "*.gpg" -print |
    229 		cut -c "$((stringsize + 1))"-  |
    230 		sed -e 's/\(.*\)\.gpg/\1/'
    231 	)
    232 }
    233 
    234 [ -f /usr/share/fzf/key-bindings.zsh ] && \
    235 	. /usr/share/fzf/key-bindings.zsh ] || :
    236 [ -f /usr/share/doc/fzf/key-bindings.zsh ] && \
    237 	. /usr/share/doc/fzf/key-bindings.zsh ] || :
    238 
    239 # report startup diagnostics if requested
    240 [ "$debug_startup" = true ] && zprof || :