90

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.

jnovack
  • 514
Ralph
  • 1,735

8 Answers8

86

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 (this is case sensitive). You must provide a keybinding, but it doesn't have to be the default. In the shortcut box, give it a different shortcut, like ControlW enter image description here

  • Now W will not close your terminal windows.

Daniel
  • 34,803
  • 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 '12 at 13:05
  • @Daniel: Nice screenshots! May I ask which program you used to take them? Or did you change your desktop background to all-white? – Roy Tinker Mar 19 '12 at 16:16
  • 5
    The program is called "Mac OS X" :-) Command-shift-4, spacebar, click in the window. – Daniel Mar 19 '12 at 16:20
  • 1
    Command-ctrl-shift-4, spacebar puts the screenshots in your clipboard so you can paste them. – Andrew M. Farrell Jan 26 '18 at 02:03
  • It is so bizzare that company that prides itself on UX has concluded "if people want to unbind this, they must instead bind it something ELSE." Just what I want to do, think about what keybinding I'm not using anywhere when they're all muscle memory. – Evan Carroll Mar 03 '23 at 17:50
35

You can set a prompt before closing in the preferences:

Terminal Preferences → Settings → Shell

enter image description here

gentmatt
  • 49,722
  • 1
    Answers crossed paths. This is a good one too. – Daniel Mar 19 '12 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 '12 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 '14 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!) http://images.luo.ma/osx/iterm/preferences-session-always-prompt-before-closing.jpg – Shwaydogg Aug 30 '14 at 19:16
20

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

10

I propose this:

In iTerm: Open Preferences (+,), click tab Keys, at the bottom of Key Bindings click +. In the appearing dialog, click Click to Set, then type +w and leave the default Action Ignore.

  • no need to manipulate global settings
  • no need to confirm conscious closing
5

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

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

  • 1
    Better than ignoring the useful command, you can undo the closing of the tab with CMD+Z. – b4d Feb 20 '18 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.

bot47
  • 7,742
1

For iterm, as @Shwaydogg mention:

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

  • That actually is THE answer to this post. No using the OS to hack-opt-out of the shortcut. Not ignoring the key in iTerm. Simply confirming that the user truly wants to close this. Of course, if your need is to totally disable it, the "Ignore" options is the way to go. – Adeynack Nov 14 '22 at 11:21
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

nhed
  • 153