Ubuntu: How do I make a Windows-style cls script for Ubuntu? [duplicate]



Question:

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


Solution: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.


Solution: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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »