Introduction

I'm a big advocate of using keyboard shortcuts in order to be more efficient. One shortcut that I use all the time on Mac is cmd+q to close the active application. The only problem is that I also us cmd+w a lot to to close the active tab in the current browser, text editor, finder window, etc. And since w and q are right next to each other on most keyboards, I would hoften end up closing entire applications when all I wanted to do was close the current tab.

To remedy this I added a bit of functionality to my Mac that requires double clicking cmd+q in order to close an application.

Required Application

To get started I downloaded Hammerspoon. Hammerspoon is an automation tool for macOS that uses the Lua scripting engine.

Once Hammerspoon is downloaded you need to add an init.lua file in ~/.hammerspoon.

bash

nano ~/.hammerspoon/init.lua

Add the script

Once Hammerspoon is downloaded all you need to do is add this script to the init.lua file and you're good to go.

lua

-- Press Cmd+Q twice to quit

local quitModal = hs.hotkey.modal.new('cmd','q')

function quitModal:entered()
    hs.alert.show("Press Cmd+Q again to quit", 1)
    hs.timer.doAfter(1, function() quitModal:exit() end)
end

local function doQuit()
    local res = hs.application.frontmostApplication():selectMenuItem("^Quit.*$")
    quitModal:exit()
end

quitModal:bind('cmd', 'q', doQuit)

quitModal:bind('', 'escape', function() quitModal:exit() end)

Was this post helpful?