In bash, how does one clear the current input?

BashInput

Bash Problem Overview


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 Solutions


Solution 1 - Bash

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

    (Optional: Press End or Ctrl-E to jump to the end of the input first.)

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

Solution 2 - Bash

Try Ctrl+U. That clears the input line.

Solution 3 - Bash

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.

Solution 4 - Bash

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

Solution 5 - Bash

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.

Solution 6 - Bash

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

Solution 7 - Bash

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

Solution 8 - Bash

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.

Solution 9 - Bash

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

Solution 10 - Bash

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

Solution 11 - Bash

To delete the current line, try:

Ctrl-X, Ctrl-U

As an alternative you may use:

Esc-D

which requires in ~/.inputrc:

"\ed": kill-whole-line 

see: http://codesnippets.joyent.com/posts/show/1690

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser85509View Question on Stackoverflow
Solution 1 - BashJohn KugelmanView Answer on Stackoverflow
Solution 2 - BashgbarryView Answer on Stackoverflow
Solution 3 - BashRoger PateView Answer on Stackoverflow
Solution 4 - BashrurView Answer on Stackoverflow
Solution 5 - BashvksView Answer on Stackoverflow
Solution 6 - BashNippeyView Answer on Stackoverflow
Solution 7 - BashknittlView Answer on Stackoverflow
Solution 8 - BashmarkisismeView Answer on Stackoverflow
Solution 9 - BashHelmyanoView Answer on Stackoverflow
Solution 10 - BashCasView Answer on Stackoverflow
Solution 11 - BashcarusoView Answer on Stackoverflow