Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I know that tput sc saves the current cursor position and tput rc restores it exactly where tput sc was called. The problem is that every time tput sc is called, it overwrites the previous saved position.

Is there a way to save more positions, e.g. tput sc pos1 and tput sc pos2 which can be restored with, say, tput rc pos1 and tput rc pos2 respectively? (The solution need not make use of tput, I mentioned it because it's the only command I know that handles cursor position)

If not, is there a way to at least save the cursor position locally in a function, so that if a function uses tput sc and then calls another function that runs again tput sc, then each function restores its own saved cursor position when invoking tput rc?

Thanks in advance.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You can use the following function to extract current cursor position in a simple array:

extract_current_cursor_position () {
    export $1
    exec < /dev/tty
    oldstty=$(stty -g)
    stty raw -echo min 0
    echo -en "\033[6n" > /dev/tty
    IFS=';' read -r -d R -a pos
    stty $oldstty
    eval "$1[0]=$((${pos[0]:2} - 2))"
    eval "$1[1]=$((${pos[1]} - 1))"
}

(the source of code used in this function was taken and adapted from this answer)

Now, for example, to save current cursor position in pos1, use:

extract_current_cursor_position pos1

To save current cursor position in pos2, use:

extract_current_cursor_position pos2

To see cursor positions saved in pos1 and pos2, you can use:

echo ${pos1[0]} ${pos1[1]}
echo ${pos2[0]} ${pos2[1]}

To move/restore cursor position to pos1, you have to use:

tput cup ${pos1[0]} ${pos1[1]}

To move/restore cursor position to pos2, you have to use:

tput cup ${pos2[0]} ${pos2[1]}
share|improve this answer
    
Works great, thanks! –  AndreasT Oct 26 '13 at 15:47

tput command works through terminal control sequences, listed here: http://sydney.edu.au/engineering/it/~tapted/ansi.html There is a sequence for extracting current position (Query Cursor Position - \e[6n) and looks like it's not present in tput. To extract it use:

stty -echo; echo -n $'\e[6n'; read -d R x; stty echo; echo ${x#??}
30;1

Now you can extract row position saved in $x to some other variable and move your cursor using tput cup later:

$ echo $my_saved_pos
12
$ tput cup $my_saved_pos 0
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.