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
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.
-
5With 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 bindCtrl+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. ButCtrl+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.– JdeBPMay 31, 2011 at 22:28 -
3Indeed, 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 combinationALT+F4
during line editing and executes the built-inexit
command in response.– JdeBPMay 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
returnsicanon
(i.e. not-icanon
). Is your commentWith 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 forstty -a
(like for any command, e.g.cat
). Whenstty
runs, it's not "when the shell is interactively accepting input". Oncestty
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 runstty -a
from elsewhere (e.g.stty -F /dev/pts/5 -a
from another console). Oct 4, 2022 at 12: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.
-
2This 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
-
-
A rather locale-independent shortcut would be Alt-F4 which simply closes the window.– SpockJun 26, 2023 at 21:04
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
.
-
1
-
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. Feb 9, 2022 at 13:22
-
Here's a nice hack: If you change
exit
toexit()
Ctrl+D works for thepython
REPL insidecmd
too.cmd
doesn't seem to mind the()
afterexit
.– SocowiJul 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 -
1Apologies, 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
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
-
Most control characters work okay (except those already handled by the console, of course). But
cmd
just interprets them as normal commands, waiting forEnter
to be pressed, so there is no advantage of aliasing Ctrl-Z versus juste
orx
. Jan 21, 2012 at 19:13
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
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. ;]
Windows Terminal now lets you choose what keyboard shortcut you want for "close tab".
I'd recommend switching to that.
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
-
-
@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