When you misspell a command or path in Bash, you probably use the cursor keys (“arrow keys”) on your keyboard to move your cursor around and fix your typo. You’re pressing Delete or Backspace repeatedly to remove whole words, only to then type them again properly.
Did you know that there are keyboard shortcuts in Bash that allow you to jump around your terminal to edit or tweak your commands quickly and avoid having to retype everything? Let’s take a look at the ones you should try.
Move in Active Line
There are many ways to move to different points of the active line. Press Home or Ctrl + A on your keyboard to move to the beginning. End or Ctrl + E has the opposite effect and moves the cursor to the end of the active line.
Use Alt + B to go back one single word and repeat to jump from word to word backward, toward the beginning of the active line. If you overdo it, press Alt + F for the opposite: to jump forward from word to word.
Edit Active Line
Instead of deleting text one character at a time, Alt + D can delete the whole word that follows the cursor. You don’t have to move to the beginning of a word to delete it, though: use Alt + Delete to delete backward, removing the word at the left of your cursor.
Although you’ll probably keep using Delete and Backspace to remove characters under or before the cursor, it’s worth noting that the Ctrl + D and Ctrl + H combinations have the same result. If you want something more radical, Ctrl + W deletes everything in the active line preceding the cursor, leaving intact everything from afterthe cursor. Ctrl + K does the opposite, deleting everything after the cursor.
Let’s say you typed two words in reverse order. Instead of deleting and retyping them, target the second word with your cursor and press Alt + T on your keyboard. The targeted word will change places with the one before it.
If it is only two characters you typed in reverse, target the second one, press Ctrl + T, and it will swap places with the one before it.
If you’re working with absolute paths or other commands that demand proper capitalization of characters, by pressing Alt + U while targeting a word, every character after the cursor until the end of the word will be capitalized. Pressing Alt + L does the opposite, turning them to lowercase.
Bash is smart enough to autocomplete your half-typed commands and paths if you press Tab. If you’ve typed something like “/home/username/Pic” instead of instead of bringing up “/home/username/Pictures,” but pressing Tab does nothing, you might have a similar folder in your path, like “/home/username/Picachu.” Bash can’t choose for you, so you’ll have to type one or two more letters to give it a hint about which of the two paths you want.
If you don’t remember what’s in the path, you can press Tab again for it to display the files and directories that match your defined path.
Bash also keeps a history of your commands – you can go backward and forward by using the up and down arrow keys on your keyboard. You can also use Ctrl + P to re-select the previous command and repeat it to “keep moving backward” in Bash”s history. Press Ctrl + N to do the opposite. For your information, here is a better way to search your command line history.
Finally, if your whole terminal has become a mess of characters and commands, you can type
clear and press Enter to have it look like a clean slate. The shortcut Ctrl + L will produce the same result, too.
If instead of fixing typos on the commands you already know and you want to expand your command vocabulary, we have some methods that will help you remember new commands easily.
Our latest tutorials delivered straight to your inbox