13

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
    A while back I posted a list of useful readline keyboard shortcuts on superuser. You may find them useful as well. (Readline is the library that bash uses for line-editing.) – jw013 Feb 7 '12 at 1:10
  • hey jw013, thank you for a nice edit. :-) – Animesh Feb 9 '12 at 0:57
15

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.

  • 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 Feb 6 '12 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 Feb 6 '12 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 Feb 7 '12 at 1:07
13

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

6

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

3

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

  • 1
    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. – j_random_hacker May 17 '18 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 May 17 '18 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.