Skip to content

Instantly share code, notes, and snippets.

@josefandersson
Last active June 8, 2020 14:51
Show Gist options
  • Save josefandersson/92d66db600802d9ce53f29ec0041a33f to your computer and use it in GitHub Desktop.
Save josefandersson/92d66db600802d9ce53f29ec0041a33f to your computer and use it in GitHub Desktop.
A chromium extension for moving tabs with key commands
chrome.commands.onCommand.addListener(cmd => {
chrome.tabs.query({ currentWindow:true, active:true }, tabs => {
const tab = tabs[0];
if (!tab) return;
switch (cmd) {
case 'move-left': chrome.tabs.move(tab.id, { index:Math.max(0, tab.index-1) }); break;
case 'move-right': chrome.tabs.move(tab.id, { index:tab.index+1 }); break;
case 'move-to-first': chrome.tabs.move(tab.id, { index:0 }); break;
case 'move-to-last': chrome.tabs.move(tab.id, { index:-1 }); break;
case 'move-next-window': tabMoveWindow(tab, 1); break;
case 'move-prev-window': tabMoveWindow(tab, -1); break;
}
});
});
function tabMoveWindow(tab, dir) {
chrome.windows.getAll(wins => {
if (wins.length <= 1)
chrome.windows.create({ tabId:tab.id });
else
for (let i = 0; i < wins.length; i++)
if (tab.windowId === wins[i].id)
return chrome.tabs.move(tab.id, { windowId: wins[(i + dir + wins.length) % wins.length].id, index:-1 }, newTab => {
chrome.tabs.highlight({ windowId:newTab.windowId, tabs:[newTab.index] }, () => {
chrome.windows.update(newTab.windowId, { focused:true });
});
});
});
}
{
"name": "Move tabs",
"version": "1.0",
"description": "Use commands to move tabs",
"permissions": ["tabs"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"commands": {
"move-to-first": {
"suggested_key": {
"default": "Alt+K"
},
"description": "Move current tab leftmost"
},
"move-to-last": {
"suggested_key": {
"default": "Alt+J"
},
"description": "Move current tab rightmost"
},
"move-left": {
"suggested_key": {
"default": "Alt+H"
},
"description": "Move current tab left"
},
"move-right": {
"suggested_key": {
"default": "Alt+L"
},
"description": "Move current tab right"
},
"move-next-window": {
"description": "Move current tab to the next window"
},
"move-prev-window": {
"description": "Move current tab to the previous window"
}
},
"manifest_version": 2
}
@josefandersson
Copy link
Author

How to use:

  • Put both files in a folder.
  • Open any Chromium based browser and go to chrome://extensions.
  • Activate 'developer mode' and find the button to 'load unpacked'.
  • Click it and select the folder you put the files in.
  • Extension should now be loaded. Go to browser settings > extensions > Move Tabs, and change the key bindings to your liking.

@josefandersson
Copy link
Author

Updated:

  • If only one window exists and move-next/prev-window is pressed, a new window will be created and the tab moved there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment