Clearing The Terminal In Linux And Windows

Clearing the terminal in Linux and Windows is easy - it's one of the first commands that newbies learn. But there's more than meets the eye.

On this page
  1. Command to Clear the Screen in Linux
  2. Command to Clear the Screen in Windows
  3. Clearing the Terminal History
  4. Conclusion

When starting with learning terminal commands on any operating system, clearing the screen is one of the things you learn. It’s also the most frequently used command and is easy to understand. But there’s a lot more to it than meets the eye. In this article, I’ll show you not only how to clear the screen in both Linux and Windows, but also how you can improve the experience and clear not just the terminal, but the terminal history of commands.

#Command to Clear the Screen in Linux

In Linux, you type “clear” to clear the screen like this:

Code
clear

It’s one of the few Linux commands not to have any options you can use, even though it relies on some environment variables to control its function.

Using a Shortcut to Clear the Screen

Less well-known, is that Linux has a shortcut to clear the screen:

Code
Ctrl+L

If you clear the screen a lot, the shortcut can make your workflow smoother.

Creating an Alias for the Clear Command

If you don’t want to use the shortcut, another option to make your work more efficient is to create an alias for the clear command so you can type that instead of “clear”. For example, here’s an alias command you can use to replace it with the letter “c” instead:

Code
alias c='clear'

I’d written a tutorial earlier on how to create an alias using the bashrc file. As you can see, Linux allows you to customize even a simple command like “clear”.

Clearing vs Scrolling

It’s important to note that in Linux, clearing the screen merely scrolls the window down by one screen. So while it appears blank, your previous command and its output are still visible by simply scrolling up.

#Command to Clear the Screen in Windows

In Windows, there are typically two terminals you will use. The command prompt and the Windows PowerShell.

Windows Command Prompt

You can open the Windows command prompt by typing “cmd” into the Start Menu. To clear the screen, you use:

Code
cls

This is the original DOS command that oldies like me learned in school when we first started working with computers. Even though Microsoft ditched DOS a long time ago, you can still mimic the interface using the command prompt.

Creating an alias command in the Windows command prompt is possible, but complicated. For example, you can create a temporary alias like this:

Code
doskey c=cls

Where “c” is the character used as the alias for cls. However, this alias will only last for the duration of the session. To make it permanent, you need to do the following:

  1. Create a batch file with the “doskey c=cls” command
  2. Configure it to execute when the command line starts up, by adding the batch file to the startup parameters of the program shortcut you use to run it.

As you can see, creating aliases isn’t a core functionality of the command prompt, and you need to use hacks to make them permanent.

The Windows PowerShell

The Windows PowerShell is a more complex program than the command line and allows users to carry out powerful tasks like automation and scripting, whereas cmd only allows for batch executions. In PowerShell, you can use either of the two following commands to clear the screen:

Code
clear

or

Code
cls

So PowerShell subsumes the functionality of the command prompt. To create a temporary alias for the “clear” command in PowerShell, use the following command:

Code
Set-Alias c Clear-Host

To make it permanent, you need to modify the $PROFILE file and you can do that by typing in:

Code
notepad $PROFILE

If this file exists, PowerShell will open it. You can test if it exists using the following command:

Code
Test-Path $PROFILE

If it returns “false”, it means the file doesn’t exist. You can create it by pasting the following code into PowerShell:

Code
if (-not (Test-Path -Path $PROFILE)) {
    New-Item -ItemType File -Path $PROFILE -Force
}

Now if you use “Test-Path”, it’ll show true as shown here:

Creating the PROFILE File in PowerShell

Creating the PROFILE File in PowerShell

As you can see, we’ve just created the $PROFILE file. Now using:

Code
notepad $PROFILE

We can type the alias command into the file and save the changes.

Clearing vs Scrolling in Windows

Unlike Linux, when you clear a screen in the PowerShell of Windows, you don’t merely scroll up. Instead, PowerShell wipes everything clean, so you can’t see what you previously entered simply by scrolling. It’s gone forever.

#Clearing the Terminal History

There’s a lot more you can clear in a terminal than just the screen. For one reason or another, you might want to clear your command history as well. For example, on Linux, here’s what you see when you type in the “history” command:

History Command in Linux

History Command in Linux

As you can see, all the commands I’ve typed in the recent past show up here. What might surprise you is that the command history persists beyond shell sessions. As you can see, I’ve exited twice recently, and it still shows up! So if, for whatever reason, you need to clear the history, you can use the following command:

Code
history -c

This wipes out all the history in the current memory, so when you type “history” again, it only shows the most recent command – namely history. However, the history commands are not erased from all storage. They persist on the hard drive in the following location:

Code
~/.bash_history

You can see below, that despite using the “history -c” command to erase the history, the file still contains the commands:

History Still Exists in File

History Still Exists in File

So if you want to clear the terminal history permanently, you need to erase them from the “.bash_history” file as well. You can do this by entering the following command:

Code
> ~/.bash_history

The above command will erase everything in the .bash_history file. While using it, don’t forget to use it in conjunction with “history -c”, otherwise you won’t erase everything.

Clearing Terminal History in Windows

As we saw earlier, there are two ways to access the terminal in Windows. For the simpler program – the command line, there’s no way to clear the history other than simply opening and closing the program. The history isn’t stored anywhere else, so you don’t need to worry.

For PowerShell, on the other hand, you can use the following command:

Code
Clear-History

Here’s an illustration of how it works:

Clearing History in PowerShell

Clearing History in PowerShell

As you can see, we can use the “Get-History” command to see the terminal history in PowerShell, and the “Clear-History” command erases it. Properly speaking, these are not “command”, but “cmdlets, but for practical purposes, it’s the same thing.

Like with Linux, the history in PowerShell is also stored in a file:

Code
~\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt

If you want a sure way to clear the terminal history, just wipe out the contents of the above file. You can even delete it altogether using the following command:

Code
Remove-Item -Path "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"

As you can see, there exist methods in both Linux and Windows to clear the terminal history from both in-memory, as well as from the file that stores the history.

#Conclusion

The ability to clear the terminal seems like something very basic, but it’s also the most frequently used. If you want to make sure that you’ve cleared not just the text on the screen, but also the terminal history, then you will find this article useful!

Photo of Bhagwad Park
Written by

Bhagwad Park

Bhagwad Park writes for the NameHero blog.

More from Bhagwad →