10

Is there some way clear the terminal but instead of leaving the prompt at the top of the screen leaves it in the middle? It looks like clear basically ignores all command-line parameters.

I thought there would be some way to do this with tput but can't find one.

12

You could use tput to move the cursor to a given line in the screen, e.g.,

tput cup 11 0

to move it to the twelfth line (values count from zero).

Along the same lines, you could use tput to clear from that position to the end of the screen, using the ed capability. Combining,

tput cup 11 0 && tput ed

might be what was wanted.

If you want to go to the halfway mark on the screen, the first number returned by

stty size

is (on most systems) the number of rows of the screen. Adding that to the command:

tput cup $(stty size|awk '{print int($1/2);}') 0 && tput ed

The clear program differs from tput ed:

  • it moves the cursor to the home position (upper left) and
  • clears from that point to the end of the screen.
0

If the line count is odd then tput cup clears the whole page.
writing it like this should fix that:

tput cup $(($(stty size|awk '{print $1}')/2)) 0 && tput ed
-1

If you're interested in clearing the bottom half of the screen while maintaining the rest, then this will work:

half=$(stty size | awk '{print int($1/2)-1;}'); for i in `seq ${half}`; do echo '' ; done && tput cup ${half} 0 && tput ed

EDIT: To clarify, if you run seq $(stty size | cut -d' ' -f1) the other answers will leave the first half of the output, while this command will leave the second half (i.e. the most recent output).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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