Search code examples
bashansi-escapecursor-position

Can I actually save several cursor positions using ANSI escape sequences?


According to this source, these are the operations that can be done about the cursor :

- Position the Cursor:
  \033[<L>;<C>H
     Or
  \033[<L>;<C>f
  puts the cursor at line L and column C.
- Move the cursor up N lines:
  \033[<N>A
- Move the cursor down N lines:
  \033[<N>B
- Move the cursor forward N columns:
  \033[<N>C
- Move the cursor backward N columns:
  \033[<N>D

- Clear the screen, move to (0,0):
  \033[2J
- Erase to end of line:
  \033[K

- Save cursor position:
  \033[s
- Restore cursor position:
  \033[u

So you can save the cursor position, using \033[s, and then restore it using \033[u. But what if I want to save several cursor positions?

For example, let's say I want to save two cursor positions, and then restoring them. Values will get erased right? So my question is : Is there a way, using ANSI escaped sequences or not, to save several cursor positions to restore them later in bash?


Solution

  • The ANSI terminal does not have a memory of cursor positions. If you need to anything complicated like that, you will have to keep track of the cursor position yourself.

    That's a lot of work and tricky to get right. You're much better off using ncurses.