In bash, how does one clear the current input?

2019-01-12 15:47发布

Suppose in bash you start writing a command like:

$ 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?

标签: bash input
11条回答
看我几分像从前
2楼-- · 2019-01-12 16:13

Pressing Esc plus Backspace in bash will clear everything up to the cursor's position.

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

查看更多
贼婆χ
3楼-- · 2019-01-12 16:14

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

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

查看更多
Ridiculous、
4楼-- · 2019-01-12 16:17

Try Ctrl+U. That clears the input line.

查看更多
相关推荐>>
5楼-- · 2019-01-12 16:21

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 :)

查看更多
Summer. ? 凉城
6楼-- · 2019-01-12 16:21

This is an expansion of knittl's answer that 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.

Comment Line & Return New Prompt

Use either key shortcut:

  • 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.

$ #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

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+#

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

  • Home, Delete
  • Ctrl+a, Ctrl+d
查看更多
甜甜的少女心
7楼-- · 2019-01-12 16:22

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

查看更多
登录 后发表回答