Skip to content

Instantly share code, notes, and snippets.

@knubie
Last active September 13, 2017 10:12
Show Gist options
  • Save knubie/5789466 to your computer and use it in GitHub Desktop.
Save knubie/5789466 to your computer and use it in GitHub Desktop.
Experimental zsh/tmux set up. Display CWD/vim file and git repo information in the tmux status bar.
# Update the status bar by fetching an environment variable set earlier by a shell script.
# Requires Thomas Adam's hook patch for tmux
# https://github.com/ThomasAdam/tmux/tree/ta/hooks
set-hook -g -n 'after-select-window' 'run "tmux set -q status-left \"`tmux show -environment TMUX_STATUS_#I | cut -c 15-`\""'
set-hook -g -n 'after-kill-window' 'run "tmux set -q status-left \"`tmux show -environment TMUX_STATUS_#I | cut -c 15-`\""'
" Update tmux statusbar
autocmd BufEnter * silent execute '!tmux_set_status.sh ' . expand("%:t") . ' ' . expand("%:h")
autocmd BufWinEnter * silent execute '!tmux_set_status.sh ' . expand("%:t") . ' ' . expand("%:h")
autocmd BufWritePost * silent execute '!tmux_set_status.sh ' . expand("%:t") . ' ' . expand("%:h")
line_divider(){
echo "${(l.$COLUMNS..—.)}"
}
PROMPT="%{$fg_bold[magenta]%}$(line_divider)\n%{$fg[white]%}➥%{$reset_color%} "
precmd () {
# Check if tmux is running
if [ "$TERM" = "screen-256color" ] && [ -n "$TMUX" ]; then
# Run the script that sets the tmux statusbar and environment variable.
tmux_set_status.sh
fi
}
#!/bin/zsh
CURRENT_DIR=${PWD##*/}
FILENAME=$1
FILEPATH=$2
# Get filename if inside vim
file() {
if [[ $FILENAME == "" ]]; then
echo ""
else
if [[ $FILEPATH == "." ]]; then
echo "#[default]#[fg=cyan]$FILENAME"
else
echo "$FILEPATH/#[default]#[fg=cyan]$FILENAME"
fi
fi
}
# Get the current git repo branch
branch() {
echo $(git symbolic-ref HEAD 2>/dev/null | awk -F/ {'print $NF'})
}
# Get whether branch needs to be synced.
sync () {
unpushed=$(git cherry -v @{upstream} 2>/dev/null)
if [[ $unpushed == "" ]]; then
echo ""
else
echo "⟳#[default]"
fi
}
# Display git information
git() {
st=$(git status 2>/dev/null | tail -n 1)
if [[ $st == "" ]]; then
echo ""
else
if [[ $st == "nothing to commit, working directory clean" ]]; then
echo " #[fg=cyan]on #[fg=white,bold]$(branch)$(sync)"
else
echo " #[fg=cyan]on #[fg=black,bold]$(branch)$(sync)"
fi
fi
}
# Set tmux statusbar (remind me to refactor this)
# if the CWD begins with '/' ie. outside the CWD
if [[ `echo $FILEPATH | cut -c1` == '/' ]]; then
# Set environment variable for retrieval later when switching windows.
tmux setenv TMUX_STATUS_$(tmux display -p "#I") " #[fg=cyan,bold]$(file)#[default]"
# Set tmux status line itself.
tmux set -q status-left " #[fg=cyan,bold]$(file)#[default]"
else
# Set environment variable for retrieval later when switching windows.
tmux setenv TMUX_STATUS_$(tmux display -p "#I") " #[fg=cyan,bold]$CURRENT_DIR/$(file)#[default]$(git)"
# Set tmux status line itself.
tmux set -q status-left " #[fg=cyan,bold]$CURRENT_DIR/$(file)#[default]$(git)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment