Clear command from terminal in OS X

25

13

Say I've typed out a command in terminal 'mv *.jpg ..\photos' and as I'm typing realize I no longer want to execute this command at all. I want to just delete the entire statement. I don't want to clear the entire screen or delete any bash history I just want to abort the current line and erase it.

Is there a keyboard shortcut or do I have to actually delete each character?

Paul Alexander

Posted 2010-09-24T22:23:27.383

Reputation: 581

Answers

37

Assuming you're in bash, use Ctrl + U.

John T

Posted 2010-09-24T22:23:27.383

Reputation: 149 037

How do you cause this in a script? – Jonny – 2014-06-20T03:17:58.183

Thanks! that did it. Is there a format list of keyboard shortcuts documented somewhere? – Paul Alexander – 2010-09-24T22:29:54.453

1http://www.ice2o.com/bash_quick_ref.html – ghoppe – 2010-09-24T22:30:52.660

2This worked for me in zshell too. – LandonSchropp – 2012-07-12T18:04:38.083

1This only works if your cursor is at the END of the line. For those situations where your cursor is at the beginning you need to use CTRL-K – Jarrod Nettles – 2013-03-10T05:51:38.460

7

Many programs that wait for you to type a command line and then execute that line, including bash, take Ctrl+C to mean “Abort the current task and go back to the main prompt”.

A few programs, including bash, take Ctrl+U(Clear command from terminal in OS X) to mean “erase the beginning of the line, up to the cursor” (or “erase the whole line”).

In bash, observable differences between Ctrl+C and Ctrl+U include:

  • You can undo Ctrl+U, but not Ctrl+C.
  • Ctrl+U retains the part of the line after the cursor, if any.
  • Ctrl+U erases the affected text from the screen; Ctrl+C shows a new prompt below the current line.

Sometimes you want to stop typing a command but enter it in the shell history to run later. A useful trick is to put a # at the beginning of the line and execute it: since the line is now a comment, it won't do anything. In bash, there is a single-key shortcut: Alt+#.

Gilles 'SO- stop being evil'

Posted 2010-09-24T22:23:27.383

Reputation: 58 319

2

There is a readline function that is probably not bound to a keystroke called kill-whole-line that will kill the whole line as opposed to only killing the part before the cursor (unix-line-discard which is somewhat of a misnomer and is bound to Ctrl-u).

You can bind that to any available keystroke. I like ShiftAlt-U since it's a related function. That keystroke may be bound to do-lowercase-version which means it does whatever the unshifted version does (in this case upcase-word). Since we don't really need two keys to do that, let's use that one.

You can try it out at the command line by creating the binding this way:

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

or make it persistent by putting this line in your ~/.inputrc file:

"\eU":kill-whole-line

To undo a Ctrl-u or ShiftAlt-U (or any operation that can be undone), press Ctrl-Shift-_ (underscore) or Ctrl-x Ctrl-u (two keystrokes). Or you can paste back in (yank) what you killed by pressing Ctrl-y which can be repeated if you want multiple copies of that text.

By the way, if you want to kill text from the cursor to the end of the line, you can press Ctrl-k. So instead of doing the binding described above, you can kill a whole line by pressing Ctrl-u Ctrl-k. Only the part killed by the second of those keystrokes will be saved in the kill buffer, but you can still do undo twice to recover them both.

Paused until further notice.

Posted 2010-09-24T22:23:27.383

Reputation: 86 075