Mindless Blather

Love Your Terminal

29 Nov 2012

Like most developers (I assume), I spend the majority of my workspace staring at a terminal window. You know, like this: Big black box with text and a flashing cursor

When I’m at work, I take up a whole screen with my terminal on my desktop. My work laptop that I always carry with me has one open in OS X Full Screen Mode, as well.

When you look at your terminal this much, it needs to be beautiful. More than that, it needs to know you. You need to have a history with it. No, not like that. Here are 5 things that you can do to make your terminal great.

1. Choose a Satisfying Color Scheme

You’re looking at it all day. Black and white. Maybe some color if you’re using grep. Choose a color scheme that looks great and, perhaps more importantly, won’t hurt your eyes if you have to look at the screen for a long period of time.

I’ve chosen Ethan Schoonover’s Solarized color scheme. There are forks of it for many different interfaces: vim, emacs, X, Visual Studios and even Eclipse if you search for it. It comes in light and dark variants, so if you prefer the “black-on-white” variant for terminals, you’re covered as well. The palate was designed so that the colors were soft and the contrast was easy to look at.

Light and Dark Variants for Solarized

If you have other good choices for colors, be sure to leave them in the comments. The important thing here is that you find a color scheme that you love to look at.

2. Choose a Good Shell

What shell are you using? Probably bash, right? Well, stop it. Or don’t. Just know why you’re using the shell that you have. You have other options. For instance, I use Z Shell (zsh). It has just about every feature you could possibly imagine a shell to have. If it doesn’t have it baked in, someone’s written it.

There are tons of options out there, and if you like bash, that’s fine. Just know your options. Wikipedia has a pretty good comparison of different shells.

If you do decide to go with zsh, I recommend installing Oh My ZShell. It’s the best extension to zsh that I’ve seen.

3. Create a Great Prompt

What does your prompt normally look like? It probably looks like this: $

Or maybe like this: ~/path/to/current/directory $

Or maybe this: username@~/path/to/current/directory $

You have to stare at your command prompt all the time. You’re constantly typing in commands, and the only useful information that your prompt is giving you is the directory that you’re in. Make your prompt work for you. And make it pretty. Here’s a picture of my prompt: My Command Prompt

It’s mostly taken from Steve Losh’s Extravant Command Prompt, with the modifications that:

  1. It shows me the time instead of battery life for my $RPROMPT.
  2. I don’t use mercurial, so I only have git information.
  3. I like a space between the result and the next prompt, so I added a new line.

I’m not going to go into too much detail about how to set it up (Steve Losh’s article does a pretty good job), but here’s a gist of what my theme looks like:

4. Keep Track of Your History

You probably run a lot of commands. You probably also run a lot of commands that you find online. Commands that you forget… and then have to look up again a week later. You should be keeping track of your history. There’s a couple of ways you can do that (and you should be doing all of them):

The $HISTSIZE variable

Setting your $HISTSIZE variable is the first and easiest step to getting everything set up the way you like it. This stores your recent history in a place where your shell can find it. I usually set the value to something fairly high, because it’s nice to have that retention. I have a line in my .zshrc file that looks like this:

export HISTSIZE=100000 SAVEHIST=100000 HISTFILE=~/.zhistory

You can access your history by browsing yourself or through some simple commands:

The history command

history is probably the easiest way to go about it. For instance, history -1 will get you the last command. history -2 will get the last two, etc. history by itself will output your entire history to the console. So you could easily pipe in some other commands for something like “I know I used grep and wc for something useful but I can’t remember what it is. Example:

history | grep wc | grep grep
# ...which, ironically I guess, would cause it to show up in your 
# history if you ran it again.
Ctrl-R

Ctrl-R performs a reverse incremental search (or reverse-i-search) over your history. This allows you to type something like grep and find your most recent command where you used grep. If you keep hitting Ctrl-R, then you’ll go through previous commands with that substring incrementally.

A special function to store all of your history.

I also have a special function in my .zshrc that stores all of my history into log files. While $HISTORY is great, it only retails a certain amount of data. That means that, over time, you’ll begin to lose commands that you had run previously.

I store all of my command history into log files separated by date. There’s different hooks you can set up to have it work, but this is what mine looks like:

function precmd() {
    if [ "$(id -u)" -ne 0 ]; then
        FULL_CMD_LOG="$HOME/.logs/zsh-history-$(date -u "+%Y-%m-%d").log"
        echo "$USER@`hostname`:`pwd` [$(date -u)] `\history -1`" >> ${FULL_CMD_LOG}
    fi
}

You’ll want to run a mkdir ~/.logs before-hand to make sure that the directory exists, otherwise it will just complain. Now you’ll wind up with log names like /home/$USER/.logs/zsh-history-2012-11-29.log and inside will be the full list of that day’s commands. Each command will look like:

ahays@:<host>/home/ahays [Thu Nov 29 21:02:22 UTC 2012]   148  cat /path/to/my/favorite/textfile

I’m planning a followup to this post that shows how you can use that data to make your work day even more productive. But for now, you’ve got a lot of all the commands that you run, and that’s very useful as well.

5. Make Aliases

If you’re like me, you probably run 10% of the same commands for 90% of your day. Aliases help you tone down the bulk of some of those commands. For example, consider this alias your .rc file:

alias ggpnp='git pull origin $(current_branch) && git push origin $(current_branch)'

This would allow you to call ggpnp instead of the original command. You can set up as many of these as you want to make commands easier. Aliases can also be useful for other reasons. For instance,

alias sl=ls

will autocorrect a quick spelling mistake (Pro-tip: zsh handles spelling mistakes automatically, without aliases.) Also, if you always run grep --color=auto instead of grep, then

alias grep='grep --color=auto'

will also work wonderfully. Put in aliases that you know will help you throughout your work day. Oh My ZShell also comes with a few handy aliases built-in.

Conclusion

As a developer, your terminal is probably one of your most important and well-used tools. You should make sure that you’re using it to its full potential. Even more, you should make it fit your personal development style.

If you have any other neat tips and tricks for perfecting your terminal, you should leave them in the comments.

a photo of me

Andrew Hays

I'm an SDE at Amazon.com, writing services and user interfaces for third-party merchants. When I'm not working, I'm probably redesigning my blog for the thousandth time. I like UI design more than service design.

I'm happily married to Ashley Hays. I was born and raised in Arkansas and attended UALR earning a BS in Mathematics and a BS in Computer Science.

I'm on virtually all of the major social networks, but for the most part, you can find me at the ones below: