【问题标题】:Terminal - clear half screen终端 - 清除半屏
【发布时间】:2015-06-02 15:53:11
【问题描述】:

我知道使用 cmd + K 可以清除整个屏幕缓冲区。 我想知道有没有办法只清除一半的屏幕缓冲区?

【问题讨论】:

    标签: terminal iterm2


    【解决方案1】:

    cmd+K 映射到用于清除可见屏幕(屏幕缓冲区)的菜单项。这不包括回滚(这是一个类似的问题,但不同)。

    iTerm2 的首选项和菜单不显示任何直接方式来清除一半屏幕。

    但是,iTerm2 模拟(如 Terminal.app)大多数 VT100 控制序列。您可以在您的外壳中添加一个绑定(给定您选择的合适键)告诉外壳回显从当前光标位置(a)到屏幕开头或(b)到末尾的控制序列屏幕。由于它支持保存/恢复光标控件,如果您知道大小,您甚至可以准确地清除一半的屏幕,例如,

    • 保存光标位置
    • 跳到屏幕中间
    • 清除(哪一半?)屏幕的一半
    • 恢复光标位置

    因为它会使用你的 shell,所以我称之为 indirect

    如前所述,Terminal.app 实现了大部分 VT100 控制序列。在 ncurses 中,适当的 TERM 将是 nsterm,已知它可以提供 VT100 样式的保存/恢复光标。您可以使用tput从终端数据库中提取scrccup的对应字符串。但是(如果您想擦除到屏幕的开头),您可以提供自己的字符串,因为该特定风格不是 termcap/terminfo 的一部分。

    您可以在 XTerm Control Sequences 中找到相关的 VT100 控制序列:

    ESC 7     Save Cursor (DECSC).
    ESC 8     Restore Cursor (DECRC).
    
    CSI Ps ; Ps H
              Cursor Position [row;column] (default = [1,1]) (CUP).
    
    CSI Ps J  Erase in Display (ED).
                Ps = 0  -> Erase Below (default).
                Ps = 1  -> Erase Above.
                Ps = 2  -> Erase All.
                Ps = 3  -> Erase Saved Lines (xterm).
    

    【讨论】:

      【解决方案2】:

      参考answer

      # 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'
      

      【讨论】:

        猜你喜欢
        • 2012-04-23
        • 1970-01-01
        • 2019-10-29
        • 2011-07-19
        • 2011-01-29
        • 2016-01-31
        • 1970-01-01
        • 1970-01-01
        • 2011-10-01
        相关资源
        最近更新 更多