Restore Cursor (DECRC)
Restore cursor position and related state previously saved with Save Cursor (DECSC).
- 0x1B
- ESC
- 0x38
- 8
Restores the cursor state previously saved by Save Cursor (DECSC). The following attributes are restored, in this order:
- SGR attributes (bold, colors, underline, etc.)
- Character set state (GL/GR designators and active charset)
- Origin mode (DEC Mode 6)
- Pending wrap state
- Protected attribute
- Cursor row and column (clamped to the current screen size)
If no DECSC has ever been issued on the current screen, DECRC resets all of the above to their default values: the cursor moves to row 1, column 1, SGR is cleared, origin mode is disabled, the pending wrap flag is cleared, the protected attribute is off, and the default character sets are selected.
Note
Primary and alternate screens each have their own saved-cursor slot. A DECSC issued on the primary screen does not survive a switch to the alternate screen and vice versa. DECRC on a screen with no saved state restores defaults rather than the most recent value from the other screen.
The saved cursor position is stored in absolute coordinates. If the terminal is resized between DECSC and DECRC such that the saved position would fall outside the current grid, the restored cursor is clamped to the last valid row and column rather than producing an error.
Tip
DECRC is the counterpart of DECSC (
ESC 7). For applications that need to save and restore only the cursor position (without SGR, charset, or origin mode), use the CSI formCSI s/CSI uinstead.
Most validation is shared with Save Cursor (DECSC), which exercises the round-trip. The cases below cover DECRC-specific behavior.
printf "\033[1;1H" # move to top-left
printf "\033[0J" # clear screen
printf "\033[1;4;33;44m" # set SGR
printf "\033[5;5H" # move away from origin
printf "\0338" # Restore Cursor with no prior DECSC
printf "X"
|Xc________|
The X lands at row 1, column 1 and is rendered with no styling,
because the implicit defaults clear SGR and reset position.
printf "\033[5;5H"
printf "\0337" # DECSC on primary screen
printf "\033[?1049h" # switch to alternate screen
printf "\0338" # DECRC on alternate (no saved state here)
printf "X"
The X is written at row 1, column 1 on the alternate screen, because
the saved state on the primary screen is not visible from the alternate
screen.