Zsh

Table of contents:

Bash-style # comments

setopt interactivecomments

Automatically cd with only dir name

setopt auto_cd

Requirements: ripgrep, skim

Binding to Alt-Enter here, use whatever you wish

function _fuzzy_search_current_word() {
    CURRENTWORD="${LBUFFER/* /}${RBUFFER/ */}"
    # If there is no world entered, do nothing
    [[ -z $CURRENTWORD ]] && return
    # Pass current subtree to skim
    # SKIMRESULT=`find . | sk -q $CURRENTWORD`
    # ripgrep seems faster
    SKIMRESULT=`rg --files --no-ignore-vcs --hidden | sk -q $CURRENTWORD`
    # Replace the word with selected result
    BUFFER=`echo "$BUFFER" | sed "s#${CURRENTWORD}#${SKIMRESULT}#"`
    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 skim in PATH, if yes, enable the function
if type "sk" > /dev/null 2>&1; then
    zle -N _fuzzy_search_current_word
    # Shortcut for fuzzy = Alt - enter
    bindkey '^[^M' _fuzzy_search_current_word
fi

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