Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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. :-) –  Nanda Feb 9 '12 at 0:57

4 Answers 4

up vote 13 down vote accepted

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.

share|improve this answer
    
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. –  Nanda 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

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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