Bash Tricks

The bash shell can operate in two modes, emacs and vi. Because bash was written and is maintained by the people who write emacs, emacs is the default mode used by bash. If the vi mode is desired then add the following command to your .bashrc file:

set -o vi

Otherwise, enjoy these emacs shortcuts for the bash shell! Now, despite my personal preference for vim over emacs, I feel that emacs bindings are much better suited for bash, primarily because a modal command prompt can be very confusing!

CTRL + a         Move to beginning of line
CTRL + e         Move to end of line
ALT  + b         Move back a word
ALT  + f         Move forward a word
CTRL + b         Move backward a character
CTRL + f         Move forward a character
ALT  + >         Move to the last line in history
CTRL + u         Cut to the beginning of the line
CTRL + k         Cut to the end of the line
CTRL + w         Cut last word
ALT  + BS        Cut last word
ALT  + d         Cut next word
CTRL + h         Delete last character
CTRL + d         Delete current character
CTRL + y         Paste last cut
CTRL + p         Previous history line
CTRL + n         Next history line
CTRL + r         Search history
ALT  + n         Search forward history
ALT  + p         Search previous history
CTRL + o         Execute current command and load the next history line
ALT  + .         Recall last argument
ALT  + 0         Recall last command - e.g. argv[0] of previous history line
ALT  + 3         Recall 3rd argument - e.g. argv[3] of previous history line
CTRL + g         Cancel interactive interface, like a history search
CTRL + _         Undo
ALT  + r         Revert line
ALT  + &         Expand tilde
CTRL + l         Clear the screen
CTRL + t         Switch previous two chars
ALT  + t         Switch previous two words
ALT  + \         Delete horizontal space
ALT + CTRL + e   Shell expand the line

My personal favorites, and the one worth making second nature are CTRL + a, CTRL + e, ALT + . and CTRL + r. Enjoy your improved bash fu!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: