• Uncategorized

About c++ : Linux—moving-the-console-cursor-visual

Question Detail

I’m currently designing a CLI interface for linux, and for various reasons I am not able to use ncurses. I am using exclusively C++ and the Qt framework.

Therefore, in order to have a user-friendly interface, I have to run this getch loop in a separate thread:


Which basically means I have to implement all basic functionalities (such as backspace) by myself. I have already implemented command completion and command history(like when you press tab or uparrow/downarrow in linux), but I can’t figure out how to implement leftarrow/rightarrow (aka seeking through the typeahead).

Normally, I implement it like this: upon every gech which is not equal to -1, I check whether the user has pressed a special key (one that modifies the typeahead somehow). I then clear the stdout using the following function:

void inputobject::clear_line(int nletters)
    QTextStream(stdout) << "\033[2K";

    for(int i = 0; i < nletters;i++){
        QTextStream(stdout) << "\b";


And replace it with something else, effectively simulating the typeahead. For example, in the case of backspace, I would save the command call clear_line, and print the command out again, just with one less letter, behaving exactly as a normal console application would.

My real problem is with the cursor, in the case of left/rightarrow, I need to move the cursor visual in order to be able to indicate where in the text is the user seeking:

Because of the nature of how I rewrite the given stdout line to simulate the typeahead, it does not really matter where the cursor REALLY is, as long as it stays on the same line – it is just the visual that matters. How can I achieve moving the cursor visual on linux?

Question Answer

The answer was provided in the comment by Evilruff:

Cursor Movement

ANSI escape sequences allow you to move the cursor around the screen at will. This is more useful for full screen user interfaces generated by shell scripts, but can also be used in prompts. The movement escape sequences are as follows:

  • Position the Cursor:
    puts the cursor at line L and column C.
  • Move the cursor up N lines:
  • Move the cursor down N lines:
  • Move the cursor forward N columns:
  • Move the cursor backward N columns:

  • Clear the screen, move to (0,0):

  • Erase to end of line:

  • Save cursor position:

  • Restore cursor position:

Not using ncurses and co is a serious limitation.

It is hell to make correct input/output on shell for displaying anything.

The only others real solutions (I can’t think as a solution to reimplement a ncurse-like library) I think of are:

  • making call to dialog (for some example www.linuxjournal.com/article/2807 and for the doc: http://linux.die.net/man/1/dialog)
  • using the framebuffer mecanism with Qt4 (here)

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.