Bash on Ubuntu on Windows: Clearing all the text, ALL of it
Recently I switched from a Mac to a Windows 10 PC and am experimenting with Microsoft's new Linux subsystem (aka, "Bash on Ubuntu on Windows"). The biggest irritation I suffer from using it on a daily basis is clearing a big wall of text. Maybe others can live with their screens cluttered with text, but it drives me crazy. I can only deal with commands that leave big dumps of text one at a time, meaning that I prefer simply being able to scroll to the top to see where all the output of the last command begins, and then when I'm done with it I clear it and move on to the next command. I don't like having to search through multiple command output dumps to find where the last executed command's output begins. Yeah, I know it's not that hard, I guess I'm spoiled from using the Mac OS X Terminal app where I could enter Ctrl+K to clear everything instantly.
On the Windows Bash/Ubuntu application. the nearest thing to clearing text is Ctrl+L, but it only clears the currently visible screen of text, it does not clear the entire scrollback. Using the "reset" command alone doesn't work. I have to scroll to the top, do Ctrl+L, then enter "reset" to get rid of all the text. That's annoying enough, but on top of that, the "reset" command always makes the window narrower than my preferences, setting it to be 80 columns wide. My preferences, as set by clicking the application icon and then "Properties", are for 145 columns of width. I even tried going into "Defaults" and setting it to 145, but "reset" still makes it 80. It must be something that's configured in the Ubuntu subsystem somewhere. That width is too narrow for me.
The top-voted answer here suggests using the GNOME terminal as it allows you to map a "reset and clear" shortcut, but the questioner was using full-blown Ubuntu, not the Windows Ubuntu subsystem, so I'm a little skeptical. Would that work on the latter? I may have to see if that's doable. The second answer suggests making a "cls" alias like so:
alias cls="echo -ne '\033c'"
But alas this did not work. I tried running the contents of the alias separately and that didn't do anything either, ditto for replacing "echo -ne" with "printf".
My last hope is the third answer on that page: "set your buffer to 0 lines". I'm guessing that this is a configuration in the Ubuntu subsystem that I have to change, or maybe a config file I have to place in my home directory (I wish they had explained exactly what they meant). In any case, going into the Windows app's "Properties" and messing with the buffer settings didn't help.
As for what terminal program is being used, echoing $TERM gives me "xterm". Oh, and the Ubuntu version is 14.04. (I hear the next major Windows 10 update will give you the ability to upgrade to a newer Ubuntu, but I don't feel like taking the leap into installing the Windows Insider "preview".)
Solution 1:
The most reliable and portable command that I've found1 is:
printf '\033c\e[3J'
Clearly, it's too long and complex to type it every time. The solution is to define an alias, so that it can be run by using a much easier name. My suggestion is to choose cls
, which is the name this command has in cmd shells on Windows.
To set cls
as alias, enter this line:
alias cls="printf '\033c\e[3J'"
This will enable the alias in the current shell. If you want to always have it at your disposal every time you open a new terminal, add that line to your .bashrc
file in your home directory.
1. I was previously suggesting to use clear && echo -en "\e[3J"
, as explained at Stack Overflow, but for some users it doesn't work, so I've updated my answer.
Solution 2:
I don't have an answer for Windows' default Ubuntu terminal. Based on DavidPostill's link, it seems the dev team is aware of the issue so I assume it will be fixed in a future release. In the meantime, I simply switched to using MinTTY for WSL (https://github.com/mintty/wsltty). The "reset" command works perfectly in that terminal, it completely clears all the text output while leaving the window dimensions alone.