One of the best parts of Unix systems is that fundamentally they are built as development platforms. The most common text command interface for Unix is call Bash (the Bourne Again Shell)and it is a full blown script-able interface allowing direct interaction with command line programs and giving the user the ability to string together these programs into really powerful applications. Because of the power of this interface, developers have over many years improved the ability to use it directly as well. Things like <tab> completion are well known, but how about reverse command searches, built-in text editor mode, and shortcuts galore. I have been trying to use more and more of this “built-in” bash functionality and so below are some of my favorite shortcuts and functionality.
Shortcuts:
Ctrl + A | Go to the beginning of the line you are currently typing on |
Ctrl + E | Go to the end of the line you are currently typing on |
Ctrl + L | Clears the Screen, similar to the clear command |
Ctrl + U | Clears the line before the cursor position. If you are at the end of the line, clears the entire line. Especially useful when you know you’ve mis-typed a password and want to start again. |
Ctrl + K | Cut the line after the cursor, inverse of the Ctrl + U |
Ctrl + Y | Pastes the content from a previous Ctrl + K or Ctrl + U cut. |
Ctrl + H | Same as backspace |
Ctrl + R | Search through previously used commands |
Ctrl + C | Sends SIGINT to whatever you are running (effectively terminating the program.) |
Ctrl + D | Exit the current shell |
Ctrl + Z | Puts whatever you are running into a suspended background process. fg restores it. |
Ctrl + W | Delete the word before the cursor |
Ctrl + T | Swap the last two characters before the cursor |
Alt + T | Swap the last two words before the cursor |
Alt + F | Move cursor forward one word on the current line |
Alt + B | Move cursor backward one word on the current line |
Tab | Auto-complete files and folder names (f there is a multiple option match hitting Tab twice will list all possible values.) |
Alt + . | Paste the previous commands final argument (great for running different commands on the same file path.) |
To see a complete list of all bound bash shortcuts you can type
bind -P |less
but you may need to look-up some bash hex character values to understand all of them. What is more you can actually set bound shortcuts to almost anything you can think of, including actual applications, for example:
$ bind -x ˜\C-e˜:firefox.
will launch the Firefox web browser from the command line when you hit Ctrl + e.
Another one of my favorite commands is fc (fix command.) If you simply type
fc
FC will copy your most recent bash history into your preferred editor (vi by default on most systems) and allow you to edit it within the editor. If you save and exit the editor it will automatically copy it the contents into the bash session and hit enter. Additionally if you are interested in editing some other history item you can type
fc -l
to get a full history with numbers beside them. Then type
fc <num>
where<num> is the history number you want to edit. In a former life my bash terminal and fc was all I needed for most SQL testing.