I’ve recently switched back to Safari from Chrome. I have Safari set with a couple of pinned tabs which works great. The only issue I have is that when I use a keyboard shortcut to close regular tabs (Command+W), if there is only one regular tab then it closes the Safari window.
Does anyone know of a way, possibly using AppleScript, to prevent Command+W from ever closing the window. I actually have a separate keyboard shortcut (Command+Shift+W) to close the window. If the last regular tab is closed then I want the window to persist and possibly have a blank tab remaining.
Thanks.
My suggestion would be to go to the General tab in Safari preferences and set a homepage to whatever you choose, then, whenever you’re down to your last tab, hit Command-Shift-H instead of Command-W. That opens whatever you have set for your homepage in preferences.
That sounds like Keyboard Maestro might be able to help them.
If KM can check for the number of tabs left, an IF statement could be used to change the CMD-W into CMD-S-H when there’s only one tab.
Clearly I haven’t tried this, but it sounds plausible to me.
Thanks, but I really don’t want another keyboard shortcut. I use Command+W to close tabs and the problem usually arises when I don’t realize I’m down to my last tab and the window closes.
I’m never used Keyboard Maestro. I’ll give it a look. Thanks.
I highly endorse the use of Keyboard Maestro, and I think @GraemeS’s solution is more clever than mine. But if I had to choose between learning one new shortcut or learning KM just to do one thing? I might choose the shortcut.
If you want to use Keyboard Maestro while keeping ⌘W, this macro in an macro group set to be available only in Safari might help:
I think the behavior you described with ⌘W closing the window is only true when there is more than one Safari window. So this macro checks that first, and then if the frontmost window has less than 2 tabs open, ⌘W will go to your home page set in Safari Preferences (you can change that to a different action within “Execute the following actions”). Otherwise, if there are 2 or more tabs, it will do a traditional ⌘W.
I didn’t test this too extensively, and there may be a more elegant way to do it, but it may point you in the right direction. Here’s the AppleScript:
set W to 0
set T to 0
tell application "Safari"
set W to count of every window
if W is greater than 1 then
try
set T to count of every tab of window 1
end try
else
set T to 2
end if
end tell
return T
4 Likes
Hello from the future! Since this is currently one of the top Google results for this issue, just wanted to note that this is totally doable without having to resort to any third-party software.
There is a workaround! You can overwrite ⌘-W to always execute “Close Tab”.
Go to System Preferences > Keyboard > Shortcuts > App Shortcuts
Add a Shortcut for the application “Safari”, menu item “Close Tab”, shortcut ⌘-W.
Source: macos - Stop Safari window closing when only pinned tabs are left - Ask Different
I’ll add that the workflow for adding a totally new shortcut is perhaps not as intuitive as it could be: You need to hit +, scroll down to App Shortcuts, then select Safari from the drop down, and where it says “Menu Title” you just type “Close Tab” (or the equivalent in your language). Clarifying this because it’s NOT the title of the menu the command is under (which is File) but rather simply the exact name of the command.
One other note, if you WANT to close the window, you can just add Shift to ⌘-W. I find it much more sensible this way.
OK, carry on what you all are doing back in 2021. Make sure you really enjoy yourself because here in 2025, hoo boy.
2 Likes