Clear! (clear your terminal screen)

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
1,210
Reaction score
2,240
Credits
3,485
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)
 


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
 
Last edited:
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
 
Very
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
How do you learn all this. Even the man page is liken to an epic novel. Interesting enough, you seem to like vi and emacs over nano. For sake of argument. Can you edit Bashrc with nano and achieve same result?
 
Cheers thank you for the welcome. Taken me many years of stumble but I finally found my way home. You don't mind if I share a link to this website on another forum for anyone wanting to take the dive into Linux?
 
Cheers thank you for the welcome. Taken me many years of stumble but I finally found my way home. You don't mind if I share a link to this website on another forum for anyone wanting to take the dive into Linux?
Hey there TCRatius,

Welcome to the forums - sure, we'd love it if you shared a link to us from another forum.

Rob
 
How do you learn all this. Even the man page is liken to an epic novel.
As the saying goes:
"Necessity is the mother of all invention"

I've been using Linux for many years - and I've always learnt things as and when I've needed to. And I'm continuing to learn. I'm always learning new things.

Any time I have a problem, or want to know how to be able to do something new - I've always looked to the documentation.

Now, I know telling people to RTFM is a bit of a cliche, but seriously:
RTFM FTW!

There is a LOT of information in the man and info pages. Reading through it can be time well spent. And if you don't want to read the entire manual - you could always just search for parts of the manual that you are interested in using the search command.
e.g.
In the man-page viewer, you can press / and enter a regex to search for specific text.

If the documentation is incomplete, or unclear - I will look to the internet for answers. And 99% of the time I manage to find an existing solution without having to post a question myself.

Sometimes it takes a bit of time to find exactly what I need, so a bit of patience and persistence is required. But to me - it's worth it!

Interesting enough, you seem to like vi and emacs over nano. For sake of argument. Can you edit Bashrc with nano and achieve same result?

To clear up any misunderstanding that you may have:
My original post has nothing to do with the systems default text editor, or anybody's preferred editor - it is purely about the default shortcut keys available in Bash via GNU readline - which depends on which input-style readline is set-up to use (emacs or vi).

GNU readline is a command-line utility which is used by various command-line tools to get input from users. This includes shells like bash.

So when you are typing commands in bash - whether you know it or not, you are using readline. And whilst editing commands in bash you can use emacs or vi style keyboard shortcuts. Again, the default for readline is emacs, so whether you knew it or not - in Bash, you've had emacs style keyboard shortcuts available to you the whole time. Even if you haven't been using them - they are there.

When you are using nano (or whatever your favourite command line text editor is) you are using nanos keyboard shortcuts. But when typing commands in Bash, depending on how readline is set up, you will either be using emacs style keyboard shortcuts, or vi-style shortcuts.

In my original post, I was pointing out that Robs suggestion to use "ctrl + l" to clear the screen in the terminal only works "out of the box" if your shell has readline set to its default 'emacs' input mode.

Personally, I'm a vim user - so I quite like using the vi input mode for readline.

If readline is set to use the vi input mode, you have to press <esc> - to switch from vi-insert mode to vi-command mode and then press "ctrl +l" to clear the screen. And it's a little clunky, having to press <esc> and then "ctrl+l".

So for the benefit of other users who use readlines vi option - I demonstrated how to set up a custom keybind for readlines vi-insert mode, which would allow "ctrl+l" to be used to clear the screen whilst editing a command (without having to press <esc> first).

WRT .bashrc:
You can edit .bashrc with ANY text editor you like. But the edits specified in my post will only set up a new key-bind for readlines vi-input mode.

Readline doesn't have a nano input mode. It only has emacs or vim input modes. But if you really wanted to, you could create custom keybinds for any of readlines emacs or vi input modes, that will mimic keybinds/functionality from nano or any other editor.

But, many of nano's keyboard shortcuts are similar to the emacs shortcuts anyway.
e.g.
ctrl+n = next line,
ctrl+p = previous line,
ctrl+f = next character,
ctrl+b = previous character
etc.

So for a nano user, the emacs input method for readline would probably meet most, if not all of your needs anyway!

I hope I explained all of that clearly enough.
If you have any further questions, fire away!

For reference:
Here is the list of the default keyboard shortcuts for Bash - including the emacs-style "cursor movement" and "editing" keybinds:
https://gist.github.com/michaelcoyote/745d8a6cf293c4bf7e31

Switching to the vi mode would change all of the keybinds in the "cursor movement" and "editing" sections to be more vi-like. The other commands (history, process control etc) are mostly the same.

Here are the default keybinds for vi-mode:
http://www.catonmat.net/download/bash-vi-editing-mode-cheat-sheet.pdf

See also:
http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/
 
Last edited:
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)
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)
 
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

Thanks its a fastest way Ctrl+l
 
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)
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)
i can programing with c++ python php and arduino so you can laern me more
 
For now I'm still content with typing the "Clear" command, taking baby steps, just an old dog learning new tricks.
 
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)
I've been using linux and the command line since the late 1990s, and I never knew about the Ctrl-l. Thank you! :)
 


Top