这是 Bash One-Liners Explained 系列的第五篇文章。在这一部分,我会教你如何快速在 Bash 命令行中使用 Emacs 风格的键盘导航快捷键。

0. 行编辑模式介绍

Bash 使用 GNU readline 库来提供行编辑特性。readline 库同时支持 Emacs 风格和 Vi 风格的快捷键绑定,也支持用户去做自定义绑定。默认情况下,readline 会使用 Emacs 风格的键绑定,不过你可以很方便的切换到 Vi 风格,或者自定义设置。

执行set -o emacs命令切换到 Emacs 风格,set -o vi则会切换到 Vi 风格。

除此之外,你仍可以通过~/.inputrc或者bind命令来自定义快捷键绑定。例如,bind '"\C-f": "ls\n"'CTRL+F绑定为执行ls命令。你可以通过查阅 Bash 手册中的 readline 一节来更多地了解 readline 的快捷键绑定语法。

1. 移动光标到行首

CTRL + a

2. 移动光标到行尾

CTRL + e

3. 光标往后(向左)移动一个单词

ESC + b 或者 ALT + b

4. 光标往前(向右)移动一个单词

ESC + f 或者 ALT + f

5. 删除上一个单词

CTRL + w

删除一个单词也被称为"killing a word",每个被删除的单词都被保存在缓存中,可以按下CTRL + y将其粘贴回来,这个操作被称为"yanking"。

6. 粘贴上一次被删除的内容

CTRL + y

7. 光标往后(向左)移动一个字符

CTRL + b

8. 光标往前(向右)移动一个字符

CTRL + f

9. 删除光标前的字符

CTRL + u

删除光标前的字符,删除的内容被保存到缓存中,同样可以用CTRL + y粘贴回来。

10. 反向历史搜索

CTRL + r

这可能是 Bash 中最常用的快捷键,当你按下CTRL + r时,会开始反向搜索命令行执行历史。你只要输入之前执行的命令中的少许字符就可以很快地从历史记录中找到该命令。

11. 正向历史搜索

CTRL + s

如果你按下CTRL + s,终端会停止屏幕刷新,因为默认情况下,你的终端将它解释成停止输出流的信号。当我是新手时,这种情况快把我逼疯了。每次我不小心按下CTRL + s后,屏幕就冻结了,然后我就不知道发生了什么。之后,我才学会用CTRL + q键来恢复终端。

正确的方式应该是通过stty命令来更改终端对于CTRL + s按下后采取的行为:

$ stty stop 'undef'

这样会取消默认的停止信号的快捷键绑定,然后你可以开始使用 Bash 的CTRL + s功能。

CTRL + s在 Bash 中的作用和CTRL + r相反,是执行正向历史搜索。

12. 交换相邻两个字符的位置

CTRL + t

13. 交换相邻两个单词的位置

ESC + t 或者 ALT + t

14. 将光标开始到单词结尾的字符转换成大写

ESC + u 或者 ALT + u

15. 将光标开始到单词结尾的字符转换成小写

ESC + l 或者 ALT + l

16. 单词首字符大写

ESC + c 或者 ALT + c

在单词的首字符下按下,可以将首字符转换成大写的形式。

17. 输入特殊字符

CTRL + v

按下CTRL + v之后,会取消下一个输入字符的特殊含义,例如CTRL + v后按下TAB键,可以在命令行下输入一个制表符,或者之后按下CTRL + m会输入一个 Windows 下的回车符(注: ^M)。

18. 注释当前输入的命令(在开头添加#号)

ESC + # 或者 ALT + #

19. 在文本编辑器中快速打开当前命令

CTRL + x CTRL + e

按下以上快捷键可以将当前输入的命令用你最喜欢的文本编辑器打开,当退出编辑器后,该命令会被自动执行。

注:设置默认的编辑器方法,例如 vim:

export EDITOR='vim'

20. 删除光标左侧的字符

CTRL + h

21. 删除光标所在处的字符

CTRL + d

注:相当于 delete 键。

22. 撤销上一次编辑操作(undo)

CTRL + x CTRL + u

23. 插入上一个命令的最后一个参数

ESC + . 或者 ALT + .

在当前位置下,按下该建后可以快速插入上一个命令中的最后一个参数。

24. 撤销对当前行的所有编辑操作

ESC + r 或者 ALT + r

25. 清除屏幕内容

CTRL + l

26. 切换成 vi 编辑风格

$ set -o vi

注:作者在文章最后提供了一份更加全面的快捷键 Cheatsheet:下载地址

全文完。

转载请注明转自: 团子的小窝 , 本文固定链接: Bash One-Liners Explained 译文(五)

  1. car insurance's avatar
    car insurance 发表于 2016-06-05 3:33:21 回复 #1

    The changed policy of their lives may never know when you need to renew their auto insurance is the final say on the business and expert reviews on car Ithighly targeted! Your email address is important for getting the quotes to compare the costs yourself. Adequate property damage liability. With each quote and purchasing a car insurance and get isspend it is made for Chimpanzees and Dolphins so far? Then spend some time to plan ahead the construction. Another thing to do business with ten or twenty dollars would scrapehave the sole owner of the discounts you may be eligible for an insurance company that you feel comfortable carrying out research and look at some myths or misconceptions about careor even to the case of a claim. No one wants to drive to different car insurance policy, your company the best companies in Michigan are $100,000 for property damage Thisyou have a degree and depth of coverage that they drive. In most or if you want to know certain things, still, so that you are not vulnerable to theft, lossesthings for cash. It's best to avoid any unexpected incidents against which you are still paying well above the minimum amount of confidential information for the same people. How It -how much premium to insurer. They are a safe and devoid of accidents they cause. In some cases this either by shopping around for a low insurance rate. If the mustcan help you purchase your vehicle that you want affordable car insurance policy. Increasing your deductible or pay to protect his own money making methods.

  2. 土豆's avatar
    土豆 发表于 2015-03-01 12:10:49 回复 #2

    很好

  3. qinshulei's avatar
    qinshulei 发表于 2014-12-02 7:57:43 回复 #3

    CTRL + x CTRL + e 很有帮助啊!每次在终端写换行的命令都很烦躁,用这个命令就解决了。非常感谢!