I spend a lot of time using the command line, and I find these features particularly useful. This might be old news to some, but they are still immensely helpful. (Also, these are extremely hard to google for, so hopefully this will save you time in more ways than one!) I tried to include as many examples as I could to make these easy to understand.
Useful Bash files
.bash_profileSourced by login shell. (When using SSH).bashrc Sourcedby all shells..bash_aliasesContains useful command aliases, generally sourced by ~/.bashrc.bash_historyContains a history of your previous commands. (historycommand will list these.)
Useful history commands in Bash
!! Replay your last command
$> echo "Hi"
Hi
$> !!
echo "Hi"
Hi
!CMD Replays the last command that started with CMD.
$> ls -al
... dir listing ...
$> !ls
ls -al
... dir listing ...
!n Replays command #n in your history log
$> ls -al
... dir listing ...
$> history
... commands ...
165 ls -al
$> !165
ls -al
CMD:p Suffix used to print what would be output in the command. (:p suffix works on other commands too!)
$> ls -al
... dir listing ...
$> !ls:p
ls -al
$> touch hello.txt
$> !!:p
touch hello.txt
!$ Replaced with the last part of your last command. (can be used with :p)
$> ls -al ~/Desktop/blah
ls: /Users/rob/Desktop/blah: No such file or directory
$> mkdir -p !$
mkdir -p ~/Desktop/blah
... makes the directory ...
!^ Replaced with the first argument of the last command (can be used with :p)
$> ls -al ~/Desktop/blah
... dir listing ...
$> !^:p
ls
!* Replaced with the entire previous command, minus the program ($0) (can be used with :p)
$> ls -a -l ~/Desktop/blah
$> !*:p
-a -l ~/Desktop/blah
^A^B Replaces “A” with “B” in the previous command. (useful for correcting typos)
$> ls ~/Desktop/blha
/Users/rob/Desktop/blha: No such file or directory
$> ^ha^ah
ls ~/Desktop/blah
... dir listing ...
{..} Repeats the command for each item in the brackets. (Ranges can work too)
$> touch hello
$> mv hello{,.old}
... moves hello -> hello.old ...
$> touch {1..10}.txt
... makes files 0.txt, 1.txt, ..., 10.txt ...
$> rm {1..10}.txt
... removes those txt files from above ...
$> touch {1..10}.{a,b,c}
... makes 1.a, 1.b, 1.c, 2.a, 2.b, ..., 10.c ...
$> rm {1..10}.{a,b}
... removes all files except 1.c, 2.c, ..., 10.c ...
Useful TAB commands in Bash
/[TAB][TAB]> Shows your directory structure (including hidden directories)
$> /[TAB][TAB]
... dir listing
STR[TAB][TAB]> Lists all commands beginning with STR
$> mk[TAB][TAB]
mkafmmap mkdep mkextunpack mkfile mkfontscale mknod mktemp
mkbom mkdir mkfifo mkfontdir mklocale mksdk
...
DIR[TAB][TAB]> Lists all subdirectories in DIR
$> ~/Desktop[TAB][TAB]
blah/
...
$[TAB][TAB]> Lists all shell variables
$> $[TAB][TAB]
$IRBRC $SHELL
$BASH $ITERM_PROFILE
$BASH_ARGC $ITERM_SESSION_ID
...
@[TAB][TAB]> Lists all entries from /etc/hosts
$> @[TAB][TAB]
@::1 @broadcasthost
...
Useful Keyboard Shortcuts in Bash
You can also use VI mode by putting set -o vi in ~/.bashrc
CTRL + aJump to the start of the lineCTRL + bMove back a charCTRL + cTerminate the commandCTRL + dDelete from under the cursorCTRL + eJump to the end of the lineCTRL + fMove forward a charCTRL + kDelete to EOLCTRL + lClear the screenCTRL + rSearch the history backwardsCTRL + RSearch the history backwards with multi occurrenceCTRL + uDelete backward from cursorCTRL + xxMove between EOL and current cursor positionCTRL + zSuspend current command (bgto list backgrounded jobs,fg #to bring it back)