In this section, we will learn how to work in the shell efficiently. We will introduce some important practices and techniques that will improve your productivity and make you a faster shell command hacker. This can make you a happier person because, eventually, you will be able to advance to feeling very comfortable working in the shell. Please note, in this section, we will show you a lot of keyboard shortcuts. Learning keyboard shortcuts is like learning any other craft, you begin slowly and gradually, because learning too many new skills at once can leave you overwhelmed and make you forget more quickly than learning in smaller chunks. My tip is to start by learning the first three to four command editing shortcuts and then incorporate more from day to day or week to week. We will start with the command editing shortcuts. Now, if you don't know any command editing shortcuts at all, let's recap what you probably know so far on how to type and edit text in the command line.
The first shortcut for moving the position of the cursor is that you can use the left and the right arrow keys, which are helpful to edit the text you wrote to insert or delete characters at a specific position. But if this were all that one could do in the shell, working in the shell would be very inefficient, because single-character cursor movement is very slow. Also, every time a command gets executed with a typo or the command needs to be rerun with a small difference, such as changing one option, the complete command needs to be retyped from beginning to end.
To be a lot more efficient, let's introduce some very important command editing shortcuts for your everyday work with Linux:
- To move the cursor to the end of the line, use Ctrl + E.
- To go back to the beginning, press Ctrl + A, Ctrl + E, Ctrl + A respectively.
- To move the cursor to the next word, which is defined by a space or special characters such as dot, semicolon, or point, use Ctrl and the right arrow key to move forward.
- To move backward one word, use the left arrow key while holding the Ctrl key. You can also use meta + F and meta + B to do the same.
- On most systems, like any normal PC keyboard, there is no meta key, so the meta key is mapped to the Esc or Alt key.
- To toggle between the current position and the beginning of the line, press Ctrl + XX twice.
- Press Ctrl + K to delete the text from the cursor to the end of the command line.
- To delete the text from the cursor to the start of the command line, press Ctrl + U. Use Alt + D to delete to the end of the word.
All of the command editing keyboard shortcuts we just discussed here are only the most important and efficient ones for your everyday daily use, and there are many, many more.
In order to get a full list of all of the Bash keyboard shortcuts, do the following:
- Type man bash and then search for the section commands for moving
- In this man page, search for Killing
- In this man page, the C key is the Ctrl key, the M key is the meta key, and the dash means to combine or press and hold two keys, as we have shown you using the Ctrl + A shortcut earlier
For example, C-k stands for kill-line, which kills the text from the point to the end of the line. Alt + T is used to swap words, M-u to make words uppercase, and M-l to make words lowercase.
Now, let's move on to the command completion shortcuts. The most important command completion shortcut is the Tab key on your keyboard. It tries to guess and autocomplete the command you are about to type. It is very useful and speeds up typing commands tremendously, but don't overdo it when using this key, it can only print the full unique command name if there are no alternatives available. Type pass and press the Tab key; it will autocomplete the name passwd as there are no other programs with this full name available. Type pa and press the Tab key; this will give you several results as no unique name can be found. Type yp and press the Tab key; this will autocomplete to a long name as this is the only variant available. The Tab short key autocompletes commands by default; to autocomplete other things, such as filenames, use the Alt + / key. More can be found in the corresponding section in the Bash man page.
Now, let's look at to the command recall shortcuts. The Linux shell has a very nice feature available, which is the history command. This is a system for storing and retrieving all the commands typed into the shell. By default, on a CentOS 7 system, the last thousand commands are stored. This number can also be changed. The command-line history is a very useful feature to save time, by not doing repetitive typing, or to see how a specific command has been executed some time ago. To print out the current history, type history and press Enter. If you want to re-execute a command from this list use the exclamation mark and the corresponding number. Two exclamation marks run the last command from the history. Another exclamation mark notation can be used to extract specific arguments from history commands. This will extract the third argument from the history command, 166, as shown in the following screenshot:
Another very useful history feature is to recall the last command:
- To go through the previous history commands you executed, press the Up arrow key on your keyboard.
- To go back to the next history commands, use the Down arrow key.
- To search through the history for a command, press Ctrl + R and then enter the search keyword.
- To cycle through the results, press Ctrl + R again.
- To run a specific command that you have found, press the Enter key.
- To quickly insert the last argument of the previous command, use Alt + dot.
- Another very useful feature is to shell expand a line manually without actually having to execute the line, which can be useful to find out errors and boxes. This can be done using Ctrl + Alt + E.
Next, we need to know how to work with programs and processes. First, we will discuss how to abort any running program. This is important if you need to quit a command because it is unresponsive or you've made a mistake and want to stop it. For example, let's type the cat command, which will just run forever. Let's ignore what this command is doing at the moment. This leaves the shell unresponsive because cat never finishes running in the forefront of our shell and runs forever. To get back to the shell prompt so we can type in new commands and work again, we need to exit the command while it is running. To do so in the shell, we can use a special key combination that exits the current foreground process. Press Ctrl + C.
You can also suspend a program, which is like pausing its processing and putting it into the background so you can work in the shell again. This can be done as follows:
- Press Ctrl + Z. If you later want to continue the program running in the foreground, type fg and press Enter.
- You can also put it in the background using the bg command while it is suspended. Now the program runs in the background and you can work in the foreground.
- The easiest way to exit this program running in the background is to put it into the foreground and then use Ctrl + C to abort it.
- The next very useful command is to press Ctrl + L, which clears the screen and has the same effect as the clear command.
- The very last useful command we will learn here is to press Ctrl + D, which closes the Bash shell. This is similar to typing the exit command.