7

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

  • 3
    You might want to read 'tput reset' (cls-like command) won't work – DavidPostill Dec 29 '16 at 21:11
  • Are you looking? if my answer doesn't work, please tell me, it would be nice to know if my solution worked – DanHolli Feb 8 '17 at 17:14
  • @DanHolli Sorry I never tried your solution, I was leery of making a batch file that appears to be an infinite loop. – William Beaumont Feb 9 '17 at 17:47
  • It is an infinite loop, but you can still use alt+f4 and the x on the window to close it – DanHolli Feb 9 '17 at 19:49
5

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.

  • It's Oct. 2018 and that doesn't work for me (or I don't understand something; I'm on "bash" of WSL, with Ubuntu, and want the scrollback cleared). Was this broken again? – Sebastien Diot Oct 24 '18 at 10:53
  • 1
    @SebastienDiot Strange, it's still working for me. Anyway, in the meanwhile I've found that this command isn't as universal as I'd like to: if I use the Bitvise SSH client to connect to a Debian machine it doesn't work. On the other hand I've found printf '\033c\e[3J' and echo -en '\033c\033[3J' to work everywhere I've tested them. Can you try them and let me have your feedback? In case they work I can update my answer. Thank you! – Fabio Turati Oct 24 '18 at 12:00
  • The echo version did not work for me, but the printf version did. Thanks! – Sebastien Diot Oct 24 '18 at 14:53
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.

0

You can create a batch file to do this when you enter exit in Bash

@echo off
powershell while (1) {bash ~; cls}

Save the file as a .bat file and run it, to exit it close the window or press Alt+F4

I also made the .bat file and converted it to an .exe file, download is available on my Google Drive here.

I have tested it about 4 times now, works flawlessly.

If the download doesn't work, please tell me.

You can change the link in the startmenu to this bat or exe file by editing the properties in C:\Users\usernamehere\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

  • I haven't tried this, I just switched to using MinTTY on WSL (github.com/mintty/wsltty) instead. – William Beaumont Feb 9 '17 at 17:41
  • could you try it, I had to learn a lot more in Powershell, and If it works well for your needs, please upvote – DanHolli Feb 9 '17 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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