Temporarily clear the terminal in Linux
Terminals (such as xterm and "any" which emulate it) support a feature called the alternate screen. Often, terminal descriptions include switching to/from the alternate screen in the smcup
and rmcup
capabilities, and it is used by ncurses applications. (The feature is not always used, because some users do not like the feature). Even if it is not part of the terminal description (seen with tgetstr("ti")
or tigetstr("smcup")
) your application could write the literal escape sequence.
On switching to the alternate screen, the convention (used in the terminal escape sequences) is to clear the alternate screen, putting the cursor at the upper left corner. Switching back restores the original (normal) screen and cursor location.
There is some discussion of alternate screen in the xterm FAQ Why doesn't the screen clear when running vi?.
Temporary clear tty
Use tput. Here's a minimal example:
#!/bin/bash
tput smcup # save the screen
clear # clear the screen
echo this is some text on a blank screen
echo press any button to exit..
read -n1
tput rmcup # reset the screen
Clear last two (and more) printed lines in Linux terminal emulator
As user @Marged rightly pointed out, ANSI escape sequences are the answer.
Here is the C code that works for me. Crucial is the line 19 which prints ANSI sequence that erases last two lines (number of lines is arbitrary, it is stated in the sequence):
#include <stdio.h>
#include <unistd.h>
void progressBar(int done, int total) {
int i;
printf("[");
for (i=0; i<total; ++i) {
printf((done < i) ? " " : "=");
}
printf("]\n");
}
int main() {
int i;
for (i=0; i<5; ++i) {
progressBar(i, 4);
progressBar(2*i, 8);
if (i != 4) {
printf("\033[2A"); // This erases last two lines!
sleep(1);
}
}
return 0;
}
How do you temporarily hide the output of a bash script that is running?
I can see two solutions:
- use screen (preferably), or
- use redirection and
tail
.
For example:
$ stdbuf -oL ./start.sh > /tmp/start.sh.log &
$ tail -f /tmp/start.sh.log
After doing this, you can see the log messages in your terminal. You can kill tail
with impunity and start it again whenever you want to see the messages. When the log file gets too big, you can shorten it to zero with:
$ echo -n > /tmp/start.sh.log
Bash's redirect can seamlessly handle the file suddenly getting truncated. Not sure if this is true for all shells. You can interpose tee
if that becomes a problem with your shell.
I added the stdbuf -oL
to “encourage” the shell-script to line-buffer stdout
. Any commands in there that buffer their own output won't be affected, but it might help.
How do I clear/delete the current line in terminal?
You can use Ctrl+U to clear up to the beginning.
You can use Ctrl+W to delete just a word.
You can also use Ctrl+C to cancel.
If you want to keep the history, you can use Alt+Shift+# to make it a comment.
Bash Emacs Editing Mode Cheat Sheet
How to clear the interpreter console?
As you mentioned, you can do a system call:
For Windows:
>>> import os
>>> clear = lambda: os.system('cls')
>>> clear()
For Linux it would be:
>>> import os
>>> clear = lambda: os.system('clear')
>>> clear()
What mechanism allows ViM to temporarily overwrite the entire console?
Most terminal emulators are able to save and restore the contents of the screen.
The terminfo codes for this are smcup
to enter full-screen mode and rmcup
to leave it. (The older termcap codes are ti
and te
.)
If these capabilities are enabled in the terminfo
database, any program that uses ncurses
will print the smcup
string on entry, and the rmcup
string on exit.
On the system I'm using at the moment, the strings are (with \E
representing the Escape character):
smcup: \E7\E[?1;47h
rmcup: \E[2J\E[?1;47l\E8
This restores the previous contents of the screen as well as the cursor position.
The specific meanings of the sequences (for xterm) are documented here:
- smcup:
\E7
Save Cursor\E[?1;47h
Application Cursor Keys; Use Alternate Screen Buffer
- rmcup:
\E[2J
Erase screen\E[?1;47l
Application Cursor Keys; Use Normal Screen Buffer\E8
Restore Cursor
(This assumes I'm understanding the use of the semicolon correctly; I'm not 100% sure of that.)
Related Topics
How to Inject JavaScript in Webbrowser Control
Launching an Application (.Exe) from C#
Conditional Compilation and Framework Targets
Why Does My .Net 4 Application Know .Net 4 Is Not Installed
Differencebetween Debug and Release in Visual Studio
Mod of Negative Number Is Melting My Brain
What Is the Correct Way to Use JSON.Net to Parse Stream of JSON Objects
ASP.NET MVC: No Parameterless Constructor Defined for This Object
Update Row If It Exists Else Insert Logic with Entity Framework
Embedding JavaScript Engine into .Net
Serialize Class Containing Dictionary Member
Differences in String Compare Methods in C#
C# Splitting Strings on '#' Character
Entity Framework 4 - Addobject VS Attach
How to Correctly Implement a Backgroundworker with Progressbar Updates