Bash: Your forgotten friend (part 2)

August 20th, 2008

Aliases are one of the core portions of Bash that will help you become more productive almost immediately. The speed at which aliases can be defined is astounding. <!—more—> To define an alias, the command is simply <pre class=”prettyprint lang-bash”><code>alias short=’long command goes here’</code></pre> Super simple! To save aliases, however, you’ll need to put them in your .bash_profile so they load automatically. To see a list of currently defined aliases, type <pre class=”prettyprint lang-bash”><code>alias</code></pre> Here are my aliases, chunk by chunk, and a brief bit about what they do. <pre class=”prettyprint lang-bash”># ALAISES

# set some aliases case $OSTYPE in

System Message: ERROR/3 (<string>, line 12)

Unexpected indentation.
linux*)
# Linux Specific

System Message: WARNING/2 (<string>, line 14)

Definition list ends without a blank line; unexpected unindent.

;; darwin*)

System Message: ERROR/3 (<string>, line 16)

Unexpected indentation.
# Mac Specific alias mvim=’/Applications/MacVim.app/Contents/MacOS/Vim -g’ alias agi=’sudo port install’

System Message: WARNING/2 (<string>, line 19)

Block quote ends without a blank line; unexpected unindent.

;; *)

System Message: WARNING/2 (<string>, line 19); backlink

Inline emphasis start-string without end-string.

System Message: WARNING/2 (<string>, line 21)

Block quote ends without a blank line; unexpected unindent.

;;

System Message: WARNING/2 (<string>, line 22)

Block quote ends without a blank line; unexpected unindent.

esac</pre> 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 LS_COLORS environment variable (linux handles it differently than macs) or commands for installing software (such as apt-get or other variants). <pre class=”prettyprint lang-bash”># 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 <a title=”Blank Page Syndrome” href=”http://www.explorewriting.co.uk/BlankPageSyndrome.html“>blank page syndrome</a>. The other command, servethis, spawns a small webserver on your computer which allows you to quickly serve files to co-workers and those nearby. <pre class=”prettyprint lang-bash”># 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 $GIT_EDITOR, which I have defined in my script. Environment variables are defined by using the syntax <pre class=”prettyprint lang-bash”>export GIT_EDITOR=’mvim -f’</pre> This can then be referenced as $GIT_EDITOR 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.


Comments are disabled. If that bothers you, please contact me on twitter at @justinlilly and let me know.