How to Clear Linux terminal screen?


Overview

The Linux terminal is an important tool for any user of the operating system, and it's often used to perform various tasks such as viewing log files or running commands. However, sometimes you may need to clear a screen on your computer so that you can see more than one thing at once. In this article we'll look at how to clear a screen in Linux.

It’ll be helpful if we clear the command prompt window so that we can see more clearly what we're doing.

We’ll discuss the different ways to accomplish this.

Using the clear Command

You can use the clear command to clear the contents of a terminal window. This will remove all the text from the current line and move down to the next line.

This will clear the entire screen. You can also use this command to clear only part of the screen by specifying which lines to clear. For example, typing clear 1−10

To clear the screen, one common method is to use the clear function. Let's fill up the screen with some commands and then run the clear function.

$ pwd
/home/groot

$ hostname
workstation

$ clear

If you use the clear command, it also clears the terminal scroll back buffer if the extended E2 capabilities are enabled.

If we want to avoid losing our changes when exiting Vim, we can use the '-x' command line argument.

$ clear -x

Using the reset Command

Another way to clear a screen is to use the reset command. The reset command resets the terminal back to its default settings. It does not delete any data. To reset the terminal, type reset.

If you want to reset the terminal to its original state, you should use the reset command. If you don't want to lose anything, you can use the nohup command instead of resetting the terminal.

$ pwd
/home/groot

$ hostname
workstation

$ reset

The reset command resets the terminal so that it starts from scratch. It’ll be useful if the program doesn’t exit properly, leaving the terminal in an abnormal condition.

Using the Console Codes

We can use the ESC c command to erase the terminal window. The ASCII value for the escape key is decimal 27. We could represent the same number in either octal (033) or hexadecimal (0x1b).

In order to enter the ESC codes into the terminal, press Ctrl+[ (Ctrl+O) followed by [ (Esc). Pressing these keys together will display the following message −

  • Press Enter to continue.

  • Press Esc again to cancel.

  • After pressing Esc, the terminal will return to normal mode.

Clearing the Scrollback Buffer

The scrollback buffer stores information about previous commands typed into the terminal. When you exit the terminal, the scrollback buffer is cleared.

However, there are times when you might want to keep some of the information stored in the scrollback buffer. One reason for this could be that you want to view the history of previously executed commands.

Let's run some command lines to fill up the terminal window and clear it using the Octal Console codes.

$ pwd
/home/groot

$ hostname
workstation

$ printf "\033c"

You can achieve the same results by entering the command in hexadecimal notation −

$ printf "\x1Bc"

Conclusion

Here, we've discussed some useful command line tools for clearing the terminal screen. These commands can be used in daily life while working with the Linux operating system.

Updated on: 26-Dec-2022

973 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements