Save Time on the Command Line with the Bash Shell

bash shell

Bash is the default shell in all GNU/Linux distributions, and is available in any of the BSD Unixes. Since you can find it everywhere, it pays to learn how to enter and edit commands using bash efficiently. Also, many of these shortcuts are implemented by the underlying readline library, so any application that uses it will have these shortcuts available. Let's start with some necessary configuration.

Optimize Your .bashrc

Place the following in your ~/.bashrc before trying any of the following examples. I've added comments to each line so you know what it does.

export HISTCONTROL=ignoreboth # Ignore dups and commands that start with a space - they won't get added to history export HISTFILESIZE=5000 # Keep up to 5000 lines in history (default is 500) export HISTSIZE=5000 # Keep up to 5000 commands in shell history (default is 500) shopt -s histappend # append to the history file, don't overwrite it shopt -s cmdhist # In history file, combine multi-line comamnds into one line stty stop "" # Disable the default meaning of C-s so it can be used for incremental search forward bind space:magic-space # Space dynamically expands any ! history expansions # set -o vi # Uncomment this to get vi-style key bindings You can force the shell to re-read your .bashrc on the fly by sourcing it: . ~/.bashrc

bind is a useful command - you can use it to display all of your key bindings:

bind -P | less

Quickly Search Command History

Apart from up- and down-arrow, which cycle through previous/next commands one at a time, you can search backward through your command history with C-r, and forward with C-s (Control-r and Control-s). These are incremental searches, so you will see the command line update as you type. You can repeat either command multiple times to continue the search back or forward in history.

Editing Shortcuts

After you pull the command you want from your history, you can just hit enter to execute it, or you can edit it in-place. Bash has two editing modes - emacs and vi, selected with set -o emacs and set -o vi. Emacs mode is the default. Again, these are actually implemented in the underlying readline library, so any application linked with readline will have these available.

One tip - While editing on the command line, you can undo any mistakes with C-/ (Control forward-slash) - hit it repeatedly to undo previous edits in sequence.

Here are some more of the most useful editing shortcuts in emacs mode:

As you might expect, vi mode is modal - you are put in input mode by default, but just hitting the escape key will put you in control mode.

Vi Control Mode

Vi Insert Mode

Quickly Grab Command Arguments

The last command argument is the last part of any command you type - so if the last command you typed was ls -lart ~/Downloads, the last argument is just ~/Downloads. If you then wanted to cd into that directory, you would type:

cd M-.

'M' here being 'Meta', or Alt on most keyboards. It will grab the last argument from your last command and display it for you, in this case you would end up with 'cd ~/Downloads'. If you hold down the Alt key and repeatedly press the period key, it will cycle backwards through previous command arguments. Note that this only works in emacs editing mode, but you can also reference the last command argument by typing !$, that is:

cd !$

This has the benefit of working in emacs or vi editing modes. If you have the magic-space activated (see above), you can hit the spacebar, and the !$ will expand into the last argument for you. There are similar history expansions for the first (!^) and all (!*) arguments. In our example above, if we typed ls -lart ~/Downloads, !^ would be -lart and !* would be -lart ~/Downloads.

More Reading