Clear the screen in linux

6 Commands to Clear Linux Terminal

Sometimes, as you go about running commands on a Linux terminal, you may feel that the terminal is somewhat ‘filled up’ with commands and outputs of those commands. Therefore, you may want to clear the terminal to declutter it and continue from the top once more.

In this tutorial, we are going to outline how you can clear the Linux terminal.

1) clear command

Let’s assume that your terminal is full of commands and output as shown below

Run the clear at the bottom of the terminal prompt.

Results

The terminal screen gets cleared as shown below

2) reset command

Just as in the example above, you need to run reset command at the bottom of the terminal to clear the screen as shown.

Results

As shown below, the terminal has been re-initialized by the reset command.

There’s also the tput reset command which will also do the trick.

3) Using the Ctrl + L shortcut

Another Handy method of clearing the terminal is the use of ctrl+L keyboard shortcut. It will have the same effect as the clear or reset command.

4) Using the printf «\033c» command

Another method one can use is by typing printf «\033c» on the terminal prompt and hitting enter. This will also have the same effect of reinitilaizing the screen.
This can also be defined as an alias through

5) Clearing the Linux terminal for KDE

For a KDE terminal also referred to as Konsole, the following command comes in handy

This can also be aliased as shown

Furthermore, you can use the keyboard shortcut when dealing with Yakauke

6) Clearing the Linux terminal for GNOME — Ubuntu 18.04

For a GNOME terminal, like you would find in Ubuntu 18.04, you can add keyboard shortcuts as shown

Note:

When using the above-mentioned methods to clear the screen, you’ll notice that the screen (putty) is still scrollable. That is one call still scroll up using a mouse or cursor up key to the previous commands and their output. If you want to clear the screen without a scrollback use the command clear && clear :

Conclusion

In this article, we have shown how you can clear the Linux terminal. you are welcome to try out the commands outlined and provide feedback. Thank you.

Источник

How to Really Clear the Terminal Screen in Linux

Normally we use the clear command or press “Ctrl + L” to clear the terminal screen in Linux. Although it works, in reality the screen is not cleared – only the previous output is shifted upwards outside of the viewable area. But what if the requirement is to actually clear the terminal screen?

In this article we will discuss the reset command that makes this possible. The article will also focus on another critical scenario where this command helps.

The problem with “clear”

As I have already mentioned in the beginning, the clear command is the most commonly used command when it comes to clearing the terminal screen in Linux, and to be honest, it does the job most of the time. However, the fact that it just shifts the previous output upwards could result in confusion at times.

For example, imagine a situation where you’re doing something really critical on the command line (such as monitoring network activity to detect a possible hacking attempt) that involves running a single or a set of commands again and again and using mouse scroll or PgUP and PgDown keys to compare/analyze the output.

Now, if you issue a clear command in between to clear the screen, the chances of you committing a mistake are high as it’s sometimes hard to differentiate where the output of the previous command ends and that of the current command begins.

Читайте также:  Windows sp1 and sp2

For example, the following screenshot (taken after scrolling the terminal window up a bit) shows the use of “Ctrl + L” while running the ls -lart command again and again.

How “reset” solves the problem

If you take a quick look at the reset command’s man page, you’ll see that it says the command eventually initializes the terminal – or better put, re-initializes the terminal – instead of just manipulating the position of output which clear does.

Keep in mind, however, that the reset command does not have any effect on the state of the shell (bash), meaning it remains unaltered.

What else can “reset” do?

There are times when you accidentally try opening an executable file in the terminal window using the cat command. Needless to say, the output produced is all garbage as the file in question is a binary file.

That’s not a problem in most cases, as you can press “Ctrl + C” to get your prompt back and move on with your work. But sometimes the operation (displaying the contents of a binary file) could even result in your command line prompt getting corrupt:

And anything you write is also displayed as garbage characters.

That’s usually because somewhere in the binary data there are some control sequences that are interpreted by the terminal as requests to change the character set used to draw. To restore things back to normal, just run the reset command.

Note: type the reset command carefully as the characters displayed on the terminal will be garbage until the command is successfully executed.

Conclusion

To sum it up, the reset command is your friend-in-need – from actually clearing the terminal screen to fixing the terminal display, it’s usually your last resort. To learn more about the command, I’d encourage you to go through its man page.

Himanshu Arora is a freelance technical writer by profession but a software programmer and Linux researcher at heart. He covers software tutorials, reviews, tips/tricks, and more. Some of his articles have been featured on IBM developerworks, ComputerWorld, and in Linux Journal.

Источник

Clear! (clear your terminal screen)

Administrator

When you’re typing a lot in a terminal and want to clear the screen quickly, you can do so easily a couple different ways.

The one most people know about:

Yeah, simply type ‘clear’.

The one you’ll use a lot more:

(hit the ctrl key, then a lowercase L)

It doesn’t make everything go away, you can actually see everything if you scroll up, but its very handy and I can’t tell you how many times I use it each day.

(really — I can’t tell you, because when you type ‘clear’ you’ll see it in your history, but if you use ctrl-l, it doesn’t make it into history)

JasKinasis

Well-Known Member

Rob, this might be a little advanced for a beginners tutorial, but I feel it is worth mentioning:

The Ctrl-l shortcut only works as a shortcut for the «clear» command if you have readline set to use the default emacs input option. But it doesn’t work if you set the vi input mode — at least not when in edit mode.

I use the following line in my .bashrc, which puts readline into vi input mode:

In the vi input mode, the Ctrl+l keybind is only available when in ‘command’ mode. So you have to hit and then Ctrl-l. Which is not really very helpful. It would also be handy to have it available in ‘insert’ mode too.

The good news is — you can easily add a keybind for Ctrl-l for ‘insert’ mode by adding the following line to your .bashrc:

Either method works. I edited .bashrc because it involved less typing and because I pretty much use bash exclusively.

But if you tend to switch between using different shells.
e.g. Bash, zsh, ksh, csh etc.
Then putting the settings into .inputrc will apply those settings to ANY shell that uses readline. In other words — no matter what shell you are using, your keybinds/settings for readline will always be the same.
Whereas .bashrc only applies to bash.

Читайте также:  Asus ze500kl драйвера windows 10

There are many different bits of functionality and options available in readline. So if there are any key-binds/shortcuts to functionality that you feel you are missing in either vi or emacs mode, you can easily set up a new keyboard shortcut — as I have done for clear-screen in the vi insert mode.

The man page for readline contains a lot of information about the functionality/options that are available.

KarlLinux

New Member

NeoBeum

New Member

Abhijit Mohanty

New Member

I tried both, however, I could scroll up and view the entire commands. [just started linux]

Administrator

I tried both, however, I could scroll up and view the entire commands. [just started linux]

Abhijit Mohanty

New Member

atanere

Well-Known Member

If you want to delete the entire command history, see if this works:

TCRatius

Member

Rob, this might be a little advanced for a beginners tutorial, but I feel it is worth mentioning:

The Ctrl-l shortcut only works as a shortcut for the «clear» command if you have readline set to use the default emacs input option. But it doesn’t work if you set the vi input mode — at least not when in edit mode.

I use the following line in my .bashrc, which puts readline into vi input mode:

In the vi input mode, the Ctrl+l keybind is only available when in ‘command’ mode. So you have to hit and then Ctrl-l. Which is not really very helpful. It would also be handy to have it available in ‘insert’ mode too.

The good news is — you can easily add a keybind for Ctrl-l for ‘insert’ mode by adding the following line to your .bashrc:

Either method works. I edited .bashrc because it involved less typing and because I pretty much use bash exclusively.

But if you tend to switch between using different shells.
e.g. Bash, zsh, ksh, csh etc.
Then putting the settings into .inputrc will apply those settings to ANY shell that uses readline. In other words — no matter what shell you are using, your keybinds/settings for readline will always be the same.
Whereas .bashrc only applies to bash.

There are many different bits of functionality and options available in readline. So if there are any key-binds/shortcuts to functionality that you feel you are missing in either vi or emacs mode, you can easily set up a new keyboard shortcut — as I have done for clear-screen in the vi insert mode.

The man page for readline contains a lot of information about the functionality/options that are available.

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

wizardfromoz

Administrator

Hi @TCRatius and a belated welcome to the site.

A search from our home page under «text editor» just on the words yields close to 200 results, but narrowing it down to in the title — less than 2 pages.

@Rob’s poll here https://www.linux.org/threads/poll-favorite-text-editor.6/ invites comment, I am going there tomorrow I hope

TCRatius

Member

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

Administrator

Hey there TCRatius,

Welcome to the forums — sure, we’d love it if you shared a link to us from another forum.

JasKinasis

Well-Known Member

As the saying goes:
«Necessity is the mother of all invention»

I’ve been using Linux for many years — and I’ve always learnt things as and when I’ve needed to. And I’m continuing to learn. I’m always learning new things.

Any time I have a problem, or want to know how to be able to do something new — I’ve always looked to the documentation.

Now, I know telling people to RTFM is a bit of a cliche, but seriously:
RTFM FTW!

Читайте также:  Отличие версий windows домашняя или профессиональная

There is a LOT of information in the man and info pages. Reading through it can be time well spent. And if you don’t want to read the entire manual — you could always just search for parts of the manual that you are interested in using the search command.
e.g.
In the man-page viewer, you can press / and enter a regex to search for specific text.

If the documentation is incomplete, or unclear — I will look to the internet for answers. And 99% of the time I manage to find an existing solution without having to post a question myself.

Sometimes it takes a bit of time to find exactly what I need, so a bit of patience and persistence is required. But to me — it’s worth it!

To clear up any misunderstanding that you may have:
My original post has nothing to do with the systems default text editor, or anybody’s preferred editor — it is purely about the default shortcut keys available in Bash via GNU readline — which depends on which input-style readline is set-up to use (emacs or vi).

GNU readline is a command-line utility which is used by various command-line tools to get input from users. This includes shells like bash.

So when you are typing commands in bash — whether you know it or not, you are using readline. And whilst editing commands in bash you can use emacs or vi style keyboard shortcuts. Again, the default for readline is emacs, so whether you knew it or not — in Bash, you’ve had emacs style keyboard shortcuts available to you the whole time. Even if you haven’t been using them — they are there.

When you are using nano (or whatever your favourite command line text editor is) you are using nanos keyboard shortcuts. But when typing commands in Bash, depending on how readline is set up, you will either be using emacs style keyboard shortcuts, or vi-style shortcuts.

In my original post, I was pointing out that Robs suggestion to use «ctrl + l» to clear the screen in the terminal only works «out of the box» if your shell has readline set to its default ’emacs’ input mode.

Personally, I’m a vim user — so I quite like using the vi input mode for readline.

If readline is set to use the vi input mode, you have to press — to switch from vi-insert mode to vi-command mode and then press «ctrl +l» to clear the screen. And it’s a little clunky, having to press and then «ctrl+l».

So for the benefit of other users who use readlines vi option — I demonstrated how to set up a custom keybind for readlines vi-insert mode, which would allow «ctrl+l» to be used to clear the screen whilst editing a command (without having to press first).

WRT .bashrc:
You can edit .bashrc with ANY text editor you like. But the edits specified in my post will only set up a new key-bind for readlines vi-input mode.

Readline doesn’t have a nano input mode. It only has emacs or vim input modes. But if you really wanted to, you could create custom keybinds for any of readlines emacs or vi input modes, that will mimic keybinds/functionality from nano or any other editor.

But, many of nano’s keyboard shortcuts are similar to the emacs shortcuts anyway.
e.g.
ctrl+n = next line,
ctrl+p = previous line,
ctrl+f = next character,
ctrl+b = previous character
etc.

So for a nano user, the emacs input method for readline would probably meet most, if not all of your needs anyway!

I hope I explained all of that clearly enough.
If you have any further questions, fire away!

For reference:
Here is the list of the default keyboard shortcuts for Bash — including the emacs-style «cursor movement» and «editing» keybinds:
https://gist.github.com/michaelcoyote/745d8a6cf293c4bf7e31

Switching to the vi mode would change all of the keybinds in the «cursor movement» and «editing» sections to be more vi-like. The other commands (history, process control etc) are mostly the same.

Источник

Оцените статью