Show pageOld revisionsBacklinksExport to PDFBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Bash line shortcuts ====== ===== Clear line ===== * <key>Ctrl+U</key> - 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 <key>Ctrl+Y</key> if you need. * <key>Ctrl+K</key> - 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 <key>Ctrl+Y</key> if you need. * <key>Ctrl+W</key> - 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 <key>Ctrl+Y</key> if you need. * <key>Ctrl+E</key><key>Ctrl+U</key> - 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 <key>Ctrl+Y</key> if you need. * <key>Ctrl+A</key><key>Ctrl+K</key> - 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 <key>Ctrl+Y</key> if you need. * <key>Ctrl+C</key> - 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. * <key>Alt+Shift+#</key> - comment the current line, keep it in the history and bring up your prompt on a new line. ===== History ===== Browse history with <key>Arrow Up</key> and <key>Arrow Down</key> Browse through history with <key>Ctrl+R</key>, and by starting to type the command. If multiple commands with the same search pattern exist in history, press <key>Ctrl+R</key> again to loop through them. <key>Ctrl+O</key> 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** <code bash> 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:~$ </code> **history -c** will clear the history. Configuring the **HISTCONTROL** variable allows to not store commands beginning with space, or duplicate commands - or both. <code bash> HISTCONTROL=ignorespace HISTCONTROL=ignoredups HISTCONTROL=ignoreboth </code> linux/shell_commands/line.txt Last modified: 2021/03/12 12:28by 127.0.0.1