27

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 Answers 3

50

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.

4
  • 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, 2014 at 8: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, 2014 at 8: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. Sep 22, 2014 at 8: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, 2014 at 8:36
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.

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.

4
  • Terminal doesn't support any key mappings. The shell you're running inside of Terminal supports key mappings. Dec 5, 2021 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. Dec 6, 2021 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. Dec 7, 2021 at 3: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! :) Dec 8, 2021 at 6:02

You must log in to answer this question.

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