80

Is there any way to disable Command+W in the terminal?

On several occasions I have accidentally closed a terminal window containing important information when I meant to close a Safari tab and did not realize that the terminal was the active window.

8 Answers 8

76

To disable W in Terminal, do the following:

  • From the  menu in the top left corner of the screen, select System Preferences. Click on Keyboard then Keyboard Shortcuts then Application Shortcuts. enter image description here

  • Click the + button to add a new shortcut

  • Select "Terminal.app" for the application, and for the command, type Close Window (this is case sensitive). In the shortcut box, give it a different shortcut, like ControlW enter image description here

  • Now W will not close your terminal windows.

6
  • I mis-spoke in my question. I actually meant to prevent closing a tab, but your answer still works well with the slight change of "Close Window" to "Close Tab". BTW, thanks for the VERY complete answer.
    – Ralph
    Mar 19, 2012 at 13:05
  • 5
    The program is called "Mac OS X" :-) Command-shift-4, spacebar, click in the window.
    – Daniel
    Mar 19, 2012 at 16:20
  • 15
    My system is Sierra, and I change the Close Window to the Close, it works for me. Thanks.
    – bwangel
    Mar 23, 2017 at 14:39
  • 1
    Command-ctrl-shift-4, spacebar puts the screenshots in your clipboard so you can paste them. Jan 26, 2018 at 2:03
  • 1
    Just want to add that this still works at macOS Big Sur (on Apple M1 but I don't think that matters). And just like bwangel's comments, it's "Close" instead of "Close Window". Now I can play dota2 in peace :) Nov 23, 2021 at 7:55
32

You can set a prompt before closing in the preferences:

Terminal Preferences → Settings → Shell

enter image description here

4
  • 1
    Answers crossed paths. This is a good one too.
    – Daniel
    Mar 19, 2012 at 12:44
  • 4
    I think this is the best solution for this particular use case. Disabling standard shortcuts makes me feel icky and would get frustrating when muscle-memory kicks in when I want to close a terminal tab/window.
    – ghoppe
    Mar 19, 2012 at 17:58
  • -1: OP asked a very specific question that could easily be modified for various applications. This is a Terminal.app specific answer which does not answer the question.
    – bot47
    Mar 10, 2014 at 20:14
  • 9
    For those using iTerm go to Prefrerences -> Profiles -> Session -> Prompt before closing! Make sure you are on the correct profile (your default one has the star!) images.luo.ma/osx/iterm/…
    – Shwaydogg
    Aug 30, 2014 at 19:16
18

I tried all of the above, and none worked for me.

What worked was changing the shortcut for the "Close" command.

enter image description here

6
  • 3
    Same here. "Close" worked for me but "Close Windows" did not. Oct 31, 2013 at 18:42
  • 1
    Ian, it says "iterm" not "TERMINAL" Mar 7, 2014 at 1:13
  • 3
    @LM126, yep, my example is using iTerm2, so not sure what your on about? Mar 7, 2014 at 9:38
  • you can make iterm prompt when you close a session. iterm pref -> profiles -> session: always prompt before closing. this will prevent you from accidentally close session by shot cut.
    – hihell
    Aug 16, 2018 at 10:37
  • 1
    This worked for me on Mojave 10.14.5 - Since the original answer is from 2012, this answer is now more correct. Jul 25, 2019 at 1:42
7

I propose this:

In iTerm: Open Preferences>Keys At the bottom of "Global Shortcut Keys" click "+", then type Cmd-W and leave the default Action "Ignore".

  • no need to manipulate global settings
  • no need to confirm conscious closing
3
  • This is the only effective solution for me on Mac OS 10.10
    – bitcycle
    May 26, 2015 at 14:47
  • Do you know how to add an alternative shortcut to close the window/tab then?
    – kuropan
    Apr 24 at 15:54
  • How about using Ctrl-D? Apr 24 at 21:44
4

A bit of an old question but in iTerm2 this helped me:

iTerm2Preferenceskeys → Add key mapping: "command + w" => ignore

1
  • 1
    Better than ignoring the useful command, you can undo the closing of the tab with <kbd>CMD</kbd>+<kbd>Z</kbd>.
    – b4d
    Feb 20, 2018 at 11:34
2

Goto Keyboard System Preferences and select "Keyboard Shortcuts". Then choose Application Shortcuts from the left and click +. Choose "Terminal.app" and enter a menu item that is not that critical. I chose "Bring All to Front". Then click into Keyboard Shortcut and hit CMD+W. Click OK.

Using this technique have have successfully redirected the CMD+W shortcut in Terminal.

1

For iterm, as @Shwaydogg mention:

For those using iTerm go to Prefrerences -> Profiles -> Session -> Prompt before closing!

0

Also worth mentioning BetterTouchTool (http://www.bettertouchtool.net/)

I wanted to have both W & C both perform Copy (muscle memory, don't ask). The native method only allows you one key per command, with BTT I was able to have both keys mapped to same command, while also avoid the undesired Close WIndow behaviour

You must log in to answer this question.

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