Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created March 13, 2012 19:02
Show Gist options
  • Star 89 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save jpoehls/2030795 to your computer and use it in GitHub Desktop.
Save jpoehls/2030795 to your computer and use it in GitHub Desktop.
Using CTRL+W to close tabs in Visual Studio

In Tools | Options | Keyboard...

  1. Add CTRL+W as a Global shortcut for Window.CloseDocumentWindow
  2. Remove the CTRL+W shortcut for Edit.SelectCurrentWord

The caveat to this is if you are used to using CTRL+W to select the current word. If you do, find another shortcut that works for that.

@travisterrell
Copy link

travisterrell commented Jan 29, 2018

Edit: SOLVED!

I have tried this so many times, but have not achieved success. Until seeing the comments from 2017 suggesting it still works, I assumed Visual Studio had made some change breaks the functionality.

For some reason, after removing the shortcut to Edit.SelectCurrentWord, and even a different key combination to it, pressing CTRL+W doesn't close the window. It still selects the current word. I have also tried assigning CTRL+W to File.Close, as some other sources suggest, but to no avail. Window.CloseDocumentWindow does function correctly with the default CTRL+F4, as well as with other unassigned key combinations (e.g. CTRL+ALT+SHIFT+])

Does anybody know what could possibly be wrong? Thank you!

Solution:
It was also assigned to other conflicting things (other than Edit.SelectCurrentWord). This seems obvious now, but I assumed a "Global" shortcut would override any other associations. Instead it seems that the global applies in any circumstance that a more specific shortcut isn't defined. (Which does make sense.)

So for anybody else experiencing this problem:

  1. Press CTRL+W as if assigning it to a command. Don't click the Assign button, though.
  2. Look at list of commands under "Shortcut currently used by: "
  3. Search for these individually, and click remove button for the CTRL+W key binding. (Re-assign to something else if desired.)
  4. Rinse and repeat.

Now, the global association made to CTRL+W shall work.

@miniduus
Copy link

miniduus commented Feb 7, 2018

Thx a lot :)

@CaBarr92
Copy link

Works in Visual Studio 2017 with a few few tweaks as travisterrell mention above.

CTRL + W is actually bound to 4 items which all need removing - then it you can set it to Window.CloseDocumentWindow.

The default command for Window.CloseDocumentWindow is CTRL + F4

@marin-bratanov
Copy link

Yes, @travisterrell is right, in my VS 2017 it was bound to 4 items by default, so you have to

  1. Remove the following mappings:
    • File.Close
    • Edit.SelectCurrentWord
    • Edit.ExpandSelection
    • Edit.NextKeyTyped
  2. Map Ctrl+W to Window.CloseDocumentWindow

@Erutan409
Copy link

Thank you!

@kallebornemark
Copy link

Thanks!

@retrodev-org
Copy link

7 years after you posted it, yet I've been banging my head against this for years. Thank you.

Copy link

ghost commented Mar 25, 2019

@jpoehls - OH MY GOD! Thank you so much for that! I've been screaming for years how to get that to work, CTRL-W was File Close so I removed that and changed Window.CloseDocumentWindow to CTRL-W and it never worked! I was like WTF!!?!?!?! I saw this post, removed the assignment to stupid select word (who doesn't use CTRL+SHIFT+Arrow to select words????) Thank you so much!

@Yeronimo
Copy link

Yeronimo commented Apr 2, 2019

@marin-bratanov Saved me further headaches! Thank you

@diogoquintanilha
Copy link

thanks!

@john-jay
Copy link

john-jay commented May 6, 2019

Mapping scheme: VS Code also works for this.

Copy link

ghost commented Aug 2, 2019

Posting from the world of tomorrow, where forks are made of lasers and this tip is still invaluable. Here are the steps I used to setup Visual Studio 2019, which should also work for 2015 and 2017:

  1. Using the Visual Studio top menu bar, navigate to "Tools > Options > Environment > Keyboard".
  2. In the “Show commands containing” textbox, type in “Window.CloseDocumentWindow”.
  3. In the “Shortcuts for selected command” section, click the “Remove” button until all existing shortcuts are deleted.
    • NOTE: I removed all of the existing shortcuts because I never use them. If you need them, though, keep them and/or choose a different keyboard shortcut.
  4. Set the “Use new shortcut in” dropdown to “Text Editor”.
  5. Set the “Press shortcut keys” textbox to “Ctrl+W”; click the “Assign” button to apply your changes.
  6. Click the “OK” button to save your changes.

NOTE: For some reason, I still have to use “Ctrl+W” twice to close a given tab. Not a big deal, just kinda weird given that the hotkey only has it registered once.

@ukod
Copy link

ukod commented Aug 7, 2019

Brilliant! Thank you a lot!

@vertisan
Copy link

vertisan commented Sep 2, 2019

In my VS Code it was command: workbench.action.closeActiveEditor (Linux)

@kintax
Copy link

kintax commented Sep 5, 2019

thank you! every time i install VS i forget this and i choose File.Close instead... hair-tearer for sure!

@DBJDBJ
Copy link

DBJDBJ commented Jan 5, 2020

Imagine the organization which can not assign "CTRL+W" to close the window in their flagship development product for a decade or more. Actually much more than a decade. This is not a snarky comment. I am just amazed users need an non-trivial community effort to solve that.

@Burgyn
Copy link

Burgyn commented Apr 1, 2020

thanks

@MarioBinder
Copy link

Thanks

@joseprofe
Copy link

Godlike

@panospd
Copy link

panospd commented Jun 16, 2021

The 2nd bullet did the magic! thanks

@Kev-in123
Copy link

Thank you! :)

@akunaatrium
Copy link

First thanks from 2022!

@DanCaprine
Copy link

Thanks!

@ugurozturk
Copy link

Thanks

@TheNotary
Copy link

Thanks

@domingoladron
Copy link

For those on Resharper, it appears Ctrl+W is used by a couple commands. In order to get Ctrl+W to close a window, you have to clear the Resharper keyboard settings first.

vs-resharper-ctrl-w

@ricardoschwarz
Copy link

thank you

@Nereare
Copy link

Nereare commented Nov 19, 2022

Thank you so much! ❤️

@LaurentBartkowski
Copy link

Thank you !

@niuage
Copy link

niuage commented Mar 3, 2024

Still useful to this day :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment