How to Overwrite/Print Over the Current Line in Windows Command Line

How to replace already-printed text in the command prompt?

In most consoles, writing a bare carriage return \r without a newline after it will return the cursor to the beginning of the current line, allowing you to overwrite the existing text. Writing the backspace character \b also moves the cursor back one character.

For simple behavior, such as a progress indicator, this is all you need. For more complex behavior, you need to control the terminal through non-standard means. On Unix-based systems, the ncurses library can be used—it gives you full control over the cursor location, text color, keyboard echoing, more fine-grained keyboard input, and more.

On Windows, there's a suite of functions for manipulating consoles, and they can do mostly the same things as Unix consoles.

print to screen from c console application overwriting current line

To achieve this, you need to access the terminal. The easiest way to do this is with a library like ncurses. There seems to be a version that supports Windows as well.

With ncurses, you can give the coordinates for the string to output, like this:

mvprintw(row, col, "%s", text);

Output to the same line overwriting previous output?

Here's code for Python 3.x:

print(os.path.getsize(file_name)/1024+'KB / '+size+' KB downloaded!', end='\r')

The end= keyword is what does the work here -- by default, print() ends in a newline (\n) character, but this can be replaced with a different string. In this case, ending the line with a carriage return instead returns the cursor to the start of the current line. Thus, there's no need to import the sys module for this sort of simple usage. print() actually has a number of keyword arguments which can be used to greatly simplify code.

To use the same code on Python 2.6+, put the following line at the top of the file:

from __future__ import print_function

How can I overwrite the same portion of the console in a Windows native C++ console app, without using a 3rd Party library?

You can use SetConsoleCursorPosition. You'll need to call GetStdHandle to get a handle to the output buffer.

How can I update the current line in a C# Windows Console App?

If you print only "\r" to the console the cursor goes back to the beginning of the current line and then you can rewrite it. This should do the trick:

for(int i = 0; i < 100; ++i)
{
Console.Write("\r{0}% ", i);
}

Notice the few spaces after the number to make sure that whatever was there before is erased.

Also notice the use of Write() instead of WriteLine() since you don't want to add an "\n" at the end of the line.

python overwrite previous line

Prefix your output with carriage return symbol '\r' and do not end it with line feed symbol '\n'. This will place cursor at the beginning of the current line, so output will overwrite previous its content. Pad it with some trailing blank space to guarantee overwrite. E.g.

sys.stdout.write('\r' + str(hpi) + ' ' * 20)
sys.stdout.flush() # important

Output the final value as usual with print.

I believe this should work both in most *nix terminal emulators and Windows console. YMMV, but this is the simplest way.

How to overwrite the current console line after gets in Ruby?

Overwriting current line (printing "\r") works just fine with gets. The thing is, gets reads a line until (and including) a linebreak. So it is you, pressing ENTER, who moves cursor to a next line. And then this next, already empty, line is rewinded by \r.

Moving to a previous line is not possible in the regular mode. (see comments) You need to use a lower-level terminal window access. curses is a popular library. Ruby has bindings for it. I suggest you start with this blog post (and follow-ups to it): http://graysoftinc.com/terminal-tricks/random-access-terminal



Related Topics



Leave a reply



Submit