Table of contents
- Making life easier for the Linux beginner - Part 1
- Problem 1: Cluttered terminal screen
- Solution: Wipe the terminal screen clean
- Problem 2: Unable to recall commands which you executed in the past
- Solution: Use the history command to show a list of all previous commands
- Problem 3: Having to re-type previous commands (difficulty in recalling the details)
- Solution: Select a command from the history list using its line number
- Problem 4: It is time-consuming to try to locate a command from the history
- Solution: Quickly search history for a matching command using reverse-i-search
- Problem 5: How to save incomplete commands to complete them later (avoid retyping)
- Solution Option 1: Save the string to the command history
- Solution Option 2:
Making life easier for the Linux beginner - Part 1
With this post, I provide quick and easy solutions to x5 typical problems when you interact with your terminal environment, which will make things a little bit easier for you. You can play around with these concepts and devise different implementations as you continue to learn.
Problem 1: Cluttered terminal screen
After executing many commands, you have reached the bottom of the terminal screen display, leaving the entire screen cluttered. Or maybe you want to hide previous outputs displayed on the screen from bystanders.
Solution: Wipe the terminal screen clean
To wipe all entries from the visible terminal screen area, you can use the clear command.
This will shift the previous command entries and outputs upwards, outside of the visible screen area. (Do not worry, you will still be able to reach them by scrolling up and down).
$ clear
Tip: Ctrl+L is the keyboard shortcut for clear. This is an even quicker way to clear the screen.
Problem 2: Unable to recall commands which you executed in the past
You can use the history command to show you a list of the commands that have been entered in the past.
Solution: Use the history command to show a list of all previous commands
history
Example:
Problem 3: Having to re-type previous commands (difficulty in recalling the details)
Solution: Select a command from the history list using its line number
To recall and execute a specific command in your shell history, use an exclamation point (!) followed immediately by the number of that command's history line, followed by pressing Enter.
Execute command number 3:
$ !3
Execute the last command in the history list
$ !!
Problem 4: It is time-consuming to try to locate a command from the history
Solution: Quickly search history for a matching command using reverse-i-search
Type Ctrl-R to invoke a recursive search of your command history. After typing this, the prompt changes to:
(reverse-i-search)\`':
Then start typing the character sequence to match. If you find the one you want, press Enter to execute that command.
For example, you can easily activate different Python virtual environments, especially when your virtual environment folder locations are nested deep inside several subdirectories. Instead of typing out the complete and long directory location each time, search for it using Ctrl-R and hit Enter.
Problem 5: How to save incomplete commands to complete them later (avoid retyping)
Sometimes, when halfway through typing a long, complex command string, you realise that you need to execute another command first. But you do not want to lose what you have already typed, only to have to retype it all again later.
Solution Option 1: Save the string to the command history
To avoid retyping the command string later, you can save it in history as a comment and retrieve it later. To this by inserting a hash symbol (#) at the beginning of the command and press Enter. This will change the command into a comment and store it in the command history.
Tip: Type Ctrl-A to take your cursor to the start of the line.
Example of a command saved in history as a comment.
2017 ls -lah
2018 df -h
2019 du -h
2020 # This is my loooooong command....
2021 history
As soon as you need to complete the stored command (currently a comment), retrieve it from the command history. Remember to remove the hash (#). After completing the command, press Enter to execute it.
Solution Option 2:
As an alternative to storing the command in history as a comment, you can use Ctrl-U to copy and remove (cut) the line. You can then type and execute other commands. And when needed, you can retrieve your previously cut command by pasting it back to the command line using Ctrl-Y.
Tip: The Ctrl-U command cuts the line from the cursor's location in the backwards direction. So be sure to move the cursor to the end of the line before cutting.