Clear a terminal screen for real

前端 未结 11 1442
悲哀的现实
悲哀的现实 2020-11-30 15:48

Using the clear command on the terminal only fools the user into thinking the screen has been cleared...you can still see output from the previous commands when

相关标签:
11条回答
  • 2020-11-30 16:22

    My favourite human friendly command for this is:

    reset
    

    Tested on xterm and VT100. It also helps after an abnormal program termination. Keeps the command buffer, so up-arrow will cycle through previous commands.

    cheers :D

    0 讨论(0)
  • 2020-11-30 16:24

    Use the following command to do a clear screen instead of merely adding new lines ...

    printf "\033c"
    

    yes that's a 'printf' on the bash prompt.

    You will probably want to define an alias though...

    alias cls='printf "\033c"'
    

    Explanation

    \033 == \x1B == 27 == ESC
    

    So this becomes <ESC>c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.

    Edit

    Here are a few other ways of doing it...

    printf "\ec" #\e is ESC in bash
    echo -en "\ec" #thanks @Jonathon Reinhart.
    # -e    Enable interpretation of of backslash escapes
    # -n    Do not output a new line
    

    KDE

    The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer...

    clear && echo -en "\e[3J"
    

    Or perhaps use the following alias on KDE...

    alias cls='clear && echo -en "\e[3J"'
    

    I got the scroll-back clearing command from here.

    0 讨论(0)
  • 2020-11-30 16:24

    I know the solution employing printing of new lines isn't much supported, but if all else fails, why not? Especially where one is operating in an environment where someone else is likely to be able to see the screen, yet not able to keylog. One potential solution then, is the following alias:

    alias c="printf '\r\n%.0s' {1..50}"
    

    Then, to "clear" away the current contents of the screen (or rather, hide them), just type c+Enter at the terminal.

    0 讨论(0)
  • 2020-11-30 16:28

    With KDE and Ubuntu 12.04 LTS and the "Konsole" terminal, none of the posted answers work. However, pressing default keyboard shortcut CTRL+Shift+X does work! Source:

    https://bugs.kde.org/show_bug.cgi?id=288913

    0 讨论(0)
  • 2020-11-30 16:29

    Just to add that tmux scroll buffer does not clear with clear, reset or printf. You need to :clear-history. See link.

    0 讨论(0)
  • 2020-11-30 16:34
    tput reset
    

    That will do the trick!

    0 讨论(0)
提交回复
热议问题