TIL

Switch to previous session in tmux

14.02.2023

I recently setup <prefix> + to switch to the previous tmux session. Super handy to switch fast between two projects, for example backend and frontend.

bind + switch-client -l

tx

03.02.2023

I have recently been trying to do a proper setup of tmux and as a part of it i made this little shell function to make re-attaching sessions a bit easier. It will give fzf prompt with available sessions if called without a session name. On the other hand when called with a session name it will check if it exists and attach or create based on the result.

tx() {
if [ -z ${1+x} ]; then
session=$(tmux list-sessions | fzf | awk '{print $1}' | sed 's/:$//' )
tmux a -t $session
else
if tmux list-sessions | grep $1 2>&1 > /dev/null; then
tmux a -t $1
else
tmux new -s $1
fi
fi
}
_tx() {
sessions=$(tmux list-sessions 2>/dev/null)
if [[ -z "$1" ]]; then
echo $sessions | awk '{split($0,a,":"); print a[1]}'
else
echo $sessions | awk '{split($0,a,":"); print a[1]}' | grep --color=never $1
fi
}
complete -C _tx tx

Alias filetypes

29.01.2023

Creating aliases with -s enables aliasing commands too files types. Thus, with the alias alias -s txt=less turns ./file.txt into less ./file.txt

Other examples:

alias -s md=glow
alias -s html=open
alias -s txt=less
alias -s json=jq

Recent branches with fzf

21.01.2023

Not something I learned today, but i recently configured a alias for recent branches with fuzzy search with fzf. It will show the branches sorted by the date of the head commit when doing git recent. Selecting one with fzf will then do a git checkout of that branch.

Add the following under aliases in gitconfig:

recent = "!git branch --sort=-committerdate --format=\"%(committerdate:relative)%09%(refname:short)\" | fzf --ansi | awk 'BEGIN{FS=\"\t\"} {print $2}' | xargs git checkout"

zz in vim centers line on screen

15.01.2023

Learned today that in normal mode in vim typing zz will center the active line in the middle of the screen.