dotfiles

Alpine Linux dotfiles

git clone git://git.lin.moe/dotfiles.git

  1# Try to start tmux first, avoid load zsh configs multiple times
  2# Auto start tmux when connect with ssh and SSHTMUX env setted
  3if [[ -v SSHTMUX ]] && [[ -x "$(command -v tmux)" ]]; then
  4	if [[ $- =~ i ]] && [[ -z "$TMUX" ]] && [[ -n "$SSH_TTY" ]]; then
  5		exec tmux new-session -A -s  ${HOST%%.*}
  6	fi
  7fi
  8
  9# config history
 10HISTFILE=~/.histfile
 11HISTSIZE=10000
 12SAVEHIST=10000
 13setopt EXTENDED_HISTORY
 14setopt HIST_EXPIRE_DUPS_FIRST
 15setopt HIST_IGNORE_DUPS
 16setopt HIST_IGNORE_SPACE
 17setopt HIST_REDUCE_BLANKS
 18setopt HIST_FIND_NO_DUPS
 19setopt HIST_SAVE_NO_DUPS
 20
 21bindkey -e
 22
 23# set path
 24[ $path[(Ie)$HOME/.local/bin] -eq "0" ] && path+=("$HOME/.local/bin")
 25[ -x "$(command -v go)" ] && [ $path[(Ie)$(go env GOPATH)/bin] -eq "0" ] && path+=("$(go env GOPATH)/bin")
 26[ -z "$PYTHONUSERBASE" ] || path+=($PYTHONUSERBASE/bin)
 27
 28# auto completions
 29autoload -Uz compinit promptinit
 30compinit
 31
 32# prompt
 33promptinit
 34# prompt walters
 35
 36# vcs
 37autoload -Uz vcs_info
 38
 39setopt PROMPT_SUBST
 40vcs_info 2>/dev/null
 41if [ $? -eq 0 ]; then
 42	add-zsh-hook -Uz precmd vcs_info
 43	zstyle ':vcs_info:git:*' formats '%F{cyan}[%b]%f '
 44  PROMPT_VCS_PREFIX='${vcs_info_msg_0_}'
 45fi
 46export PROMPT_EOL_MARK=''
 47
 48NEWLINE=$'\n'
 49PROMPT_HOST='%n'
 50[[ -n $SSH_CLIENT ]] && PROMPT_HOST+='@%U%m%u'
 51_CURRENTNET=$(ip netns identify $$ 2>/dev/null)
 52[[ -n "$_CURRENTNET" ]] && PROMPT_NETNS="%F{magenta}<$_CURRENTNET>%f "
 53
 54PROMPT='$PROMPT_NETNS'"$PROMPT_VCS_PREFIX"'%F{yellow}${PROMPT_HOST} %F{blue}%~ ${LINEUP}%F{green}<%D{%H:%M}>%f'
 55PROMPT+="${NEWLINE}%B%F{red}%(?..[%?] )%f$%b "
 56
 57# hooks
 58function xterm_title_precmd () {
 59	print -Pn '\e]2;%n@%m %1~\a'
 60}
 61function xterm_title_preexec () {
 62	print -Pn '\e]2;%n@%m %1~ %# '
 63	print -n "${(q)1}\a"
 64}
 65
 66function source_env {
 67	if [[ -x .env ]]
 68	then
 69		set -a; source .env; set +a
 70	fi
 71}
 72
 73function py_venv {
 74	PYENV=${PYENV:-venv}
 75
 76	if [[ -f "$PYENV/bin/activate" ]]; then
 77		if [[ -n "$VIRTUAL_ENV" && $(realpath "$VIRTUAL_ENV") != $(realpath "$PYENV") ]]; then
 78			# if running venv is not the same as current one, deavtivate it first
 79			deactivate
 80		fi
 81
 82		if [ -z "$VIRTUAL_ENV" ]; then
 83			# if no running venv(avoid activate multiple times), active current one
 84			. $PYENV/bin/activate
 85		fi
 86
 87		# do nothing if current one is activated
 88	else
 89		unset PYENV
 90	fi 
 91
 92	if [[ -n "$VIRTUAL_ENV" && -z "$PYENV" ]]; then
 93		if [[ $PWD != $(dirname $VIRTUAL_ENV)/* ]]; then
 94		    deactivate
 95		fi
 96	fi
 97}
 98
 99pipe_command_output_precmd() {
100	if ! builtin zle; then
101		print -n "\e]133;D\e\\"
102	fi
103}
104pipe_command_output_preexec() {
105  print -n "\e]133;C\e\\"
106}
107
108if [[ "$TERM" == (screen*|xterm*|rxvt*) ]]; then
109	add-zsh-hook -Uz precmd xterm_title_precmd
110	add-zsh-hook -Uz preexec xterm_title_preexec
111
112	add-zsh-hook -Uz precmd pipe_command_output_precmd
113	add-zsh-hook -Uz preexec pipe_command_output_preexec
114	# add-zsh-hook chpwd source_env
115	# add-zsh-hook chpwd py_venv
116fi
117
118
119# active when start zsh in home dir
120# source_env
121# py_venv
122
123# other configuration files
124if [ -f "$HOME/.zshrc" ]; then source $HOME/.zshrc; fi
125
126if [ -f "$ZDOTDIR/.zshalias" ]; then source $ZDOTDIR/.zshalias; fi
127
128if [ -f "$HOME/.zshalias" ]; then source $HOME/.zshalias; fi
129
130# launch applications
131[[ -x "$(command -v gpgconf)" && -d "$HOME/.gnupg" && ! -S $HOME/.gnupg/S.gpg-agent ]] && \
132    gpgconf --launch gpg-agent && gpg-connect-agent updatestartuptty /bye >/dev/null
133
134
135if [ -f "/usr/share/fzf/completion.zsh" ]; then source /usr/share/fzf/completion.zsh; fi
136if [ -f "/usr/share/fzf/key-bindings.zsh" ]; then source /usr/share/fzf/key-bindings.zsh; fi
137export FZF_CTRL_T_COMMAND="command find -L . -mindepth 1 -maxdepth 3 \\( -path '*/.*' \\) -prune \
138       -o -type f -print \
139       -o -type d -print \
140       -o -type l -print 2> /dev/null | cut -b3-"
141
142stty -ixon