59

When we use clear command or Ctrl+L in terminal, it clears terminal but we can still scroll back to view the last used commands. Is there a way to completely clear the terminal?

  • It's terminal dependent, and none of the answers address that. – Thomas Dickey Oct 19 '16 at 1:11

11 Answers 11

69

You can use tput reset.

Besides reset and tput reset you can use following shell script.

#!/bin/sh
echo -e \\033c

This sends control characters Esc-C to the console which resets the terminal.

Google Keywords: Linux Console Control Sequences

man console_codes says:

The sequence ESC c causes a terminal reset, which is what you want if the screen is all garbled. The oft-advised "echo ^V^O" will only make G0 current, but there is no guarantee that G0 points at table a). In some distributions there is a program reset(1) that just does "echo ^[c". If your terminfo entry for the console is correct (and has an entry rs1=\Ec), then "tput reset" will also work.

  • 1
    What's the difference between reset and tput reset? – Toothrot Oct 18 '16 at 12:19
  • tput reset is faster than reset in Ubuntu 16.04, at least – Iván Rodríguez Torres Feb 13 '17 at 11:22
  • 5
    -1, you can still see the buffer if you scroll up – João Pimentel Ferreira Jun 18 '17 at 10:31
15

You can use the reset command, that will reset the terminal settings.

15

I know you're on a gnome terminal, but I thought I'd answer with a tip for others who might be (like me) on a Mac:

If you're using Terminal.app or iTerm.app then Control+L will scroll up so the terminal looks blank, but Cmd+K will actually reset the terminal / clear scroll-back.

Or, if you're able to set keyboard preferences for your terminal you may be able to assign something like Ctrl+K to input echo -e \\033c as was mentioned above.

  • 2
    Cmd+K is all i wanted. ty – Gaurav Gandhi Mar 30 '16 at 11:13
9

Anthon's answer works in KDE Konsole, but not urxvt. In urxvt, I've been using reset for a few years to clear the scrollback (along with all the other things it does), and wasn't satisfied that it didn't work in Konsole. So now for Linux I have a new alias:

alias allclear='clear; echo -e "\033c\e[3J"'

But it doesn't work on OS X.

tput reset doesn't work in any context AFAICT.

In KDE Konsole, ctrl-shift-k clears the scrollback (including the current shell prompt, so it's completely empty). In iTerm or Apple's terminal on OS X, cmd-shift-k also clears scrollback. To add this feature to urxvt, add this to ~/.Xresources:

urxvt*keysym.C-S-K: command:\033c
  • This answer is the answer that anybody using Linux Bash will love if they are expecting the same result as they would receive on a Mac by pressing command + K. I went ahead and turned this into a standalone command by adding the following function to the bottom of /etc/bash.bashrc (then log out of server, log back in and it will work via the command allclear): allclear(){ ` echo "Clearing terminal and scrollback..."` ` sleep 1.5` ` clear; echo -e "\033c\e[3J"` } ` Edit: Can't get formatting to work. If anybody is able to edit this so it is readable, I would appreciate it. – KLaw Feb 7 '18 at 16:26
3

I found this on the net years ago, and works well for me. It completely clears the terminal with no scroll history.

echo -e "\e[3J"
2

I mapped F12 to clear screen in each of the following shells: bash, csh and fish shell. My command is different from all previous ones because it preserves whatever you've already typed in the current line.
(Note: all the configurations below are terminal dependent, but I hope they will work virtually everywhere)

bash

Open the file ~/.inputrc and insert the following line:

"\e[24~": "\C-k \C-uecho -ne '\\ec\\e[3J'; history -d $((HISTCMD-1))\n\C-y\C-?"

To have the new key binding available, either open a new terminal or type Ctrl+X then Ctrl+R to reload the .inputrc file in the current terminal.

Explanation
The file ~/.inputrc controls the key bindings of the bash terminal.

About the line:

  1. The first part of the line, "\e[24~", maps the key F12.
  2. The C-k (Ctrl+X) clears the kill-ring (bash's copy-and-past memory).
  3. The space avoids a beep in the next command.
  4. The C-u (Ctrl+U) sends the current line to the kill-ring, and erase it.
  5. The echo -ne '\\ec\\e[3J'; history -d $((HISTCMD-1))\n sends two grouped commands to the terminal. They are separated by ; and terminated by \n.
    • The echo -ne '\\ec\\e[3J' clears the screen.
    • The history -d $((HISTCMD-1)) avoids these two commands from entering in history.
  6. The C-y (Ctrl+Y) pastes the kill-ring stored by the C-u command.
  7. The C-? (Ctrl+?) is equivalent to Backspace and removes the space inserted in step 3 above.

References:

csh

Type the command bellow.
bindkey -c "^[[24~" "echo -ne '\ec\e[3J'"
It can also be inserted in your .cshrc file to load the key binding to every new terminal.
(Yes, it is much simpler than in bash)

fish shell

Type the command bellow.
bind -k f12 "echo -ne '\ec\e[3J'; commandline -f repaint"
You can edit and save the function fish_user_key_bindings to load the key binding to every new terminal.
(Yes, it is much simpler than in bash)

2

[Ubuntu 16.04]

The .bash_history file is created in the user's home directory once the terminal is closed

It is useful, but you could delete it if you wish.. Nevertheless it would be created every time you finish using the terminal.

You could delete it manually from:

  • your files browser.

  • the terminal: remember that a new .bash_history file will be created when you close the terminal (based in its default configuration).

Or you can call a custom function if you save the functionality you need into a file linked from the bashrc.. you could edit the bashrc itself if you like putting your code there, but the next examples are in a separate file

Honestly this is not useful for me but if you wish is to delete the commands history before exit you could do this:

.bash_custom #this is my custom file

blotout() {
    HISTSIZE=0
    rm $HOME/.bash_history
    exit
}

then at the bottom of add the name and path of my file
.bashrc # this is the configuration file for the bash (I think)

# existent code
#...
#..

# import user customizations
source $HOME/.bash_custom

and thatś all.

BUT, to do exactly what you want you just need this function in your custom file and link it:

refresh() {
        tput reset
        H=HISTSIZE
        HISTSIZE=0
        HISTSIZE=H
}

Or just put the function in the .bashrc if you want but that way you probably need an export statement after the function I'm not sure and if the file is updated probably lost your functions.. I'm not sure about that too :D .
Remember you have to re-start the terminal when edit the .bashrc or your 'custom' file.

1

For me, using PuTTY against CentOS 7, clear leaves only the clear command in the scrollback, but running "clear && clear", two instances of the clear command at the same time, completely clears the scrollback and leaves me with no scrollbar. Try that. It's a weird solution but none of these here cleared the scrollback that effectively, and this does.

0

Added to ~/.inputrc to bind the complete clearing to F12:

"\e[24~":'!echo -ne \047\\0033\\0143\047\r'
0

In Fedora 30, deleting the history is done by removing the $HOME/.bash_history file. The easiest way to clear the terminal history file via the keyboard is:

  • Press Alt+F4 to close each GNOME Terminal window, cycling through windows via Alt+Tab or Alt+Shift+Tab as needed. (All terminal windows must be closed because the .bash_history file is created or updated when they’re closed.)
  • Press Alt+F2.
  • Type nautilus and press the Enter key to open the (GNOME) Files application (which was formerly known as Nautilus).
  • Press Ctrl+H if needed to show hidden files.
  • Type .bash_history (which should appear in the search bar if typed immediately after the window is opened; otherwise, the search bar can be accessed via Ctrl+L).
  • Press the down arrow key to select the file, which should be the only search result.
  • Press Shift+Delete to permanently delete the file. (If you use the unmodified Delete key instead, the .bash_history file will still be accessible via the Trash folder.)
-2
cat /dev/null > ~/.bash_history && history -c && exit
  • The least you can do is explaining and formatting your answer. – Paradox May 16 at 16:37
  • (1) It is not clear that the OP wants to delete the shell’s memory (history) of previous commands (the question is about the terminal).  You can’t see it, but history -c has been presented in two previous answers, and they were deleted for not being correct answers to the question.  (2) While closing the terminal window will (probably) erase the terminal’s memory of previous commands, it’s far from apparent that the OP wants to do that. … And P.S. A single exit command isn’t guaranteed to close the terminal window. – G-Man Says 'Reinstate Monica' May 16 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.