35

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?

8 Answers 8

26

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.

4
  • 4
    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, 2011 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, 2011 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? Nov 18, 2019 at 16:52
  • 1
    @levantpied The tty is icanon because Bash sets it for stty -a (like for any command, e.g. cat). When stty runs, it's not "when the shell is interactively accepting input". Once stty exits, Bash sets the tty back to -icanon for itself. This is "when the shell is interactively accepting input". To see this -icanon Bash sets for itself, you need to run stty -a from elsewhere (e.g. stty -F /dev/pts/5 -a from another console). Oct 4, 2022 at 12:13
13

Alt-Space. Then, C.

  • (Well, that works on my system, where "C" chooses the option called "close". Some variation may be needed, as discussed further, later in this answer.)

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.

Some more notes about variations:

  • Note that a variation might be Alt-Space (pop up Windows context menu), then up arrow (choose bottom option, which might often be Close/Exit), and then Enter (to choose the highlighted menu option).
    • Naturally, whether a specific keystroke sequence works with a specific program is a detail that can potentially vary between different software programs and, as noted by Richard's comment, different variations (such as different-language release versions). However, something like that is likely to be an available option for many programs.
  • The information mentioned here will work with many pieces of software designed to run in Microsoft Windows (including software bundled in with the operating system, and third party software), but, even so, surely carefully proceeding and testing is recommended before counting on very specific behavior.
    • That said, I accept Richard Neumann's helpful feedback (in a comment to this answer), which is why this answer has been edited to include some information about potential variations.
3
  • 2
    This is a locale-dependent answer. Alt+Space opens the Window's context menu and C is then the shortcut so close the window when running with English locale. On my German system it's actually an S(chließen) that I have to press. Oct 3, 2022 at 19:19
  • @RichardNeumann Thank you
    – TOOGAM
    Oct 4, 2022 at 11:53
  • A rather locale-independent shortcut would be Alt-F4 which simply closes the window.
    – Spock
    Jun 26, 2023 at 21:04
7

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

#IfWinActive, ahk_class ConsoleWindowClass
  ^d::
    ; First send ESC, in case we're in select mode.
    Send {Esc}{Esc}exit{Enter}
#IfWinActive

Update 2022-10-04

@RichardNeumann wrote it does not work, so checked my own files, it still does. But I do have another code example:

#IfWinActive ahk_class ConsoleWindowClass

^d::Send, exit {Enter}

#IfWinActive

Works as for cmd.exe and for powershell.

7
  • 1
    That's a very good solution. Nov 12, 2020 at 10:45
  • I used to use this solution for months until I regularly had to connect to a host with screen, and every time I try to detach from a session, I end up sending "e" to the session and write "xit" instead. It was so frustrating I ended up giving up on the AHK hack.
    – mohkamfer
    Feb 9, 2022 at 13:22
  • Here's a nice hack: If you change exit to exit() Ctrl+D works for the python REPL inside cmd too. cmd doesn't seem to mind the () after exit.
    – Socowi
    Jul 29, 2022 at 11:07
  • This does not work on Windows 10. However Set-PSReadlineKeyHandler -Key ctrl+d -Function ViExit does. Oct 3, 2022 at 19:32
  • 1
    Apologies, I thought this was supposed to be a Powershell script. I did not realize that Autohotkey referred to some third-party software. Oct 4, 2022 at 22:59
5

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.

Example:

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

1
  • 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. Jan 21, 2012 at 19:13
3

In Windows 10 using the old conhost.exe terminal the equivalent is Alt+F4! The option is controlled by the registry key AllowAltF4Close and is enabled by default. See Command prompt in Windows 10 can be closed by Alt+F4 for how to disable it (which is what you don't want)

In case of Windows Terminal it's even easier, just press Ctrl+Shift+W to close the current tab/pane. It'll work for any shells like cmd, powershell, bash... Windows Terminal is vastly better, faster, more performant, supports Unicode and POSIX ANSI sequences fully, and completely configurable so there's no reason to use the old conhost.exe terminal anymore. Even Windows 11 made Windows Terminal the default console

2

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. ;]

1

Windows Terminal now lets you choose what keyboard shortcut you want for "close tab".

I'd recommend switching to that.

0

You can get the same effect with this AutoHotKey snippet:

; Close Command Prompt when pressing Ctrl+D
#IfWinActive, Command Prompt$
  ^d::
    ; First send ESC, in case we're in select mode.
    Send {Esc}{Esc}exit{Enter}
#IfWinActive
2
  • Doesn't seem to work, any clues why? Jan 3, 2015 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. Jan 3, 2015 at 20:14

You must log in to answer this question.

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