In bash, how does one clear the current input?

前端 未结 11 1949
礼貌的吻别
礼貌的吻别 2020-12-04 05:56

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

相关标签:
11条回答
  • 2020-12-04 06:00

    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, ...)

    0 讨论(0)
  • 2020-12-04 06:01

    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
    0 讨论(0)
  • 2020-12-04 06:06

    There are two options to do this

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

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

    0 讨论(0)
  • 2020-12-04 06:11

    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.

    0 讨论(0)
  • 2020-12-04 06:12

    Try Ctrl+U. That clears the input line.

    0 讨论(0)
  • 2020-12-04 06:15

    Ctrl-U, Ctrl-K does the trick as well.

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

    0 讨论(0)
提交回复
热议问题