Is there an equivalent key-stroke for exiting a command prompt on Windows (launched via Start->Run : cmd) to the bash standby of Ctrl-d to prevent needing to type exit to leave the shell?


No. CtrlD on *nix generates a EOF, which various shells interpret as running exit. The equivalent for EOF on Windows is CtrlZ, but cmd.exe does not interpret this specially when typed at the prompt.

  • 3
    With most modern shells, the terminal is not in canonical input mode when the shell is interactively accepting input, and Ctrl+D is in fact just an ordinary character and not an EOF special character. Shells bind Ctrl+D to a GNU Readline or ZLE action that exits (but only if the line editing buffer is empty) so the behaviour is mostly the same result. But Ctrl+D is not EOF with modern shells, just an ordinary control character. The behaviour when the editing buffer is not empty is markedly different to what happens with an EOF special character. – JdeBP May 31 '11 at 22:28
  • 3
    Indeed, the nearest Windows NT equivalent to what bash et al. are actually doing is a TCC/LE autoexecuting keyboard alias, such as this one, where the command interpreter (not the console) recognizes the ordinary keystroke combination ALT+F4 during line editing and executes the built-in exit command in response. – JdeBP May 31 '11 at 22:36
  • @JdeBP Going off this as the starting point, I just tested this with bash 4.4.12 and stty -a returns icanon (i.e. not -icanon). Is your comment With most modern shells, the terminal is not in canonical input mode when the shell is interactively accepting input really statistically correct at this point of time? – levant pied Nov 18 '19 at 16:52

Alt-Space. Then, C.

Yeah, it's a bit slower than Ctrl-D. But this is the answer to exactly what you're asking for: the built-in equivalent key stroke sequence that lets you exit the prompt without needing to type the exit command. No third party software needed.

This may not work absolutely identical to bash: bash will only logout on an empty command line. In Windows, this can close the window, even if you've already typed a partial command on it.

  • :D :D :D :D :D :D – Fatih Jun 12 '19 at 11:17

Fixed autohotkey approach. (I cannot add a comment.)

#IfWinActive, ahk_class ConsoleWindowClass
    ; First send ESC, in case we're in select mode.
    Send {Esc}{Esc}exit{Enter}
  • 1
    That's a very good solution. – Michael Wyraz Nov 12 '20 at 10:45

You can use DOSKEY to create macros in CMD.exe.

To create a macro type:

DOSKEY [macroname]=[command(s)]

That way you can bind a key to another command.


DOSKEY e=exit

would bind e to the exit command, so when you input an e to the prompt it would be like you input an exit

I'm not sure if you can use CTRL-$n combinations as macros though

  • Most control characters work okay (except those already handled by the console, of course). But cmd just interprets them as normal commands, waiting for Enter to be pressed, so there is no advantage of aliasing Ctrl-Z versus just e or x. – user1686 Jan 21 '12 at 19:13

There is now an open source project called clink at http://code.google.com/p/clink/ (edit: it moved to http://mridgers.github.io/clink/) - it brings Unix readline functionality to the Windows command prompt.

It supports most functionality including command-line editing, history search, tab completion along with completion scripts, etc. Well worth checking out, makes cmd suck much less. ;]


You can get the same effect with this AutoHotKey snippet:

; Close Command Prompt when pressing Ctrl+D
#IfWinActive, Command Prompt$
    ; First send ESC, in case we're in select mode.
    Send {Esc}{Esc}exit{Enter}
  • Doesn't seem to work, any clues why? – Avindra Goolcharan Jan 3 '15 at 18:57
  • @AvindraGoolcharan: I've been told, depending on how you start Command Prompt, the window title can be different from what I used in this script. The second line might need adjustments depending on your preference. – Der Hochstapler Jan 3 '15 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.