19

In Windows command line (powershell and cmd), when you press Esc key while on a line, whatever you have typed at the prompt is removed.

I found that pressing Esc key at bash prompt does nothing. Pressing Esc and then backspace deletes a word, but this has to be done for each word.

I am learning Bash incrementally and sometimes type something stupid in the middle of the line and feel that it is better to type from scratch again. To do this, pressing backspace is the only way I found until now.

What do you do?

I am aware of the clear command and Ctrl-L shortcut, but I am not talking about clearing the entire terminal. Just the line.

1

4 Answers 4

18

You want kill-whole-line, but this is not bound by default in bash. backward-kill-line (CtrlX Backspace) and unix-line-discard (CtrlU) both erase from the current point to the beginning of the line, so just go to the end of the line and use either.

3
  • You are correct, it is not bound to anything. I just found out that using bind -P. I guess Ctrl-U and then Ctrl-Y is the only option then.
    – Animesh D
    Commented Feb 6, 2012 at 6:34
  • You'll find that a number of the default bindings are derived from the key sequences in the Emacs editor and that those bindings show up elsewhere, too.
    – Blrfl
    Commented Feb 6, 2012 at 11:41
  • @Blrfl to extend your comment, most of these keybindings have been abstracted into the readline library, which a large number commandline programs use and which is why they all have similar line editing keys.
    – jw013
    Commented Feb 7, 2012 at 1:07
16

You could use Ctrl+C to abandon whatever you were typing and get an empty new command line, see Is CTRL+C incorrect to use to return to command line?.

7

To expand on Ignacio Vazquez-Abrams's answer a little, you can get (almost) Windows-like behaviour from the Esc key in bash by binding the kill-whole-line to Esc with the following command

bind '"\e":kill-whole-line'

If you add the line to your ~/.inputrc file, the binding will persist between sessions.

Note, however, that this is very non-standard, as bash actually uses the Esc key as another modifying key (like a non-persistent Alt or Ctrl). If you look at the list of existing key bindings (with bind -P), you will likely see several commands bound to key-combinations that start with "\e" (e.g.

"\eb": backward-word

which sets up the combination Esc + B to move the cursor back to the most recent word-beginning – rather like Ctrl + , except that you have to keep releasing Esc (as well as B, of course) if you want to do it more than once in a row).

And that explains why I said that the behaviour you get is almost Windows-like: when you press Esc, bash checks to see if you're using it in combination with another key; so, if you bind it to kill-whole-line, there's a bit of a delay between pressing (or rather, releasing) Esc and bash clearing the line for you.

However, if you don't care about any of that, and would rather live with the delay than retrain yourself to a different keystroke, it can be done.

(Note: much of the information here I got as a result of reading – and following the links in – this answer.)

3
  • 4
    This was very helpful, thanks, and works more-or-less as advertised. But I did find it produced some annoying behaviour when other commands are executed "invalidly", such as pressing DEL when at the end of a line: This would clear the line and replace it with [19; or a similar-looking escape code. In the end I found mapping "\e\e" to kill-whole-line (i.e., tap Esc twice to clear the line) a nice compromise. Commented May 17, 2018 at 14:02
  • @j_random_hacker : Hm, I like that. I often hit Esc a few times anyway, so I might try that. I currently have it bound to Ctrl+Del but I never remember that.
    – Wilson F
    Commented May 17, 2018 at 18:32
  • I struggled to make this work for my .inputrc on Mac. It turns out I didn't need the bind command at the beginning, i.e., just do "\e\e": kill-whole-line. BTW, thanks @j_random_hacker for that double-escape tip - it's awesome!
    – Paul
    Commented Sep 1, 2021 at 20:39
6

Ctrl+a goes to the beginning of the line and Ctrl+k deletes to the end of the line

1
  • Try ^U first, in most Emacs command line editing mode implementations it already deletes the full line. In those where it only deletes left from the cursor (GNU readline, for example), press ^K afterwards.
    – mirabilos
    Commented Aug 25, 2019 at 2:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .