Kiến Thức Linux

How to Clear Terminal in Linux {Clear Screen in Linux}

Introduction

The clear command is the go-to tool for clearing the terminal screen in Linux. Despite its effectiveness, clear does not reinitialize the terminal, which is sometimes necessary. Some alternative methods provide users with that option as well.

Read this tutorial to learn how to clear the terminal in Linux using different methods.

How to Clear Terminal in Linux {Clear Screen in Linux}

Clear Terminal via clear Command

The fastest way to clear the terminal screen in Linux is with the clear command. In most terminal emulators, like GNU, running clear without any arguments creates a blank-slate screen:

clear
clean command before terminal output

Once executed, the command clears up the terminal:

clear command after terminal output

The command deletes everything, including the scrollback buffer. To keep the scrollback buffer, use clear with the -x argument:

clear -x
clear -x terminal output before

The clear -x command clears the terminal, but previous output is still available. Scroll up or use the PgUp button:

clear -x terminal output after

However, in some terminal emulators, clear without arguments doesn’t delete previous output but shifts it upwards (same as clear -x in GNU). In that case, accessing earlier output is possible with the PgUp key.

Additionally, the clear command does not reset the terminal. The shell state remains the same as before.

Clear Terminal via reset Command

Unlike clear, the reset command reinitializes the terminal and restores settings to default. The reinitialization process covers tab extensions, turns off echo and raw modes, and turns on newline translation.

The command resets a terminal that is in an abnormal state and reinitializes the command line:

reset
reset terminal output before

Executing reset takes a few moments to complete while clear shows effect instantly.

Clear Terminal via Ctrl+L / Ctrl+Shift+K Shortcut

Keyboard shortcuts also work for clearing the terminal, depending on the terminal emulator.

In GNOME, the Ctrl + l shortcut has the same effect as clear -x. The shortcut clears the terminal while leaving the scrollback buffer:

ctrl + l and new command terminal output

Typing in a new command, like whoami, is done on a clear screen. Still, users can access command output history with PgUp or by scrolling up:

ctrl + l terminal output

An alternative in some terminal emulators is Ctrl + Shift + K. The command provides the same output as Ctrl + L.

Clear Terminal via alias Command

Alternative methods for clearing the terminal are also more complicated. For instance, the 33 is the ASCII escape character used to start terminal control sequences. When followed by c, the command clears the terminal.

printf "33c"
printf 33c terminal output

The command clears the terminal screen, including the scrollback buffer. To avoid typing numbers, create an alias for the command. For instance, set x as an alias for printf "33c" with:

alias terminal output

Once the alias is created, running x results in a clear terminal screen.

Conclusion

Now you know how to clear the terminal screen using a few different methods. Next, check out the ultimate guide of all Linux commands everyone should know.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !