We old coots are forever going on about our favorite keyboard shortcuts, because that is still the fastest way to get stuff done on a computer. Life is too short to faff around the hard way, so let’s try some nice Bash time-saving shortcuts.
The barrier for a lot of people is not having decent typing skills. There are numerous free typing tutors if you need a little help, and if you practice 10 minutes every day you will amaze and delight yourself at your rapid progress. When you can touch-type 40 words per minute without errors, that is a great milestone to reward yourself with a sweet mechanical keyboard, and then you can spend many fun hours hacking your keyboard.
Navigating the Current Line
Ctrl+a or the Home key go to the beginning of the line, and Ctrl+e or the End key go to the end. When you are working with a long command that wraps, remember that it is still just one line.
Alt+b moves back one word, and Alt+f moves forward one word. This does not work in many X terminals because the Alt key is mapped to menu commands. Look at your menus; when the first letter is underlined that means Alt+ the underlined letter opens that menu. So, just for fun try it in the console; exit your graphical session by pressing Ctrl+Alt+F1. This should bring you to a console login, so login, type something random or bring up a command from your history, and lo! Alt+b and Alt+f work as they are supposed to.
If Ctrl+Alt+F1 does not bring you to a console login, as various Linuxes map these differently, try Ctrl+Alt+F2, Ctrl+Alt+F3, heck, try all of them up to F8 and see what happens. To get back to your graphical session, try Alt+F7, which is traditional. If that doesn’t work try Alt+F1-F8 until you find your way home.
That was fun, but what if you want to stay in your graphical session and use Alt-key combinations? This is a function of your desktop environment, individual applications, and Linux distribution, so for now my advice is investigate how your particular environment manages this.
Replay Command History
You can use the up and down arrow keys to scroll through your command history, or type the
history command. When you find the command you want, type ![number] to run it. For example,
carla@studio:~$ history [...] 1626 ls /etc/default/ 1627 less /etc/default/smartmontools 1628 apt-cache search smart-notifier carla@studio:~$ !1626 ls /etc/default/ [...]
Try Ctrl+r to search your history. Start at a blank prompt, press Ctrl+r, then type your search term, for example “vpn” to find your previous openVPN commands. Then keep pressing Ctrl+r to cycle through your results until you find the one you want, and when you find it press the return key to run it.
Cutting and Pasting
Erase the current command with Ctrl+c. If you change your mind a lot, press the Alt+e or End key to go to the end of the line, then Ctrl+u to delete it. Ctrl+y pastes it back. Ctrl+u deletes from the cursor to the beginning of the line.
Another way is to press Ctrl+a or Home, then Ctrl+k, which deletes from the cursor to the end of the line.
You can copy and paste with your mouse by selecting with the cursor, and middle-click paste. This is my favorite way to copy and paste, but sadly it is not universal, and some applications and Web sites disable it.
Ctrl+l or typing the
clear command clears the screen. This would be more useful if it remained cleared after running a command. For example, when you’re running searches that return a lot of results and your screen gets all cluttered, you can clear it and get rid of the clutter. But when you run another command your screen un-clears, so you still have to wade through a lot of stuff to see the results of your last command.
Moving Around Directories
This can take some getting used to, especially in deeper directories like
/home/carla/Documents/1articles/linuxcom/bash-shortcuts/. First, just where the heck am I?
$ pwd /home/carla/sketchbook
Well that’s the wrong place. How do I find where I want to go? There are several ways. My favorite, when I know the directory I want, is to use
$ locate bash-shortcuts /home/carla/Documents/1articles/linuxcom/bash-shortcuts
Then use mouse select/middle-click paste with the
$ cd /home/carla/Documents/1articles/linuxcom/bash-shortcuts
No, it’s not cheating to use the mouse. It’s fast. You may also use tab completion to quickly type the path. Tab completion is wonderful and I use it all the time as insurance against typos. Start typing, then press the tab key to auto-complete the directory name.
If you need to browse directories, use the asterisk wildcard to browse many directories at one time:
$ ls dirname /*/*
Adjust the number of asterisk and slashes to control how deeply into the filesystem you want to go. Use Shift+PageUp and Shift+PageDown to navigate long command output.
cd - toggles between your current and previous directory.
cd .. takes you up one level, and
cd ../../ goes back two levels.
cd is Bash’s ruby slippers, and it returns you to your home directory.
That’s enough for today, and thank you for coming along!
Learn more about Linux through the free “Introduction to Linux” course from The Linux Foundation and edX.