1

How do I make a cls-type command for Ubuntu/Linux that will not only clear the terminal emulator screen, but also make it so you can't scroll up to see the stuff you cleared? Note that the clear command does not eliminate the text. It just scrolls it up out of view. Also, any information about setting it up as a system-wide command, called cls would be helpful (especially nice for Windows users coming to Linux).

  • Ah, I was actually just asking it to post an answer similar to user207900's. It's a very, very similar question (and I didn't know of the other question when I asked or answered it), but my question did ask about creating a script to do it (rather than just a command-line command). I plan to update my answer with an explanation of update-alternatives. I'll update the question a little, too. – Brōtsyorfuzthrāx Sep 12 '14 at 10:20
  • 1
    Even if we close this as a duplicate, your answer can be merged into the master question. So don't worry--and by all means, if you wish to continue expanding/improving your answer, please do so! (Btw, welcome to Ask Ubuntu!) – Eliah Kagan Sep 12 '14 at 10:22
  • Thanks! :) I added the updates I referenced (and upvoted user207900's answer to help it gain more attention). – Brōtsyorfuzthrāx Sep 12 '14 at 10:35
1

As already mentioned by @user2962794, The command to clear the terminal is issuing the escape code \033c. This is the VT-100 ANSI escape sequence for resetting the terminal to its initial state (RIS, see here) and is an ASCII "Escape" character (\033) followed by a c.

Therefore, to clear your terminal, all you need is one of

printf printf "\033c"

or

echo -ne "\033c"

To make that simpler, you can create an alias for it. Add this line to your ~/.bashrc:

alias cls="printf '\033c'"

Now, open a new terminal and running cls will clear it. Note that if there already is a command named cls, this will supersede it. Make sure you choose a name that is unique to avoid this.

| improve this answer | |
  • Is there a system-wide (all users) version of .nashrc? – Brōtsyorfuzthrāx Sep 14 '14 at 5:33
  • 1
    @user2962794 first of all, that was a typo, it should have been .bashrc not .nashrc (I've now corrected it). Anyway, the system-wide one is /etc/bash.bashrc for all users whose shell is bash. – terdon Sep 16 '14 at 13:07
2

Make a script, name it cls, make it executable (chmod +x cls), and give it these contents:

#!/bin/bash

printf "\033c"

Before you add it to your path, you may want to make sure there are no programs called cls already:

update-alternatives --config cls

Then, to add it to your path system-wide, put your script in a safe system location, such as /opt/bin/cls and use update-alternatives to add it to the path for all users:

sudo update-alternatives --install /usr/bin/cls cls /opt/bin/cls 0

You can place your script in ~/bin (the tilde stands for your home directory) for it to be in a single user's path (if you had to create the bin directory, you'll need to restart your computer before it will be in your path. There's no need to do anything with update-alternatives in this case.

| improve this answer | |
  • 1
    Will update-alternatives --config cls effectively check for other clses if they're present but don't use the alternatives system? – Eliah Kagan Sep 13 '14 at 6:02
  • I don't believe it would. I know it won't check the contents of ~/bin. – Brōtsyorfuzthrāx Sep 13 '14 at 10:30
  • I don't see the point of the update-alternatives approach. If there is another cls command in the OP's path, there is no reason to expect it to be in the alternatives system. – terdon Sep 13 '14 at 12:14

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