93

Is there a shortcut to clear the command line when typing in it instead of keeping backspace pressed for a few long seconds?

I ask because I regularly use the Tab key to automatically fill in file names when using bash commands in the Terminal. For example instead of typing cd this_is_a_long_file_name, I will type cd th<Tab>. But then I realize I have filled in the wrong command/filename and I have to hold down backspace to clear everything and repeat the process.

3

4 Answers 4

164

Ctrl+U will only clean from the cursor to the beginning of the line, so if the cursor is at the end of the line, all the line will be cleaned, and if not, there will remain the characters after the cursor.

For a complete answer, I will say:

  • Ctrl+U - clear all the current line from the end to the beginning only if the cursor is at the end of the line.

    • You can then recall the cleared line with Ctrl+Y if you need.
  • Ctrl+K - clear all the current line from the beginning to the end only if the cursor is at the beginning of the line.

    • You can then recall the cleared line with Ctrl+Y if you need.
  • Ctrl+W - clear the previous word in the current line. For example if you have typed a command like git diff /path/to/some/file and you want to delete just the last parameter to the command, Ctrl+W is very useful.

    • You can then recall the cleared data using Ctrl+Y if you need.
  • Ctrl+E Ctrl+U - move the cursor to the end of the line and clear all the current line from the end to the beginning.

    • You can then recall the cleared line with Ctrl+Y if you need.
  • Ctrl+A Ctrl+K - move the cursor at the beginning of the line and clear all the current line from the beginning to the end.

    • You can then recall the cleared line with Ctrl+Y if you need.
  • Ctrl+C - my favourite - cancel the current command line, which implies clear all the current line no matter where the cursor is.

    • Disadvantage: you can't recall the cleared line anymore.
  • Alt+Shift+# - comment the current line, keep it in the history and bring up your prompt on a new line.

2
  • 3
    There's also Ctrl+L, which outputs a bunch of newlines to hide previous output. (I know the question is about the input line itself)
    – Kroltan
    May 24, 2014 at 14:14
  • @Kroltan It is the same as writing "clear". But ctrl+L is shorter. Happy to learn it.
    – atilkan
    Feb 14, 2017 at 2:00
24

A shortcut, Ctrl+u should do what you want.

1
  • 5
    and Ctrl+l for clearing the screen (a bit like a stty -sane, but maybe a bit less potent and far reaching). Ctrl+l works in many apps, as well (it forces a redraw in vi, for example. Usefull when someone "write" or "wall" on it) May 23, 2014 at 8:05
17

If you realized you pressed the wrong key, press Ctrl+_ (underscore, you may need to hold Shift depending on your keyboard layout, e.g. Ctrl+Shift+- a US keyboard) to undo the last editing command.

If you realized that you typed the wrong command name but the argument is right, you can use cursor movement commands such as arrow keys, Home and End to navigate to the part of the command line that you want to modify.

The commands Alt+b and Alt+f move back and forth by one word at a time. Alt+d and Ctrl+w delete the next word after the cursor, or the end of the word if the cursor is within a word; Alt+b uses the shell's notion of a word while Ctrl+w uses whitespace-separated words. Esc Backspace deletes the word before the cursor.

If you want to abandon the current command line completely and start afresh, press Ctrl+c.

5

Use Ctrl-c to cancel the current command, this also works if you haven't yet pressed enter. It will not clear the terminal though, only bring up your prompt on a new line. To clear the visual part of the terminal, you may press Ctrl-l

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