Developer Aspirations

YAPB - Yet Another Programming Blog

Thursday

10

April 2014

Upgrade Your Command-Line - Part 4: better prompt

by Colin Miller, on command-line, bash

So far I've covered navigation and multi-tasking on the command-line. In this part I'm going to cover another important aspect of the command-line: information feedback.

When using the command-line, you're often viewing various bits of information. Having more (useful) information can make you more productive. The most looked at piece of information in a shell, is the command prompt. You may recognize your command prompt as something like:

colin@colintmiller.com:~/documents$ 

This conveys a few bits of information. I know that I'm logged in with a username of colin, that the machine is named colintmiller.com, and that I'm currently in the directory ~/documents (where ~ represents the home directory). I also know that I'm logged in with a user account and not a root account as the prompt ends in a $.

This could convey a lot more information. For instance, my general prompt looks like:

┌(cmiller@colintmiller.com)─(✓)─(~/Projects/android) (master)
└─(28 files, total 80Kb)─>

The ✓ tells me the status of the last command, pass or fail. It could also be ✗ if it failed. I know at a glance how many files are in the directory and how much space the files in the current directory (not recursive) is taking up. I also know what git branch I'm on if the directory I'm in is a git directory.

The git branch information is one of the most useful bits to me and it was added using a tool called git-prompt. It saves time not having to type git branch to see what branch I'm on.

For those who are curious, here is my full prompt setting as defined in my ~/.bash_profile:

export PS1="\[\033[1;37m\]\342\224\214($(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]\h'; else echo '\[\033[01;34m\]\u@\h'; fi)\[\033[1;37m\])\342\224\200(\$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\]\342\234\223\"; else echo \"\[\033[01;31m\]\342\234\227\"; fi)\[\033[1;37m\])\342\224\200(\[\033[1;32m\]\w\[\033[1;37m\])\[\033[1;37m\]\$(__git_ps1)\n\342\224\224\342\224\200(\[\033[1;32m\]\$(ls -1 | wc -l | sed 's: ::g') files, \$(ls -lah | grep -m 1 total | sed 's/total //')b\[\033[1;37m\])\342\224\200> \[\033[0m\]"

Next Up: part 5: TaskWarrior

comments powered by Disqus