199

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
user85509
  • 36,612
  • 7
  • 33
  • 26
  • Or, suppose you paste a command, and realize it was the wrong one-liner. Backspace gets really old after about 20 characters! – bgStack15 Mar 15 '17 at 19:12
  • 2
    Ctrl-C, if you want to keep the history refer to http://stackoverflow.com/a/9679852/6521116 – LF00 May 11 '17 at 09:36

11 Answers11

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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 32
    There is a caveat: Ctrl-C will kill the current prompt, start a new prompt, and set the return code to 1. – user716468 Jan 19 '13 at 20:24
  • 17
    Don't get used to that, use Ctrl-U. Ctrl-C is not that bad in bash, but if you have e.g. a mysql client prompt, Ctrl-C will disconnect from the server which is really annoying. – Christian Nov 25 '13 at 10:54
  • 1
    Note that on many systems, there is also a terminal 'kill' character which kills the input so far. Often, that is control-X. You can check with `stty -a`. And that will work with any program that is not actively modifying the terminal controls. – Jonathan Leffler May 31 '15 at 03:49
  • Ctrl-A followed by a Ctrl-K also works. The 'killed' text is stored and you can paste it by using Ctrl-Y. These keystrokes in fact come from the read line library: https://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html .. so you can find them in any tool which user interface uses the library (Actually Emacs, bash, etc..). You can customize its behavior by editing the .inputrc file (take a look to the link above for more details). – rkachach Sep 22 '15 at 15:48
103

Try Ctrl+U. That clears the input line.

goncalotomas
  • 1,000
  • 1
  • 12
  • 29
gbarry
  • 10,352
  • 6
  • 33
  • 43
  • 8
    Thanks, I nearly accepted this as the answer. But I found it clears only the input before the cursor. In some cases I am in the middle of editing the command, so I would rather remember Ctrl+C instead of Ctrl+U as simply "discard the current input". – user85509 Jun 29 '09 at 03:15
31

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.

Lucas
  • 3,376
  • 6
  • 31
  • 46
27

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

goncalotomas
  • 1,000
  • 1
  • 12
  • 29
rur
  • 373
  • 3
  • 6
15

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.

Lucas
  • 3,376
  • 6
  • 31
  • 46
vks
  • 309
  • 4
  • 12
  • 2
    "Consider that using C-u (or C-e and then C-u) will store what you clear in a buffer so that you can then paste it later using C-y." Markisisme a simpler single command is ctrl+_. http://stackoverflow.com/questions/1056440/undelete-the-deleted-command-in-bash answered by john kug -- just started stackoverflow dont have enough points to add comment to you answer. – vks Jun 29 '09 at 03:44
9

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

goncalotomas
  • 1,000
  • 1
  • 12
  • 29
Nippey
  • 4,708
  • 36
  • 44
9

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

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 3
    In most terminals, this is also the same as Alt+#, which makes it one (chorded) keystroke. – Dolda2000 Mar 24 '17 at 04:22
  • A simple yet clever solution and I think this is a more user friendly way of cancelling a command. Exactly what I was searching for :) – Cas May 14 '17 at 17:07
4

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
Community
  • 1
  • 1
Cas
  • 6,123
  • 3
  • 36
  • 35
3

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.

goncalotomas
  • 1,000
  • 1
  • 12
  • 29
3

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!

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
Helmyano
  • 143
  • 1
  • 7
2

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

goncalotomas
  • 1,000
  • 1
  • 12
  • 29
  • If you want just the escape key to clear the whole line add `"\e\e": kill-whole-line` (has to be double press of the esc key though) – binaryfunt May 14 '20 at 09:21