在 bash 中,如何清除当前输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1056394/
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 18:17:41  来源:igfitidea点击:

In bash, how does one clear the current input?

bashinput

提问by user85509

Suppose in bash you start writing a command like:

假设在 bash 中您开始编写如下命令:

$ rm -rf /foo/bar/really/long/path/here

and then realize you don't want to execute this after all. Is there a way to clear the input with one or two keystrokes?

然后意识到您毕竟不想执行此操作。有没有办法通过一两次按键清除输入?

What I have been doing lately is prepending echo and enclosing the input in quotes (Ctrl+A, echo ", Ctrl+E, ") then hitting enter. Is there a faster way?

我最近一直在做的是在 echo 前面加上引号并将输入括起来(Ctrl+A, echo ", Ctrl+E, ")然后按回车键。有没有更快的方法?

回答by John Kugelman

  1. Press Ctrl-Uto delete everything before the cursor. The deleted command will be stored into a buffer. Press Ctrl-Yto paste the deleted command.

    (Optional: Press Endor Ctrl-Eto jump to the end of the input first.)

  2. Alternatively, press Ctrl-Cto abort what you're typing.

  1. Ctrl-U删除光标前的所有内容。删除的命令将存储到缓冲区中。按Ctrl-Y粘贴已删除的命令。

    (可选:按EndCtrl-E先跳到输入的结尾。)

  2. 或者,按Ctrl-C中止您正在输入的内容。

回答by gbarry

Try Ctrl+U. That clears the input line.

试试Ctrl+ U。这清除了输入行。

回答by gbarry

Found a short reference at http://www.ice2o.com/bash_quick_ref.htmlwhile searching.

发现在短参考http://www.ice2o.com/bash_quick_ref.html搜索

ctrl+ e(if not at the end of the line) plus ctrl+ uwill do it.

ctrl+ e(如果不是在行尾)加上ctrl+u就可以了。

回答by rur

Ctrl-U, Ctrl-Kdoes the trick as well.

Ctrl- U, Ctrl-K也可以。

Ctrl-Udeletes everything from the beginning of the line up to the cursor, Ctrl-Kdeletes everything from the cursor to the end of the line. (It is sometimes useful to use only one of them.)

Ctrl-U删除从行首到光标的所有内容,Ctrl-K删除从光标到行尾的所有内容。(有时只使用其中之一很有用。)

回答by vks

There are two options to do this

有两种选择可以做到这一点

ctrl+c- this clears the whole line, no matter where the cursor is.

ctrl+ c- 这会清除整行,无论光标在哪里。

ctrl+u- this clear the line from the position of the cursor until the beginning.

ctrl+ u- 这会清除从光标位置到开头的行。

回答by knittl

A nice shortcut is pressing Esc#. It will prepend a #character (thus making the line a comment) and then press enter. If you then decide that you still the need the command, you still have it in your history :)

一个不错的快捷方式是按Esc#。它将在前面添加一个#字符(从而使该行成为注释),然后按 Enter。如果您随后决定仍然需要该命令,那么您的历史记录中仍然有它:)

回答by Nippey

Pressing Escplus Backspacein bash will clear everything up to the cursor's position.

在 bash 中按Esc加号Backspace将清除光标位置之前的所有内容。

(In Cygwin, this will clear the input up to the next word. Words are separated by spaces, underscores, ...)

(在 Cygwin 中,这将清除输入到下一个单词。单词由空格、下划线、...分隔)

回答by Helmyano

If you are using Bash in vi mode(set it with set -o vi), then press Escto switch to the normal mode of vi, and type ddto delete the current line!

如果你在vi模式下使用Bash (用 设置set -o vi),然后按Esc切换到vi的普通模式,键入dd删除当前行!

回答by Cas

This is an expansion of knittl's answerthat stores the line in the console history by prefixing with a hash. Overcoming drawbacks of the clipboard, such as accidental overwriting or being unable to view the cut line for reference.

这是knittl 答案的扩展,通过在控制台历史记录中添加哈希前缀来存储该行。克服剪贴板的缺点,例如意外覆盖或无法查看剪切线以供参考。

Comment Line & Return New Prompt

评论行并返回新提示

Use either key shortcut:

使用任一快捷键:

  • Esc,#
  • Alt+#
  • Esc,#
  • Alt+#

A hash character #will be prepended to the line, thus turning the whole line into a comment. It will also return a new prompt, as if enter was pressed by the user. e.g.

哈希字符#将被添加到该行,从而将整行变成注释。它还会返回一个新的提示,就好像用户按下了 Enter 键一样。例如

$ #rm -rf /foo/bar/really/long/path/here
$


Retrieve Commented Line

检索注释行

To recover the old line from console history use one of the following shortcuts:

要从控制台历史记录中恢复旧行,请使用以下快捷方式之一:

  • Up
  • Ctrl+p
  • Up
  • Ctrl+p

Repeat key shortcut until the desired line appears.

重复快捷键,直到出现所需的行。



Quick Hash Prefix Removal

快速哈希前缀删除

To remove the line's hash #prefix there are a few different options available:

要删除行的哈希#前缀,有几个不同的选项可用:

Remove first character and immediately execute command:

删除第一个字符并立即执行命令:

  • Esc,1,Esc,#
  • Alt+-, Alt+#
  • Esc, 1, Esc,#
  • Alt+ -, Alt+#

Move cursor to start and remove first character, without executing the command:

移动光标开始和删除第一个字符,不执行命令:

  • Home, Delete
  • Ctrl+a, Ctrl+d
  • Home, Delete
  • Ctrl+ a, Ctrl+d

回答by Cas

Consider that using Ctrl-U(or Ctrl-Eand then Ctrl-U) will store what you clear in a buffer so that you can then paste it later using Ctrl-Y.

考虑使用Ctrl- U(或Ctrl-E然后Ctrl- U)会将您清除的内容存储在缓冲区中,以便您稍后可以使用Ctrl-粘贴它Y