How do I clear the Gnome terminal history?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 3

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.

Method 4

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 "33ce[3J"'

But it doesn’t work on OS X.

tput reset doesn’t work in any context AFAICT.

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

urxvt*keysym.C-S-K: command:33c

Method 5

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"

Method 6

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))nC-yC-?"

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 'ece[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 'ece[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)

Method 7

[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 😀 .
Remember you have to re-start the terminal when edit the .bashrc or your ‘custom’ file.

Method 8

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.

Method 9

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

"e[24~":'!echo -ne 47\0033\014347r'

Method 10

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.)

Method 11

cat /dev/null > ~/.bash_history && history -c && exit


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x