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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9679776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:46:25  来源:igfitidea点击:

How do I clear/delete the current line in terminal?

bashterminal

提问by triad

If I'm using terminal and typing in a line of text for a command, is there a hotkey or any way to clear/delete that line?

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

For example, if my current line/command is something really long like:

例如,如果我当前的行/命令很长,例如:

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

Is there a hotkey or command to go from the above to:

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

>

?

?

Usually I will press the key, and if my current line is a brand new one on the history, that will clear it. But if I'm going through my command history via the key and start editing or using those commands, will only change the prompt to the next newest command in history, so it doesn't work here unless I press multiple times.

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

回答by kev

You can use Ctrl+Uto clear up to the beginning.

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

You can use Ctrl+Wto delete just a word.

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

You can also use Ctrl+Cto cancel.

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

If you want to keep the history, you can use Alt+Shift+#to make it a comment.

如果你想保留历史记录,您可以使用Alt+ Shift+ #,使之成为注释。



Bash Emacs Editing Mode Cheat Sheet

Bash Emacs 编辑模式备忘单

回答by J4cK

Just to summarise all the answers:

总结一下所有的答案:

  • Clean up the line: You can use Ctrl+Uto clear up to the beginning.
  • Clean up the line: Ctrl+ECtrl+Uto wipe the current line in the terminal
  • Clean up the line: Ctrl+ACtrl+Kto wipe the current line in the terminal
  • Cancel the current command/line: Ctrl+C.
  • Recall the deleted command: Ctrl+Y(then Alt+Y)
  • Go to beginning of the line: Ctrl+A
  • Go to end of the line: Ctrl+E
  • Remove the forward words for example, if you are middle of the command: Ctrl+K
  • Remove characters on the left, until the beginning of the word: Ctrl+W
  • To clear your entire command prompt: Ctrl+ L
  • Toggle between the start of line and current cursor position: Ctrl+ XX
  • 清理行:您可以使用Ctrl+U清理到开头。
  • 清理线:Ctrl+ ECtrl+U擦拭当前行终端
  • 清理线:Ctrl+ ACtrl+K擦拭当前行终端
  • 取消当前的命令行/行:Ctrl+ C
  • 调用删除的命令:Ctrl+ Y(然后是Alt+ Y
  • 转到行首:Ctrl+A
  • 转到行尾:Ctrl+E
  • 例如,如果您在命令中间,则删除前向词:Ctrl+K
  • 删除左边的字符,直到单词的开头:Ctrl+W
  • 清除整个命令提示符:Ctrl+L
  • 在行首和当前光标位置之间切换:Ctrl+XX

回答by tharunkumar

I have the complete shortcuts list:

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

  1. Ctrl+aMove cursor to start of line
  2. Ctrl+eMove cursor to end of line
  3. Ctrl+bMove back one character
  4. Alt+bMove back one word
  5. Ctrl+fMove forward one character
  6. Alt+fMove forward one word
  7. Ctrl+dDelete current character
  8. Ctrl+wCut the last word
  9. Ctrl+kCut everything after the cursor
  10. Alt+dCut word after the cursor
  11. Alt+wCut word before the cursor
  12. Ctrl+yPaste the last deleted command
  13. Ctrl+_Undo
  14. Ctrl+uCut everything before the cursor
  15. Ctrl+xxToggle between first and current position
  16. Ctrl+lClear the terminal
  17. Ctrl+cCancel the command
  18. Ctrl+rSearch command in history - type the search term
  19. Ctrl+jEnd the search at current history entry
  20. Ctrl+gCancel the search and restore original line
  21. Ctrl+nNext command from the History
  22. Ctrl+pprevious command from the History
  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历史记录中的上一个命令

回答by Spyros Mandekis

Ctrl+A, Ctrl+Kto wipe the current line in the terminal. You can then recall it with Ctrl+Yif you need.

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

回答by bluTaz

or if your using vi mode, hit Escfollowed by cc

或者,如果你使用vi模式,命中Esc之后cc

to get back what you just erased, Escand then p:)

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

回答by Muhammet Can

I'm not sure if you love it but I use Ctrl+A(to go beginning the line) and Ctrl+K(to delete the line) I was familiar with these commands from emacs, and figured out them accidently.

我不确定你是否喜欢它,但我使用Ctrl+ A(开始行)和Ctrl+ K(删除行)我熟悉 emacs 中的这些命令,并且偶然发现了它们。

回答by Andrey Starodubtsev

An alternative to Ctrl+A, Ctrl+Kis Ctrl+E, Ctrl+U.

Ctrl+ A, Ctrl+的替代方法KCtrl+ E, Ctrl+ U

回答by Cyrus

  • Ctrl+u: moveup to the beginning of your line to a ring buffer
  • Ctrl+k: moveup to the end of your line to a ring buffer
  • Ctrl+w: movecharacters and (multiple) words left from your cursor to a ring buffer

  • Ctrl+y: insert last entry from your ring buffer and then you can use Alt+yto rotate through your ring buffer. Press multiple times to continue to "previous" entry in ring buffer.

  • Ctrl+ u向上移动到行首到环形缓冲区
  • Ctrl+ k向上移动到行尾到环形缓冲区
  • Ctrl+ w:光标左侧的字符和(多个)单词移动到环形缓冲区

  • Ctrl+ y:从您的环形缓冲区插入最后一个条目,然后您可以使用Alt+y来旋转您的环形缓冲区。多次按下以继续到环形缓冲区中的“上一个”条目。

回答by Daniel

CTRL+Rand start typing to search for previous commands in history. Will show full lines.
CTRL+Ragain to cycle.

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

回答by Benjamin W.

To delete the whole line no matter where the cursor is, you can use the kill-whole-linecommand, but it is unbound by default. It can be bound to, for example, Ctrl+Alt+Kby inserting

无论光标在哪里,都可以删除整行,可以使用该kill-whole-line命令,但默认是未绑定的。它可以,例如,被绑定Ctrl+ Alt+K由插入

"\e\C-k": kill-whole-line

into your Readline init file (conventionally ~/.inputrc).

进入你的 Readline init 文件(通常是~/.inputrc)。

Various remarks:

各种备注:

  • To avoid accidentally re-assigning a key sequence that is already in use for something else, you can check all your bindings with bind -P. Check for the suggested binding with

    bind -P | grep '\e\C-k'
    
  • The Readline init file name is is taken from the shell variable INPUTRC. If it is unset, the default is ~/.inputrc, or (if that doesn't exist) /etc/inputrc. Notice that if you have ~/.inputrc, /etc/inputrcwill be ignored.
  • To reload your Readline init file, you can use Ctrl+XCtrl+R.
  • Links to relevant manual sections:
  • 为避免意外地重新分配已用于其他用途的键序列,您可以使用bind -P. 检查建议的绑定

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