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

tri*_*iad 723 bash terminal

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

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

> git log --graph --all --blah..uh oh i want to cancel and clear this line <cursor is here now>
Run Code Online (Sandbox Code Playgroud)

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

>
Run Code Online (Sandbox Code Playgroud)

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

kev*_*kev 1346

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

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

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

如果要保留历史记录,可以使用Alt+ Shift+ #将其作为注释.


Bash Emacs编辑模式备忘单

  • Ctrl-U删除的行也可以使用Ctrl-Y重新调整. (211认同)
  • 武汉理工大学?非常感谢keks&kev,^ + U在Cursor之前全部删除,^ + K在Cursor之后全部删除,^ + A是要去行的开头.所以从技术上讲,^ + C是问题的最佳解决方案 (57认同)
  • Upvoted用于教我`<kbd> ... </ kbd>`标签以显示SO中的键. (47认同)
  • 为什么不是一个简单的ESC喜欢windows'命令提示符? (12认同)
  • @LưuVĩnhPhúc`ESC`是bash中自己的修饰键.它用于取消历史导航(`CTRL` +`r`),或交换光标后面的最后两个单词(`ESC` +`t`). (11认同)
  • Alt-Shift-#不起作用?认为其他人这样做.Alt-Shift-#在终端上打印>字符 (3认同)
  • `U` /`A`和`Y`更像是一种剪贴.尝试多次执行`Ctrl` +`Y`. (2认同)

J4c*_*4cK 537

只是总结一下所有的答案:

  • 清理线:您可以使用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

  • 注意ctrl-k删除光标和行尾之间的所有内容. (5认同)
  • 删除下一个单词:`Alt + D` (5认同)
  • `CTRL + L` 清除屏幕,但保留当前命令行中输入的内容。 (4认同)
  • `Alt` + `D` 不起作用,这就是为什么我没有包含它:( (2认同)

tha*_*mar 142

我有完整的快捷方式列表:

  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+ xx在第一个和当前位置之间切换
  16. Ctrl+ l 清除终端
  17. Ctrl+ c 取消命令
  18. Ctrl+ r 历史记录中的搜索命令 - 键入搜索词
  19. Ctrl+ j 结束当前历史记录条目的搜索
  20. Ctrl+ g 取消搜索并恢复原始行
  21. Ctrl+ n 历史记录中的下一个命令
  22. Ctrl+ p 历史记录中的上一个命令

  • 对不起,但我提供了所有有用的快捷键,其中7,8,9,10,11,14,16,17是我们可以根据要求使用的清除/删除快捷键. (4认同)
  • 缺少`Ctrl`+`h`:删除光标前的字符 (3认同)
  • 应该提到的是,`CTRL` +`L`(16.)保留当前命令行的内容. (2认同)

Spy*_*kis 22

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


小智 16

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

找回你刚刚删除的东西,Esc然后p:)

  • 您可以通过`echo'set editing-mode vi'>>〜/ .inputrc`启用vi模式.也适用于python解释器提示和一些SQL客户端 (4认同)

ben*_*elo 12

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


Muh*_*Can 10

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


And*_*sev 10

Ctrl+ A,Ctrl+ 的替代KCtrl+ E,Ctrl+ U.


Akh*_*rou 9

另一个不错的完整列表:

TERMINAL 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
Run Code Online (Sandbox Code Playgroud)


Ben*_* W. 7

无论光标在哪里都可以删除整行,可以使用该kill-whole-line命令,但默认是未绑定的。例如,可以通过插入将其绑定到Ctrl++AltK

"\e\C-k": kill-whole-line
Run Code Online (Sandbox Code Playgroud)

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

各种备注:


Cyr*_*rus 6

  • Ctrl+ u:向上移动到行的开头到环形缓冲区
  • Ctrl+ k:向上移动到行的末尾到环形缓冲区
  • Ctrl+ w:光标中剩余的字符和(多个)字移动到环形缓冲区

  • Ctrl+ y:从环形缓冲区插入最后一个条目,然后可以使用Alt+ y在环形缓冲区中旋转.按多次以继续环形缓冲区中的"上一个"条目.