如何清除/删除终端中的当前行?

时间:2012-03-13 07:17:31

标签: bash terminal

如果我正在使用终端并输入一行命令文本,是否有热键或任何方式清除/删除该行?

例如,如果我当前的行/命令非常像:

> git log --graph --all --blah..uh oh i want to cancel and clear this line <cursor is here now>

是否有热键或命令从上面开始:

>

通常我会按键,如果我的当前行是历史上的全新行,那将清除它。但是,如果我通过键浏览我的命令历史记录并开始编辑或使用这些命令,只会将提示更改为历史记录中的下一个最新命令,所以除非我多次按,否则它不起作用。

14 个答案:

答案 0 :(得分:1227)

您可以使用 Ctrl + U 来清除开头。

您可以使用 Ctrl + W 删除一个单词。

您也可以使用 Ctrl + C 取消。

如果您想保留历史记录,可以使用 Alt + Shift + 对其进行评论。


<子> Bash Emacs Editing Mode Cheat Sheet

答案 1 :(得分:497)

总结所有答案:

  • 清理该行:您可以使用 Ctrl + U 来清除行尾。
  • 清理该行: Ctrl + E Ctrl + U 擦除终端中的当前行
  • 清理行: Ctrl + A Ctrl + K 擦除终端中的当前行
  • 取消当前命令/行: Ctrl + C
  • 召回已删除的命令: Ctrl + Y (然后 Alt + Y
  • 转到行首: Ctrl + A
  • 转到行尾: Ctrl + E
  • 删除前进单词,例如,如果您在命令的中间位置: Ctrl + K
  • 删除左侧的字符,直到单词的开头: Ctrl + W
  • 清除整个命令提示符: Ctrl + L
  • 在行首和当前光标位置之间切换: Ctrl + XX

答案 2 :(得分:129)

我有完整的快捷键列表:

  1. Ctrl + a 将光标移动到行首
  2. Ctrl + e 将光标移动到行尾
  3. Ctrl + b 向后移动一个字符
  4. Alt + b 向后移动一个单词
  5. Ctrl + f 向前移动一个字符
  6. Alt + f 前进一个单词
  7. Ctrl + d 删除当前字符
  8. Ctrl + w 剪切最后一个字
  9. Ctrl + k 剪切光标后的所有内容
  10. Alt + d 在光标后剪切字
  11. Alt + w 在光标前剪切字
  12. Ctrl + y 粘贴上次删除的命令
  13. Ctrl + _ 撤消
  14. Ctrl + u 剪切光标前的所有内容
  15. Ctrl + x x 在第一个和当前位置之间切换
  16. Ctrl + l 清除终端
  17. Ctrl + c 取消命令
  18. Ctrl + r 历史记录中的搜索命令 - 键入搜索词
  19. Ctrl + j 结束当前历史记录条目的搜索
  20. Ctrl + g 取消搜索并恢复原始行
  21. Ctrl + n 历史记录中的下一个命令
  22. Ctrl + p 历史记录中的上一个命令

答案 3 :(得分:22)

Ctrl + A Ctrl + K 擦除终端中的当前行。然后,如果需要,可以使用 Ctrl + Y 调用它。

答案 4 :(得分:15)

或者如果你使用vi模式,点击 Esc 后跟 cc

取回刚刚删除的内容, Esc 然后 p :)

答案 5 :(得分:10)

替代 Ctrl + A Ctrl + K Ctrl + E Ctrl + U

答案 6 :(得分:9)

我不确定你是否喜欢它,但我使用 Ctrl + A (开始行)和 Ctrl + < kbd> K (删除行)我熟悉emacs中的这些命令,并意外地弄清楚了这些命令。

答案 7 :(得分:6)

  • Ctrl + u 移动直到行的开头到环形缓冲区
  • Ctrl + k 移动直到行尾到环形缓冲区
  • Ctrl + w 移动字符和从光标留下的(多个)字到环形缓冲区

  • Ctrl + y :从环形缓冲区插入最后一个条目然后你可以使用 Alt + y < / kbd>通过环形缓冲区旋转。按多次以继续环形缓冲区中的“上一个”条目。

答案 8 :(得分:3)

CTRL + R 并开始输入以搜索历史记录中的先前命令。将显示全线。
CTRL + R 再次循环。

答案 9 :(得分:2)

Ctrl + W 将清除左侧的单词。

答案 10 :(得分:2)

要删除整行,无论光标在哪里,都可以使用kill-whole-line命令,但默认情况下它是未绑定的。例如,它可以通过插入

绑定到 Ctrl + Alt + K
"\e\C-k": kill-whole-line

进入您的Readline初始文件(通常为~/.inputrc)。

各种评论:

  • 为避免意外重新分配已用于其他内容的键序列,您可以使用bind -P检查所有绑定。使用

    检查建议的绑定
    bind -P | grep '\\e\\C-k'
    
  • Readline init文件名取自shell变量INPUTRC。如果未设置,则默认值为~/.inputrc,或者(如果不存在)/etc/inputrc。请注意,如果您有~/.inputrc,则会忽略/etc/inputrc
  • 要重新加载Readline初始化文件,您可以使用 Ctrl + X Ctrl + R
  • 相关手册部分的链接:

答案 11 :(得分:2)

为了清洁整条线(两种不同方式):

  • Home Ctrl + K
  • End Ctrl + U

答案 12 :(得分:1)

另一个不错的完整列表:

TERINAL Shortcuts Lists:

Left            Move back one character
Right           Move forward one character
Ctrl+b          Move back one character
Ctrl+f          Move forward one character

Alt+Left        Move back one word
Alt+Right       Move forward one word
Alt+b           Move back one word
Alt+f           Move forward one word

Cmd+Left        Move cursor to start of line
Cmd+Right       Move cursor to end of line
Ctrl+a          Move cursor to start of line
Ctrl+e          Move cursor to end of line

Ctrl+d          Delete character after cursor
Backspace       Delete character before cursor

Alt+Backspace   Delete word before cursor
Ctrl+w          Delete word before cursor
Alt+w           Delete word before the cursor
Alt+d           Delete word after the cursor

Cmd+Backspace   Delete everything before the cursor
Ctrl+u          Delete everything before the cursor
Ctrl+k          Delete everything after the cursor

Ctrl+l          Clear the terminal

Ctrl+c          Cancel the command
Ctrl+y          Paste the last deleted command
Ctrl+_          Undo

Ctrl+r          Search command in history - type the search term
Ctrl+j          End the search at current history entry and run command
Ctrl+g          Cancel the search and restore original line

Up              previous command from the History
Down            Next command from the History
Ctrl+n          Next command from the History
Ctrl+p          previous command from the History

Ctrl+xx         Toggle between first and current position

答案 13 :(得分:0)

Alt + 注释掉当前行。如果需要,它将在历史记录中可用。