Zsh

<< Back to wiki homepage

Table of contents:

Bash-style # comments

setopt interactivecomments

Automatically cd with only dir name

setopt auto_cd

Requirements: ripgrep, fzf

Binding to Alt-Enter here, use whatever you wish

# Helper to check if a command exists
# replaces uglier: if type "somecommand" > /dev/null 2>&1; then
_exists() { (( $+commands[$1] )) }

function _fuzzy_search_current_word() {
    CURRENTWORD="${LBUFFER/* /}${RBUFFER/ */}"
    # If there is no word entered, do nothing
    [[ -z $CURRENTWORD ]] && return
    # Pass current subtree to fzf
    # --height parameter is buggy here
    FZFRESULT=`rg --files --no-ignore-vcs --hidden | fzf --reverse -q $CURRENTWORD`
    # Replace the word with selected result
    BUFFER=`echo "$BUFFER" | sed "s#${CURRENTWORD}#${FZFRESULT}#"`
    zle end-of-line
}

# Bindkey should be initialized before
# Normally this is not an issue,
# but hacks like tmux doesn't work with this for some bullshit reason
bindkey -e
# Check and enable if we have binaries in PATH
_exists fzf && _exists rg && zle -N _fuzzy_search_current_word
_exists fzf && _exists rg && bindkey '^[^M' _fuzzy_search_current_word

Disable system sound

Who likes it anyway?

setopt NO_BEEP

Search and kill a process

Requirements: toilet (for visual purposes)

Usage: fuck nautilus (or any other unique term to grep process)

function fuck() {
    VICTIMS=`pgrep -a $1`
    if [[ "${VICTIMS}" != "" ]];then
        echo "Found processes:\n${VICTIMS}"
        echo "kill? (y/N)"
        read isKill
        if [[ $isKill == "y" ]]; then
            pkill -9 $1 > /dev/null 2>&1
            echo "\n (╯°□°)╯︵$(echo "$1"|toilet -f term -F rotate)\n"
        fi
    fi
}

Random number generator

Usage: random_number 200 (or any other value as top limit)

function random_number() {
    if [[ "$1" == "" ]]; then
        echo "I need a number as top value"
    else
        echo $[${RANDOM}%$1]
    fi
}

Only complete directories with cd

compctl -/ cd

Disable confirmation with *

setopt rm_star_silent

Auto-add "sudo"

Forgot to type sudo on your current or last command?

Just press Esc twice.. Or whatever shortcut you bind.

sudo-command-line() {
    [[ -z $BUFFER ]] && zle up-history
    if [[ $BUFFER == sudo\ * ]]; then
        LBUFFER="${LBUFFER#sudo }"
    elif [[ $BUFFER == $EDITOR\ * ]]; then
        LBUFFER="${LBUFFER#$EDITOR }"
        LBUFFER="sudoedit $LBUFFER"
    elif [[ $BUFFER == sudoedit\ * ]]; then
        LBUFFER="${LBUFFER#sudoedit }"
        LBUFFER="$EDITOR $LBUFFER"
    else
        LBUFFER="sudo $LBUFFER"
    fi
}
zle -N sudo-command-line
# Defined shortcut keys: [Esc] [Esc]
bindkey "\e\e" sudo-command-line

I wanted this because I don't like to have a sticky RPROMPT which causes problems on copy/paste.

Be aware that you can also achieve command timings with: fc -liD

This will print the start time right after the command, but only if you finish the command with a semicolon.

showshow () {
    if [[ "${1}" != *";" ]]; then
        return
    fi

    DATE="# started at $( date +'%Y/%m/%d %H:%M:%S' )"
    local len_prompt=$( strlen "$PROMPT" )
    local len_command=$( strlen "${1}" )
    local start_from=$(($len_prompt + $len_command + 2))

    local len_cmd=$( strlen "$@" )
    local len_prompt=$(strlen "$PROMPT" )
    local len_left=$(($len_cmd+$len_prompt))

    RDATE="\033[${start_from}C ${DATE}"

    echo -e "\033[1A${RDATE}"

}
preexec_functions=($preexec_functions showshow)