31

In Terminal, let's say I type a long command, but before I press Enter, I decide that actually I don't want to run this command, and want to run another new command instead.

In such case, how to quickly remove everything I have typed, so that I can start typing the new command?

Pressing Delete for a few long seconds is not a good solution, I am looking for something faster.

3 Answers3

58

You can cancel current command with ⌃ CTRL+c. You can clear command to the beginning with ⌃ CTRL+u. To clear line after the cursor press ⌃ CTRL+K.

Here are some great answers on StackOverflow.

  • The crt + u one is a good one to know. crtl + k does not work. The only similar one I know to that is cmd+k which clears all visible history from the window. – markhunte Sep 22 '14 at 08:23
  • 3
    @markhunte if you are in the middle of the command line you are editing, ctrl-k will delete from the cursor to the end of the line – nohillside Sep 22 '14 at 08:26
  • 3
    "crtl + k does not work." It definitely works for OS X default key mappings. It deletes everything after the cursor. "ctrl+a" takes the cursor to the beginning of the line. – Trane Francks Sep 22 '14 at 08:26
  • 1
    @patrix , Ah I get you. I was at the end of the line. so had nothing to clear. Doh.. Good tips here. – markhunte Sep 22 '14 at 08:36
  • @Mateusz Szlosek, not all heroes wear capes. – SeeDerekEngineer Jan 11 '24 at 14:28
6

You can just use cmd + . to stop and go to a new command prompt on a new line.

It will not erase what you typed. But more importantly it will not execute it.

markhunte
  • 12,242
5

Terminal natively supports basic Emacs key mappings. As such:

Press Ctrl+a to move the cursor to the beginning of the line and then Ctrl+k to delete to the end of line.

Trane Francks
  • 2,380
  • 12
  • 12
  • Terminal doesn't support any key mappings. The shell you're running inside of Terminal supports key mappings. – Marc Wilson Dec 05 '21 at 17:10
  • @MarcWilson That's a logical assumption, but it's worth mentioning that Emacs key mappings feature natively across the operating system even today in modern releases such as Big Sur. You can open TextEdit, Skype, Pages or any number of apps and use the key mappings irrespective of whether they're Apple apps or not. That last phrase tells the story: Emacs key mappings are baked into the operating system. There are outlier 3rd-party apps, yes, but that seems to be by virtue of them having their own hardwired hotkeys. – Trane Francks Dec 06 '21 at 20:50
  • Yes, that's true, for applications that use standard input controls. The shell is not one such. Bash uses readline. Zsh and ksh have their own integrated line-editors and use ncurses for other things. I'm not sure what [t]csh does, it's been a long time. All can be told to use emacs controls, or vi controls, or in the case of ksh, to use no controls at all. So at some point the OP should have been asked how their shell was configured. Ah, well. – Marc Wilson Dec 07 '21 at 03:14
  • 1
    @MarcWilson Hi, again. I won't disagree that shells and some apps, e.g., less, have their own built-in keymapping handling. That said, Apple programmatically makes emacs bindings the default for all the shells installed on the system. It may have been otherwise early on, but it's been that way since at least Tiger, which was the first OS X version with which I worked. Given that the OP apparently wouldn't know that vi or emacs bindings are possible in the shell, I'd say it's safe to assume it was default. Have a great one, Marc! :) – Trane Francks Dec 08 '21 at 06:02