84

How can I clear the current line in the command prompt? (I'm using Windows 7.)

Too often, I enter a command, execute it, get many lines of output, then wish to enter another command. But before entering the second command, I press the up arrow key to review the first command, then I find I have to hold backspace for 30-or so characters. (I can't just press down again to get an empty line. Nor can I get it by pressing up again.)

essential
  • 211
  • 2
  • 7
JellicleCat
  • 2,227
  • 4
  • 24
  • 31
  • @myrddim has it but that aside, some poor alternatives are keep tapping up until you have something short then hold backspace. another one is make the command be a bad command or filename, or rather, an "'sdfd' is not recognised as an internal or external command" so move the cursor a bit with left arrow and just tap a letter or don't and just tap a letter at the end, mess up the cmd input, then hit ENTER, get that error and the next line will be blank. – barlop Mar 05 '12 at 18:55
  • 1
    @barlop That is indeed a poor workaround! It only takes one keystroke. If you can't use the `Esc` key, you could use AutoHotkey to simulate it. – iglvzx Mar 05 '12 at 19:20

2 Answers2

142

The Escape (Esc) key will clear the input line.

In addition, pressing Ctrl+C will move the cursor to a new, blank line. This may be helpful as the input you just reviewed remains visible while you type the new command.

Myrddin Emrys
  • 2,371
  • 3
  • 21
  • 29
  • 3
    And if you want to clear the whole screen, you can use the `cls` command. :) – iglvzx Mar 05 '12 at 18:52
  • 11
    As a side note, on UNIX it's `ctrl+u`. – Milo Wielondek Oct 22 '12 at 23:12
  • 2
    Which would be relevant if the question wasn't specifically targeted at the Windows 7 command prompt. :-) Also, I believe this is shell specific, as I am reasonably certain some shells have the same behavior as Windows when pressing `Ctrl+C`. – Myrddin Emrys Oct 22 '12 at 23:19
  • and how do you remove all the text so you can't scroll up? – FutuToad Jul 26 '14 at 16:55
  • 1
    @FutuToad the `clear` command will do what you ask. – Myrddin Emrys Jul 28 '14 at 03:57
  • 2
    @MyrddinEmrys on Windows it is `cls` – CJ7 Jun 01 '16 at 05:24
  • 3
    Note: the `CTRL+C` command will also exit any command line session you are having, i.e `python`, and return you back to the command line environment. – SexyBeast Jul 14 '16 at 07:41
  • 1
    @AttitudeMonger That is a **common** command line utility behavior, but is not actually a feature of the command line itself. Many command line utilities do not exit on receipt of the Ctrl+C keystroke. It will not exit *any* command, but it is accurate to say it exits *many* commands. – Myrddin Emrys Jul 15 '16 at 01:07
  • Oh I see. Thanks, I did not know that! – SexyBeast Jul 15 '16 at 06:33
  • 1
    I use Ctrl + backspace to clear the current line, or ctrl + Home, if you press just `home` you go to the start of the line – Ervin Sep 24 '16 at 20:00
  • Is it possible to set it to `ctrl+u` like in windows. I have to lift my hand to reach `esc` – balki Aug 07 '18 at 22:01
  • ctrl+u on mac, thank you! – ennth Feb 12 '21 at 23:13
40

Aside from the two that Myyrddin covered - Esc and Ctrl+C - there are also two more shortcuts related to clearing the current input in CMD.

Ctrl+Home will clear all characters in the input before the cursor (equivalent to Ctrl+U in Bash)

Ctrl+End will clear all characters in the input after the cursor (equivalent to Bash's Ctrl+K)

Both are fairly useful and, once internalised and gotten used to, could speed up editing in CMD by quite a bit.

Hashim Aziz
  • 11,898
  • 35
  • 98
  • 166
  • 2
    I didn't know about these keystrokes; thank you. May you get the upvotes you deserve. – Myrddin Emrys Jan 31 '17 at 13:47
  • 1
    Same here. Being used to Bash I always wanted those. OP is my savior. – uranusjr Jul 25 '17 at 15:57
  • 1
    From bash(1): (Ctrl)+(X), (Rubout) is defined to be **backward-kill-line**; i.e., kill backward to the beginning of the line; i.e., the same as (Ctrl)+(Home) in Windows CMD. “Rubout” *may* be Backspace or Delete, so try (Ctrl)+(X), (Backspace) and (Ctrl)+(X), (Delete). Also, (Ctrl)+(U) is **unix-line-discard**; i.e., kill backward from the insertion point (cursor) to the beginning of the line; i.e., also the same as (Ctrl)+(Home).  (Ctrl)+(K) is defined to be **kill-line**; i.e., kill the text from the insertion point to the end of the line; i.e., the same as (Ctrl)+(End) in Windows CMD. – Scott - Слава Україні Aug 26 '18 at 05:47
  • 2
    Just to be clear: (Ctrl)+(U) is a long-standing, general feature of Unix.  It works in old, non-Linux, non-GNU, non-bash systems; it works in `vi`; it works if you do `cat > file123`.  The others are specific to the bash command line. – Scott - Слава Україні Jul 22 '19 at 16:12
  • 2
    Shift+Home and Shift+End selects the same as above, instead of deleting it. – Dercsár Apr 30 '22 at 19:00