Don't look back, remove your shell history!


For most of the time I can remember using Linux I kept as much shell history as possible, I commonly had history files with thousands of lines of history. One of my sources of pride was this function to selectively choose what to save and not

HISTSIZE=5000
SAVEHIST=5000
HISTIGNORE=(mpv mplayer player many many commands zless)
HISTRESTRICTED=(nova neutron glance heat cinder keystone sudo git)
RESTRICTEDWORDS=(delete router-delete net-delete subnet-delete image-delete console-log show \
port-show net-show stack-create stack-delete image-list stack-show help apt-get aptitude rm mv $HISTIGNORE show)
zshaddhistory () {
    # do not store command not found commands
    { whence ${${(z)1}[1]} >| /dev/null || return 1 }

    local -a line
    line=(${(Q)${(z)1}})

    # aca filtro cosas peligrosas
    if [[ $line[1] == "git" ]]
    then
        if [[ $line[2] == "push" ]]
         then
            if [[ $line[3] == "--force" ]]
            then
                return 1
            fi
        fi
    fi
    # aca la magia de que salvar y que no
    if [ [ ${HISTIGNORE[(i)$line[1]]} -le ${#HISTIGNORE} ]]
    then
        return 2
    else
    if [ [ ${HISTRESTRICTED[(i)$line[1]]} -le ${#HISTRESTRICTED} ]]
    then
        if [ [ ${RESTRICTEDWORDS[(i)$line[2]]} -le ${#RESTRICTEDWORDS} ]]
        then
        return 2
        fi
    fi
fi
}

Needless to say, searching backwards (a.k.a. Ctrl+R) for a command was a nightmare, very similar occurrences would pop up, making easy to make mistakes or forcing me to rewrite part of the command (the very thing I wanted to avoid in the first place!)

A few months ago I started a new job, which basically requires me to code and run the same 10 commands all-day-long. The history setup I had was unnecessary (very few commands) and exposed me to risks (a bad backwards search and I'll be running fabric against a wrong environment).

So I trimmed down the history settings to this

HISTSIZE=5000
SAVEHIST=5000
zshaddhistory() {
       [[ $1 != ${~HISTORY_IGNORE} ]];
       # do not store command not found commands
       { whence ${${(z)1}[1]} >| /dev/null || return 1 }

       return 2
}

This will keep up to 5000 of local (to that shell) lines of history but it won't save any of them to the $HISTFILE

My usual workflow is to open a terminal for the lifetime of a particular change/deploy/whatever and when I'm done I'll close that terminal, losing all history and environmental variables, which is awesome (no chances of hitting the wrong cloud provider region, account, virtualenv, etc)

My $HISTFILE now has 12 lines with the commands I use the most and are long to write, with safe defaults (inexistent hostnames, noop=1, etc)

Now I feel liberated, like I stopped carrying a backpack full of stones