5 Command Line Productivity Tips

by Drew Barontini

As developers, we spend a lot of time on the command line. It’s important to not only be comfortable working in it, but also to create efficiencies for how we work in it. Let’s look at five tips for improving productivity while on the command line.

1. Get comfortable with standard commands

For one week, I urge you to avoid the Finder (or equivalent operating system file-management application). Constrain yourself to using only shell commands, and you will be more efficient. Let’s look at shell commands you can use in place of editing files and directories from within Finder.

These commands are for Unix, which runs on all OS X (Apple) machines. Similar commands should be available, depending on your command-line environment.

Listing files and directories

ls

This will list out the files and directories in the current working directory.

Changing directories

cd ~/Projects

This will move you from your current location in the shell to the ~/Projects directory.

Creating files and directories

touch index.html

This will create the index.html location in the current directory.

Removing files and directories

rm index.html

This will remove the index.html file.

rm -rf stylesheets

This will (recursively) remove the stylesheets directory.

Moving/copying files and directories

mv index.html index_new.html

This will rename the index.html file to index_new.html. You can also use it to move the file to a directory.

mv index.html views/index.html

And, if you need to move a folder and its contents:

mv stylesheets assets

This will move the stylesheets directory into the assets directory.

Printing file contents

cat index.html

This will print the file contents of index.html.

less index.html

With the less command, you view the file contents in a “pager” format, which allows you to scroll up and down through the file. This is different than cat, which just outputs the file contents onto the screen.

2. Customize your prompt

The standard prompt is boring, and we don’t need to know the name of our computer. Let’s make it better by displaying the current directory, git branch, and version-control status.

Custom Bash Prompt My custom prompt.

This is my prompt. It is a modified version of the one in Mathias Bynens' dotfiles. Rather than discussing the laborious details of the prompt (half of which I don’t understand), I recommend downloading and editing an existing prompt you like, or by following a tutorial.

Tip: Another option is to use something like Git Radar, which has a more detailed display.

A customized prompt with useful information will improve how you work on the command line. I recommend spending the time to get one set up.

3. Create (and use) aliases

Aliases are vital to improving efficiency on the command line. Use them to shorten long (or forgettable) commands for quick and easy reference.

Creating an alias

In your .bash_profile (or equivalent environment file), you create an alias like so:

alias gs='git status'

The gs portion is the name of the alias, and the git status on the other side of the equals sign is the command that is aliased to gs.

Now let’s look at a few example aliases I use.

All of my aliases are in my dotfiles, so you can see them there, if you’d like.

Unix

alias l='ls -1F'

This command displays a better file listing than the default ls we talked about earlier.

Screenshot With the default ls command.

Screenshot With the l alias.

alias ..='cd ..'
alias ...='cd ../..'

These two aliases (.. and ...) will change directory up one and two levels, respectively.

alias mf='touch'
alias md='mkdir'
alias rmd='rm -rf'

These aliases are for fast file and directory creation/removal, as we discussed earlier with touch, mkdir, and rm -rf.

Git

alias g='git'
alias ga='git add'
alias glr='git pull --rebase'
alias go='git checkout'
alias gom='git checkout master'
alias gs='git status'
alias gpp='git pull --rebase && git push'

Here are a few example git aliases I use. I have a lot more of them, and they make my command-line work much, much faster.

4. Create (and use) functions

Like aliases, functions can remove repetitive tasks, which is the goal of improving efficiency. Let’s look at some example functions that I use.

All of these functions (and much more) are also in my dotfiles.

Copy File Contents

This function will copy the contents of a file and paste it to the clipboard.

# ----------------------------------------------------
#   Copy File Contents
#   -> Copies a files' contents to clipboard
# ----------------------------------------------------
#
# $1 - The file to copy
#
# Usage: `copy_file_contents file.txt`
#

function copy_file_contents() {
  cat $1 | pbcopy
  echo "-- '$1' copied to the clipboard! --"
}

Copy File Contents copy_file_contents function.

Tip: You can create aliases of your user-defined functions. For example, alias cfc='copy_file_contents'.

Find Alias

With all of the aliases I have, I created this find_alias function to easily search for a given alias.

# ----------------------------------------------------
#   Find Alias
#   -> Greps for an alias
# ----------------------------------------------------
#
# $1 - the alias name
#
# Usage: `find_alias 'gba'`
#

function find_alias() {
  alias | grep "alias\s$1"
}

Find Alias find_alias function.

Tip: You can type alias in the shell to print out all of your aliases.

Git Delete Branch For Real

This <sarcasm>non-specific function</sarcasm> will delete a git branch locally and remotely.

# ----------------------------------------------------
#   Git Delete Branch For Real
#   -> Delete both local and remote copies of a branch
# ----------------------------------------------------
#
# $1 - the branch name
#
# Usage: `git_delete_branch_for_real fix_form_styles`
#

function git_delete_branch_for_real() {
  git branch -D $1 && git push origin :$1
}

Git Delete Branch For Real git_delete_branch_for_real function.

Git Find Branch

Like the find_alias function above, this one will help you find the name of the branch you need to work on.

# ----------------------------------------------------
#   Find Branch
#   -> Find a specific branch (local or remote)
# ----------------------------------------------------
#
# $1 - the name of the branch
#
# Usage: `git_find_branch update_`
#

function git_find_branch() {
  git branch -a | grep $1
}

Find Branch find_branch function.

These functions abstract complex commands and eliminate repetitive tasks. They are another tool for fine-tuning your efficiency on the command line. And they are just shell commands inside of a function() {} definition.

5. Use autojump

autojump is a faster way to navigate your filesystem. It works by maintaining a database of the directories you use the most from the command line. – GitHub docs

Jumping from project to project can be tedious. Autojump remembers directories you cd into, and then makes it easy to get back there.

Usage

Screenshot The autojump command.

All you have to do is type j and the directory name (or part of it), and autojump will take you there.

The more that I “move” into the codeschool directory, the less characters I have to type to get there. Autojump picks up that I use it more than other directories with a similar name.

Installation

I recommend installing autojump through Homebrew if you’re on OS X.

brew install autojump

That’s all there is to installing it.

That’s All, Folks

Hopefully these tips help you improve your productivity and efficiency on the command line. They improved my workflow, and I continue to find more ways to reduce complexity and eliminate repetitive actions.