Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd please sign up again. Thanks!

Clear! (clear your terminal screen)

Discussion in 'Linux Beginner Tutorials' started by Rob, Apr 25, 2017.

  1. Rob

    Rob Administrator
    Staff Member

    Joined:
    Oct 27, 2011
    Messages:
    89
    Likes Received:
    349
    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:
    Code:
    clear
    Yeah, simply type 'clear'.

    The one you'll use a lot more:
    Code:
    ctrl + l
    (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)
     
    KarlLinux likes this.
  2. JasKinasis

    JasKinasis Member

    Joined:
    Apr 25, 2017
    Messages:
    36
    Likes Received:
    35
    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 {see *} 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.

    {* NOTE: readline is a utility that is used by bash and other shells to get input from the user.}

    I use the following line in my .bashrc, which puts readline into vi input mode:
    Code:
    set -o vi
    
    In the vi input mode, the Ctrl+l keybind is only available when in 'command' mode. So you have to hit <esc> 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:
    Code:
    bind -m vi-insert "\C-l":clear-screen
    
    Alternatively, instead of editing .bashrc - you could create or edit .inputrc, which is a config file used by readline:
    Code:
    set editing-mode vi
    $if mode=vi
    
    # Set up a Ctrl+l key-bind for vi's insert-mode
    set keymap vi insert
      Control-l: clear-screen
    
    # Set up a Ctrl+l key-bind for command mode
    # Note: This key-bind is already defined in vi mode
    # I've put it here as an example of how to create a
    # vi command-mode shortcut
    set keymap vi command
      Control-l: clear-screen
    
    $endif
    
    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.
    Code:
    man readline
    
     
    #2 JasKinasis, May 1, 2017
    Last edited: May 2, 2017
    siva arja and Rob like this.
  3. KarlLinux

    KarlLinux New Member

    Joined:
    May 2, 2017
    Messages:
    17
    Likes Received:
    5
    Thanks for that tip. Saves a lot of time typing 'clear'.
     
    #3 KarlLinux, May 2, 2017
    Last edited: May 8, 2017
    siva arja likes this.
  4. NeoBeum

    NeoBeum New Member

    Joined:
    May 19, 2017
    Messages:
    1
    Likes Received:
    0
    Hi, I've only been using Linux for about a month, but I use this:
    Code:
    sudo nano ~/.bashrc
    Code:
    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    
    Code:
    sudo nano ~/.bash_aliases
    Code:
    alias cls="printf '\033c'"
    Code:
    source ~/.bashrc
    Code:
    cls
     

Share This Page