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.


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.

Caveat: on some platforms tput ed may not work due to problems fixed long ago. In those cases, upgrading your curses/ncurses configuration will fix the problem.

  • on unix, this doesn't clear the text for me, but does move the cursor – James Owers Jan 24 '20 at 18:50
  • 1
    Sounds like a bug fixed ten years ago. Perhaps time to upgrade things. – Thomas Dickey Jan 25 '20 at 1:32

Refering answer

# Get ceiling eg: 7/2 = 4
ceiling_divide() {

clear_rows() {
  # Insert Empty Rows to push & preserve the content of screen
  for i in {1..$((LINES-POS-1))}; echo
  # Move to POS, after clearing content from POS to end of screen
  tput cup $((POS-1)) 0

# Clear quarter
alias ptop='ceiling_divide $LINES 4; clear_rows $ceiling_result'
# Clear half
alias pmid='ceiling_divide $LINES 2; clear_rows $ceiling_result'
# Clear 3/4th
alias pdown='ceiling_divide $((3*LINES)) 4; clear_rows $ceiling_result'

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

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.