====== Bash line shortcuts ======
===== Clear line =====
* Ctrl+U - clear all the current line from the end to the beginning only if the cursor is at the end of the line.
You can then recall the cleared line with Ctrl+Y if you need.
* Ctrl+K - clear all the current line from the beginning to the end only if the cursor is at the beginning of the line.
You can then recall the cleared line with Ctrl+Y if you need.
* Ctrl+W - clear the previous word in the current line. For example if you have typed a command like git diff /path/to/some/file and you want to delete just the last parameter to the command, Ctrl+W is very useful.
You can then recall the cleared data using Ctrl+Y if you need.
* Ctrl+ECtrl+U - move the cursor to the end of the line and clear all the current line from the end to the beginning.
You can then recall the cleared line with Ctrl+Y if you need.
* Ctrl+ACtrl+K - move the cursor at the beginning of the line and clear all the current line from the beginning to the end.
You can then recall the cleared line with Ctrl+Y if you need.
* Ctrl+C - cancel the current command line, which implies clear all the current line no matter where the cursor is.
Disadvantage: you can't recall the cleared line anymore.
* Alt+Shift+# - comment the current line, keep it in the history and bring up your prompt on a new line.
===== History =====
Browse history with Arrow Up and Arrow Down
Browse through history with Ctrl+R, and by starting to type the command. If multiple commands with the same search pattern exist in history, press Ctrl+R again to loop through them.
Ctrl+O will run the selected command, and leave it in prompt
Typing the command **history** will display all history.
Also, you can grep the output like this: **history | grep apt**
Re-run a specific command from history referenced by command number: **!#**. **!70** will rerun the command on position 70 in history file
Re-run the previous command **!!**. This is helpful when you forget to sudo, so you can use **sudo !!** to sudo the previous command, or you can use **!! | grep text** to grep the output of the previous command.
Run a recent command by referencing to part of it: **!text**. If you recently user the command **ping 8.8.8.8**, you can re-run it by typing **!pi**
Adding **:p** to the above outputs will print the command into terminal. **!70:p**, **!!:p**, **!pi:p**
Reuse the last argument from previous command **!$**. If you user **ping 8.8.8.8**, you can then traceroute it by typing **traceroute !$**. Note that this only uses the last argument, so after **ping 8.8.8.8 -c 4** using it like **traceroute !$** will result with **traceroute 4**.
Reuse the first argument from previous command **!^**, so after **ping 8.8.8.8 -c 4**, using **traceroute !^** will result with **traceroute 8.8.8.8**.
Reuse all arguments from previous command with **!***, so after **ping 8.8.8.8 -c 4**, using **ping !*** will result with **ping 8.8.8.8 -c 4**.
You can combine this with the command history: **command !abc:#** or **command !abc:***
Correct the previous command with **^abc^xyz**
su@www:~$ ping gogle.com
^C
su@www:~$ ^gog^goog
ping google.com
PING google.com (216.58.214.238) 56(84) bytes of data.
64 bytes from bud02s24-in-f238.1e100.net (216.58.214.238): icmp_seq=1 ttl=115 time=10.9 ms
64 bytes from bud02s24-in-f238.1e100.net (216.58.214.238): icmp_seq=2 ttl=115 time=11.0 ms
64 bytes from bud02s24-in-f238.1e100.net (216.58.214.238): icmp_seq=3 ttl=115 time=11.1 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 10.917/11.020/11.105/0.077 ms
su@www:~$
**history -c** will clear the history.
Configuring the **HISTCONTROL** variable allows to not store commands beginning with space, or duplicate commands - or both.
HISTCONTROL=ignorespace
HISTCONTROL=ignoredups
HISTCONTROL=ignoreboth