Chrome: open new tabs beside the current one
On my Mac, Chrome’s ⌘T puts new tabs at the end. I wanted them beside the tab I was using, without another extension. I already use Hammerspoon, so I added a Chrome-only shortcut.
With B active:
Before: A | B | C
⌘T: A | B | New | C
The new tab is selected. This changes ⌘T; the + button keeps its usual behavior.
Hammerspoon setup
The full code and setup instructions are also on GitHub Gist.
With Hammerspoon installed, save this as ~/.hammerspoon/chrome-tabs.lua:
-- Chrome: Cmd-T opens and selects a tab immediately after the active tab.
local chromeBundleID = "com.google.Chrome"
local log = hs.logger.new("chrome-tabs", "info")
chromeNewTabHotkey = hs.hotkey.new({"cmd"}, "t", function()
local ok, _, details = hs.osascript.applescript([[
tell application "Google Chrome"
if (count of windows) = 0 then
make new window
else
tell front window
set currentIndex to active tab index
make new tab at after tab currentIndex with properties {URL:"chrome://newtab/"}
set active tab index to currentIndex + 1
end tell
end if
end tell
]])
if not ok then
log.e(hs.inspect(details))
hs.alert.show("No se pudo abrir la pestaña de Chrome. Consulta la consola de Hammerspoon.")
end
end)
local function updateHotkey()
local app = hs.application.frontmostApplication()
if app and app:bundleID() == chromeBundleID then
chromeNewTabHotkey:enable()
else
chromeNewTabHotkey:disable()
end
end
-- Retain the watcher and hotkey across garbage collection.
chromeTabWatcher = hs.application.watcher.new(function(_, event)
if event == hs.application.watcher.activated
or event == hs.application.watcher.deactivated then
updateHotkey()
end
end)
chromeTabWatcher:start()
updateHotkey()
Add this line to your existing ~/.hammerspoon/init.lua:
dofile(hs.configdir .. "/chrome-tabs.lua")
Choose Hammerspoon → Reload Config. If macOS asks, allow Hammerspoon to control Chrome.
How it works
Lua handles the shortcut and watches which app is active. The English-looking code inside [[ ... ]] is AppleScript stored as a string. hs.osascript.applescript runs it to control Chrome. active tab index is the selected tab’s position, counting from 1.
I checked the tab order in Chrome and repeated the test after reloading. The shortcut also disabled correctly outside Chrome.