SEANK.H.LIAO

things in my zsh config

dot dot dot

zsh config

Some of us lug around giant config files that improve our quality of life, Here are some things I stuffed in mine:

aliases

pyramid of dots

mash dots until you get to the right level, though I don't usually go above 5 (or I get it wrong). Along with setopt AUTOCD this helps navigation a lot.

 1alias ..='cd ..'
 2alias ...='cd ../..'
 3alias ....='cd ../../..'
 4alias .....='cd ../../../..'
 5alias ......='cd ../../../../..'
 6alias .......='cd ../../../../../..'
 7alias ........='cd ../../../../../../..'
 8alias .........='cd ../../../../../../../..'
 9alias ..........='cd ../../../../../../../../..'
10alias ...........='cd ../../../../../../../../../..'
11alias ............='cd ../../../../../../../../../../..'
go to repo root

Sometimes you just want to go back to the top

1alias rr='cd $(git rev-parse --show-toplevel)'
common tools

There are just some things I use a lot.

1alias cp='cp -v'
2alias ln='ln -v'
3alias mv='mv -v'
4alias g='git'
5alias h='htop'
6alias k='kubectl'
7alias s='ssh'
8alias tf='terraform'
9alias v='${EDITOR}'
better ls

I want a better ls with exa, but I don't want my config to break every time I clone it onto a new machine.

1(( $+commands[exa] )) \
2    && alias ll='exa -l -a --git --time-style iso --group-directories-first' \
3    || alias ll='ls -alh';
git aliases

turn aliases defined in ~/.config/git/config into zsh aliases as g<git alias>

 1# short git aliases
 2function {
 3    local gitconfig="${XDG_CONFIG_HOME}/git/config"
 4    [[ -f "${gitconfig}" ]] || gitconfig="${HOME}/.gitconfig"
 5    local start_alias=false
 6    while read line; do
 7        if "${start_alias}"; then
 8            [[ "${line}" =~ '\s*\[[a-z]*\]' ]] && return
 9            sub="${${line%%=*}// /}"
10            [[ "${sub}" ]] && alias g${sub}="git ${sub}"
11        else
12            [[ $line =~ '\s*\[alias\]' ]] && start_alias=true
13        fi
14    done < "${gitconfig}"
15}

functions

mkdir and cd

really simple and common thing

1function md() {
2    [[ -z ${1// } ]] && echo "no directory name given" && return 1
3    mkdir -p "$1" && cd "$1"
4}
t

wrapper for t, population numbered aliases from the results of ripgrep

1function t() {
2    command t -i "$@"
3    source /tmp/t_aliases 2>/dev/null
4}
testrepo

creates a temporary unique repo with an autoincrementing number

 1function testrepo() {
 2    local vers=$(( $(cat ${XDG_CONFIG_HOME}/testrepo-version)+1))
 3    echo ${vers} > ${XDG_CONFIG_HOME}/testrepo-version
 4    mkdir -p ${HOME}/tmp
 5    cd ${HOME}/tmp
 6    mr testrepo-${vers}
 7}
 8
 9function mr() {
10    local repo=${1// }
11    [[ -z ${repo} ]] && echo "no repo name given" && return 1
12    mkdir -p ${repo}
13    cd ${repo}
14    git init
15    git commit --allow-empty -m "root-commit"
16    git remote add origin s:${repo}
17}

zle

double tap ESC to get the previous command with sudo in front toggled

1zle -N _sudo_cmdline
2
3function _sudo_cmdline() {
4    [[ -z ${BUFFER} ]] && zle up-history
5    [[ ${BUFFER} == sudo\ * ]] && BUFFER=${BUFFER#sudo } || BUFFER="sudo ${BUFFER}"
6}
7
8# ^[ == escape
9bindkey '^[^[' _sudo_cmdline

prompt

my prompt looks like

113:38:33 ~/.config/zsh 0:00:06
2main »

Which is:

1time current-working-directory previous-command-execution-time
2[screen]-[git-repo-branch] [ssh-user@host]»

With things in [] only showing if they have a valid value, and [ssh-user@host]» changing color based on if the previous command exited with 0

 1#!/usr/bin/env zsh
 2
 3export PROMPT_EOL_MARK=''
 4
 5function _preexec() {
 6    typeset -g prompt_timestamp=$EPOCHSECONDS
 7}
 8
 9function _precmd() {
10    integer elapsed=$(( EPOCHSECONDS - ${prompt_timestamp:-$EPOCHSECONDS} ))
11    local human="$(( elapsed / 3600 )):${(l:2::0:)$(( elapsed / 60 % 60 ))}:${(l:2::0:)$(( elapsed % 60 ))}"
12    vcs_info 2>&1 >/dev/null
13    local newline=$'\n%{\r%}'
14
15    PROMPT="%F{green}%*%f %F{blue}%~%f %F{yellow}${human}%f"
16    PROMPT+="${newline}"
17    PROMPT+="%F{242}${STY:+screen-}${VIRTUAL_ENV:+venv-}${vcs_info_msg_0_:+${vcs_info_msg_0_} }%f"
18    PROMPT+="%(?.%F{magenta}.%F{red})${SSH_CONNECTION+%n@%m}»%f "
19}
20
21add-zsh-hook precmd  _precmd
22add-zsh-hook preexec _preexec