Filter Gmail Messages Shortcut
![Piotr Stojanow's avatar](/_astro/avatar@2x.DiUF6KWN_1Jumne.webp)
- Name
Piotr Stojanow
- @piotrstojanow
I have a Gmail account only for subscribing to newsletters, blogs and substacks. Often, I find myself selecting one or more emails and filtering them for an impromptu reading list. Not only does it save me from switching reading contexts, but it is also a great way to archive/delete multiple emails at once. Labels alone can’t solve this use case.
There is no keyboard shortcut to do this. You can press .
(period) for the dropdown “More” menu, navigate up/down a few times with cursor keys and select “Filter messages like these” with Enter
.
Since I mainly use the keyboard to navigate in Gmail and want to save myself a few clicks, I created the following userscript (GitHub) to add a shortcut (“alt+g”) for the above workflow.
(function() {
'use strict'
function handleKeyPress(event) {
if (event.altKey && event.code === 'KeyG') {
const main = document.querySelector('div[role=main]');
const emailElements = main.querySelectorAll('tr')
const selectedEmailRows = Array.from(emailElements).filter((row) => {
const tdElements = row.querySelectorAll('td');
const hasAriaChecked = Array.from(tdElements).some((td) => {
const isChecked = td.querySelector('div[aria-checked]')
if (isChecked) {
return isChecked.getAttribute('aria-checked') === 'true'
}
});
const hasEmailSpan = row.querySelector('span[email]');
return hasAriaChecked && hasEmailSpan;
});
if (selectedEmailRows.length === 0) {
selectedEmailRows = [emailElements[1]]
}
let emails = []
selectedEmailRows.filter((emailRow) => {
const emailElement = emailRow.querySelector('td span[email]')
const emailAddress = emailElement.getAttribute('email');
emails.push(encodeURIComponent(emailAddress))
})
const searchUrl = `https://mail.google.com/mail/u/0/#search/from:(${emails.join(' OR ')})`;
window.location.href = searchUrl;
}
}
document.addEventListener('keydown', handleKeyPress);
})();
If you prefer a bookmarklet just drag and drop the following button to your bookmark bar:
Select the emails you want to filter and click the bookmarklet (or set a keyword for it and run it from the address bar).
UPDATE (13-06-2024): I’ve modified the script to allow filtering based on an opened email. Open any email and press “alt+g” to filter all messages from the same sender.