Working within a command line environment, like a Bash session, can be far more productive that using a graphical user interface. Here’s a collection of shortcuts.
Moving Around The Command Line
| Shortcut | Function |
|---|---|
| Ctrl + a | position the cursor at the start of the line |
| Ctrl + e | position the cursor at the end of the line |
| Ctrl + xx | alternate the cursor position between start of line and current position |
| Alt + b | move the cursor backwards a word |
| Alt + f | move the cursor forewards a word |
| Ctrl + w | cut backwards a word |
| Ctrl + u | cut backwards from the cursor to start of line |
| Ctrl + k | cut forwards from the cursor to end of line |
| Ctrl + y | paste the contents of the paste buffer |
| Esc + . | paste the last typed argument |
| Esc + n + Esc + . | paste the nth argument from the previous command |
| Ctrl + _ | undo |
| Ctrl + l | clear the screen |
| Ctrl + c | cancel the current running command |
| Ctrl + d | disconnect (exit) the current session |
Using Command Line History
| Shortcut | Function |
|---|---|
history | view command history, with line numbers |
!n | retrieve the command at line number n, like !7 |
!-n | retrieve the nth last command, like !-2 |
!string | retrieve the command that starts with string, like !curl |
sudo !! | re-execute the last command, but with sudo |
^string^newstring^ | re-execute the last command, replacing string with newstring |
| Ctrl + r | begin search history mode, type a few characters, repeat press Ctrl + r for next match |
| Ctrl + g | escape search history mode |
Using Arguments From Command Line History
| Shortcut | Function |
|---|---|
command !$ | execute command and use the last argument of the previous command |
command !* | execute command and use all arguments of the previous command |
command !:n | execute command and use the nth argument of the previous command |
command !m:$ | execute command and use the last argument of the command at line number m |
command !m:* | execute command and use the all arguments of of the command at line number m |
command !m:n | execute command and use the nth argument of the command at line number m |
command !-m:n | execute command and use the nth argument of the the mth last command |
Managing Command Line History
Prevent a command from being written to history… precede the command with a space, like this:
1 cat ~/.bashrc
Reliably delete all history, clear the history buffer and prevent history from being re-written on exit:
1cat /dev/null > ~/.bash_history && history -c && exit
Navigating Directories
| Shortcut | Function |
|---|---|
cd | go to home directory (same as cd ~) |
cd - | go to previous working directory |
pushd /foopushd /bar | push the current directory to the directory stack, then cd to the /foo directorypush the current directory to the directory stack, then cd to the /bar directory |
popd | remove the top directory from the directory stack, then cd to it |