从命令行清除一半屏幕

从命令行清除一半屏幕

是否有某种方法清除终端,但不是将提示留在屏幕顶部,而是将其留在中间?看起来clear基本上忽略了所有命令行参数。

我以为会有某种方法可以做到这一点,tput但找不到。

答案1

您可以使用tput将光标移动到屏幕中的给定行,例如,

tput cup 11 0

将其移动到第十二行(值从零开始计数)。

同样,您可以使用tput该功能从该位置清除到屏幕末尾ed。结合起来,

tput cup 11 0 && tput ed

可能就是想要的。

如果您想转到屏幕上的中间标记,则返回的第一个数字

stty size

是(在大多数系统上)屏幕的行数。将其添加到命令中:

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

clear计划不同于tput ed

  • 它将光标移动到位置(左上)和
  • 从该点到屏幕末尾清除。

注意:在某些平台上tput ed由于很久以前解决的问题,可能无法工作。在这些情况下,升级curses/ncurses 配置将解决问题。

答案2

参考回答

# Get ceiling eg: 7/2 = 4
ceiling_divide() {
  ceiling_result=$((($1+$2-1)/$2))
}

clear_rows() {
  POS=$1
  # 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'

答案3

如果行数为奇数,则tput cup清除整个页面。
像这样写应该可以解决这个问题:

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

答案4

如果您有兴趣清除底部屏幕的一半,同时保持其余部分,那么这将起作用:

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

编辑:为了澄清,如果您运行seq $(stty size | cut -d' ' -f1)其他答案将留下输出的前半部分,而此命令将留下后半部分(即最新的输出)。

相关内容