function::ansi_cursor_restore - Linux


function::ansi_cursor_restore is a shell function that restores the cursor position to its saved location. It’s typically used after executing commands that move the cursor around the screen, such as less or top.


function ansi_cursor_restore() {
  local escape_sequence=$(printf "\033[s")
  printf "%s" "$escape_sequence"


This function does not support any options or flags.


Simple Usage

After running a command that moves the cursor around, run ansi_cursor_restore to return the cursor to its original position.

less index.txt

Complex Usage

Use ansi_cursor_restore in a script to automatically restore the cursor position after executing a series of commands.


# Save cursor position
escape_sequence=$(printf "\033[s")
printf "%s" "$escape_sequence"

# Execute commands
less index.txt

# Restore cursor position
escape_sequence=$(printf "\033[u")
printf "%s" "$escape_sequence"

Common Issues

  • If ansi_cursor_restore doesn’t work as expected, check if the terminal supports ANSI escape sequences.
  • If the terminal doesn’t support ANSI escape sequences, there are other ways to restore the cursor position, such as using the tput command.


Combine ansi_cursor_restore with other commands to automate tasks or improve the user experience. For example:

  • Use ansi_cursor_restore with less to keep the cursor position when exiting the pager.
  • Use ansi_cursor_restore with vi to restore the cursor position after using the :browse command.

Related Commands

  • tput – Print terminal control sequences.
  • less – A pager.
  • top – A process viewer.