详解并改进Linux终端的clear命令

<h4>clear做了啥?</h4> <p><code>clear</code>做的工作很简单:把待输入的下一行移动到左上角。 <br />所以你看,它根本没有真正对终端的内容进行清除,只不过是将其藏起来了。</p> <h5>如何实现clear</h5> <h6>Linux的终端控制序列</h6> <p>Linux控制台实现了VT102和ECMA-48/ISO 6429/ANSI X3.64终端控制的子集,详见:<a href="http://manpages.ubuntu.com/manpages/quantal/en/man4/console_codes.4.html">console_codes - ubuntu man page</a>。 <br />Linux使用<strong>ANSI escape code</strong>方式来描述其控制序列,详见:<a href="http://en.wikipedia.org/wiki/ANSI_escape_code">ANSI escape code - Wikipedia</a>。 <br />简而言之,使用<code>ESC</code>符号开始的一串字符序列会被解析成控制序列,多数控制序列由<strong>CSI(control sequence introducer)</strong>开始,由<code>ESC [</code>来表示。</p> <h6>使用printf命令实现clear</h6> <p><code>printf</code>支持终端控制序列,其中<code>ESC</code>可以用<code>\e</code>转义符表示。</p> <p><a href="http://manpages.ubuntu.com/manpages/quantal/en/man4/console_codes.4.html">console_codes - ubuntu man page</a>中给出了一个表(节选):</p> <p>Sequences <br />Name <br />Describe</p> <p>ESC @ <br />ICH <br />Insert the indicated # of blank characters.</p> <p>ESC A <br />CUU <br />Move cursor up the indicated # of rows.</p> <p>ESC B <br />CUD <br />Move cursor down the indicated # of rows.</p> <p>ESC C <br />CUF <br />Move cursor right the indicated # of columns.</p> <p>ESC D <br />CUB <br />Move cursor left the indicated # of columns.</p> <p>ESC E <br />CNL <br />Move cursor down the indicated # of rows, to column 1.</p> <p>ESC F <br />CPL <br />Move cursor up the indicated # of rows, to column 1.</p> <p>ESC G <br />CHA <br />Move cursor to indicated column in current row.</p> <p>ESC H <br />CUP <br />Move cursor to the indicated row, column (origin at 1,1).</p> <p>ESC J <br />ED <br />Erase display (default: from cursor to end of display).</p> <p>ESC [ 1 J <br />erase from start to cursor.</p> <p>ESC [ 2 J <br />erase whole display.</p> <p>维基百科中<a href="http://en.wikipedia.org/wiki/ANSI_escape_code">ANSI escape code - Wikipedia</a>中也说到:</p> <blockquote> <p><strong>CSI 2 J</strong> — This clears the screen and, on some devices, locates the cursor to the y,x position 1,1 (upper left corner).</p> </blockquote> <p><code>CSI 2 J</code>或<code>ESC [ 2 J</code>指令会在清除屏幕后将光标移动到第一行第一列(左上角)。 <br />结合<code>printf</code>,得出以下命令:</p> <pre><code>printf &quot;\e[2J&quot;</code></pre>

<p>不知道是不是Linux水土的问题,这个命令执行效果和描述不太一样,其实际效果是将当前行顶出屏幕外(负一行),并打印若干空白行(尼玛)。</p>

<p>为了修正这个问题,我们看到有个指令<code>ESC H</code>,描述如下:</p>

<blockquote> <p>Move cursor to the indicated row, column (origin at 1,1).</p> </blockquote>

<p>于是我们可以借助这个指令来完善我们的功能,修改如下:</p>

<pre><code>printf &quot;\e[H\e[2J&quot;</code></pre>

<p>这下终于完成我们<strike>坑爹</strike>的<code>clear</code>命令。</p>

<h4>难道就只是这样吗?</h4>

<p><code>clear</code>命令这么坑爹Linus Torvalds知道吗?

<br /><img title="" alt="enter image description here" src="http://image15-c.poco.cn/mypoco/myphoto/20140329/13/437946412014032913331296THX.jpg" /></p>

<h5>实现一个真正意义上的clear</h5>

<p>在<a href="http://manpages.ubuntu.com/manpages/quantal/en/man4/console_codes.4.html">console_codes - ubuntu man page</a>中发现了另一个东西:</p>

<p>Sequences <br />Name

<br />Describe</p>

<p>ESC c <br />RIS

<br />Reset</p>

<p>重置你的终端来达到清除内容的效果。</p>

<pre><code>printf &quot;\ec&quot;</code></pre>

<p>真是一个简单又暴力的方法。 <br />推荐写成shell脚本并移动到<em>/usr/bin/</em>目录下。</p>

<hr />

<blockquote> <p>@<a href="http://mrlyc.blogspot.com/">LYC</a>

<br />转载请注明<a href="http://mrlyc.blogspot.com/">出处</a>。</p></blockquote>

转载于:https://my.oschina.net/IMLYC/blog/225316

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值