Moving Safari Tabs with Keyboard Shortcuts

Boon aka Hwee-Boon Yar April 3, 2024
Source
For years, I have F2 and F3 in vim set to move the current tab left and right respectively — I wrote about navigating tabs with the keyboard in MacVim previously. I wanted to do the same in Safari. I found a way to do it with AppleScript. The short answer: Safari already has built-in shortcuts for switching tabs, and macOS lets me remap those menu commands. Moving the current Safari tab left or right takes a small AppleScript Quick Action. The built-in Safari shortcuts are: * ctrl-tab or shift-cmd-] — select the next tab * ctrl-shift-tab or shift-cmd-[ — select the previous tab I prefer ctrl-h and ctrl-l because they match how I move between tabs in tmux and other tools. The rest of this post is how I set that up, plus cmd-F2 and cmd-F3 for moving the current Safari tab left and right. Save this as a Quick Action "Move Current Safari Tab Left.workflow" as a Run AppleScript action: --Move left tell application "Safari" set currentTabIndex to index of current tab of front window set totalTabs to count of tabs of front window -- Ensure there is a previous tab to swap with if currentTabIndex > 1 then set previousTabIndex to currentTabIndex - 1 -- Swap the tabs tell front window set currentTab to tab currentTabIndex set previousTab to tab previousTabIndex move previousTab to after currentTab end tell end if end tell Save this as a Quick Action "Move Current Safari Tab Right.workflow" as a Run AppleScript action: --Move right tell application "Safari" set currentTabIndex to index of current tab of front window set totalTabs to count of tabs of front window -- Ensure there is a next tab to swap with if currentTabIndex < totalTabs then set nextTabIndex to currentTabIndex + 1 -- Swap the tabs tell front window set currentTab to tab currentTabIndex set nextTab to tab nextTabIndex move nextTab to before currentTab end tell end if end tell The workflows accept no input in Safari.app. Then, under the Settings.app > Keyboard > Keyboard Shortcuts > App Shortcuts, assign these 2 to Safari.app: * "Move Current Safari Tab Left" to cmd+F2 * "Move Current Safari Tab Right" to cmd+F3 (I didn't use F2 and F3 because I have assigned them to do other things in Safari) Couple them with assigning these to All Applications too: * "Select Previous Tab" to ctrl-h — switch to the previous Safari tab * "Select Next Tab" to ctrl-l — switch to the next Safari tab These 4 keyboard shortcuts make moving around and re-ordering tabs in Safari so much easier.

Discussion in the ATmosphere

Loading comments...