Bash tricks

Some small list of tricks to make bash better and more like z-shell without actually switching over. You may use these instead of switching over because for example there is no z-shell or you may not have admin privileges on a server.

Editing

Some things to make editing better and faster.

Better history search

Make your up/down-arrow keys more awesome with the following lines in your ~/.inputrc:

"\e[A": history-search-backward
"\e[B": history-search-forward

With those lines your up and down keys just work like you know on empty lines, but if you start typing and touch an arrow key you will search your history with the prefix you just entered.

Better prompt

As you are probably aware the variable PS1 defines your prompt, but you probably don't know about PROMPT_COMMAND, this command is executed each time a new prompt is shown, and yes it can call a bash function () {}, so have fun builing your prompt.

ANSI colors

COLOR_WHITE='\033[1;37m'
COLOR_LIGHTGRAY='033[0;37m'
COLOR_GRAY='\033[1;30m'
COLOR_BLACK='\033[0;30m'
COLOR_RED='\033[0;31m'
COLOR_LIGHTRED='\033[1;31m'
COLOR_GREEN='\033[0;32m'
COLOR_LIGHTGREEN='\033[1;32m'
COLOR_BROWN='\033[0;33m'
COLOR_YELLOW='\033[1;33m'
COLOR_BLUE='\033[0;34m'
COLOR_LIGHTBLUE='\033[1;34m'
COLOR_PURPLE='\033[0;35m'
COLOR_PINK='\033[1;35m'
COLOR_CYAN='\033[0;36m'
COLOR_LIGHTCYAN='\033[1;36m'
COLOR_DEFAULT='\033[0m'

Add these to ~/.bash_rc to be able to colorize your output. Probably useful for PS1.

Useful aliases

Actually some aliases and single line functions, because I can't remember everything.

Serve current directory as a website

alias serve='python -m SimpleHTTPServer 8008'

This needs python installed (it seems to be installed everywhere nowadays) and opens a HTTP server that serves the current directory on port 8008. It honours index.html files but does not dynamic parsing, etc.

Quit with CTRL+C

Hexdump a file

alias hexhead='xxd -g 1 -l 1024'
function hex() { xxd -g 1 "$1" | ${PAGER:-less} }
function hextail() { tail -c 1024 "$1" | xxd -g 1 } 

hexhead displays the first kilobyte of the file as a hex dump, just hex displays all of it in your default pager (or less if not set, Beware: Don't do this to Big files, it takes ages ;) ). hextail displays the last kilobyte.

Switch a python virtualenv

function venv() { . "$HOME/.virtualenvs/$1/bin/activate" }

Switch to a python virtual env by name (assuming you store your virtual python installations in $HOME/.virtualenvs)