Bash: Your forgotten friend (part 2)
Aliases are one of the core portions of Bash that will help you become more productive almost immediately. To define an alias, the command is simply:
alias short='long command goes here'
Super simple! To save aliases, however, you'll need to put them in
your .bashprofile so they load automatically. To see a list of
currently defined aliases, type alias
. Here are my aliases, chunk by
chunk, and a brief bit about what they do.
# ALAISES # set some aliases case $OSTYPE in linux*) # Linux Specific ;; darwin*) # Mac Specific alias mvim='/Applications/MacVim.app/Contents/MacOS/Vim -g' alias agi='sudo port install' ;; *) ;; esac
This bit of code sets up os specific commands. It is especially helpful when porting your config file between various locations as I do. Although I haven't setup any linux specific commands, prime candidates for this would be a different `LSCOLORS` environment variable (linux handles it differently than macs) or commands for installing software (such as apt-get or other variants).
# Global alias l='ls -lah' # Long view, show hidden alias ls='ls -GFp' # Compact view, show colors alias la='ls -AF' # Compact view, show hidden alias ll='ls -lFh' # Long view, no hidden alias grep='grep --color=auto' # Always highlight grep search term alias ping='ping -c 5' # Pings with 5 packets, not unlimited alias df='df -h' # Disk free, in gigabytes, not bytes alias du='du -h -c' # Calculate total disk usage for a folder alias ..='cd ..' # Go up one directory alias ...='cd ../..' # Go up two directories alias servethis="python -c 'import SimpleHTTPServer; SimpleHTTPServer.test()'" alias clr='clear;echo "Currently logged in on $(tty), as $(whoami) in directory $(pwd)."'</pre>
These aliases, and those subsequently defined, are global to all installations. The first four aliases are permutations of the `ls` command. They allow quick access to various filesystem views, without trying to remember the flags (the extra stuff after the command) that displays them.
The last two elements bear specific explanation. The clr command clears the current buffer (screen) and tells you which user you're on and which directory you're looking at. This doesn't serve a lot of purpose as that information is part of my prompt, but does help alleviate blank page syndrome. The other command, servethis, spawns a small webserver on your computer which allows you to quickly serve files to co-workers.
# Git Aliases alias gb='git branch' alias gba='git branch -a' alias gc='git commit -v' alias gl='git pull' alias gp='git push' alias gst='git status' alias gd='git diff | $GIT_EDITOR -' # HG ALIASES alias hgst='hg status' alias hgd='hg diff | $GIT_EDITOR -'</pre>
These are mostly helper aliases that serve to only shorten longer commands. The one interesting bit here is the use of the environment variable $GITEDITOR, which I have defined in my script. Environment variables are defined by using the syntax:
export GIT_EDITOR='mvim -f'
This can then be referenced as $GITEDITOR in any and all shell scripts.
In the last installment of this series, I'll go over the bash functions I use and how they make life easier.