The Shortcuts
Shivm Soaini
Math Major Turned AI Enthusiast: Mastering Computer Vision | Building Robust Python Code | Delving Deep into the Math of AI | Sharing Knowledge Through Writing | Ready to Transform Ideas into Reality
We can speed up our command-line experience by taking advantage of the many built-in shortcuts.
These shortcuts are designed so that your hands never leave your keyboard "home base".
They take a little bit of practice to get comfortable with, but it's worth the effort!
Clearing
Use can use clear command to clear the entire screen, but there is also a shortcut ctrl+l which does the same thing.
Try it out!
Jumping
Use ctrl+a to move the cursor to the beginning of the line.
Use ctrl+e to move the cursor to the end of the line.
Use ctrl+f to move the cursor forward one character at a time (same as the right arrow).
Use ctrl+b to move the cursor backwards one character at a time (same as the left arrow).
These shortcuts are useful for the ones having arrow keys far right on their keyboard. It is a subtle convenience for them.
Use alt+f to move the cursor forward one word.
Use alt+b to move the cursor backwards one word.
Swapping
Use the shortcut ctrl+t to swap the current character under the cursor cursor with the one preceding it. This can be useful to correct the typos made by typing too quickly.
While swapping characters my not be that useful, but shortcut alt+t to swap words can come in handy.
领英推荐
Kill the Line
Use ctrl+k to kill the text from the current cursor location until the end of the line.
Use ctrl+u to kill the text from the current cursor location to the beginning of the line.
Killing the Word
Use alt+d to kill the text from the current cursor location through the end of the word.
Use alt+backspace to kill the text from the current cursor through the beginning of the word.
Reviving Text (Yanking)
When we kill text using commands like ctrl+k, ctrl+u, alt+d, and alt+backspace, the "killed" text is stored in a memory in an area known as the "kill-ring" (sounds like a wrestling event!).
We retrieve the most recently killed text using ctrl+y shortcut.
History
Bash keeps a record of the command we have previously entered. We can see the actual file at ~/.bash_history.
You can scroll through the history one command at a time using the up and down arrows.
We can also use the history command to view the entire history, though it's generally easier to manage if we pipe the output to less.
We can easily re-run an earlier command if we have its line number from the history. For example, to run the 73rd command in the history, we could run !73.
Often, it's easiest to find an earlier command by searching using a small portion of the command that you remember.
Type ctrl+r to enter "reverse-i-search". As you start typing, bash will search the history and show you the best match.
Hit ctrl+r to cycle through potential matches.
The better way to do this which I found useful is to open bash_history file using nano and then use search functionality / to search for your desired command.
Thanks for reading : )