2017年10月18日 星期三

Gmail Filter

Reference:

http://raisedbyturtles.org/view-unlabeled-gmail


Why do we even want to find unlabeled emails?

I assume if you’ve arrived here, you already have your reasons. But you might be asking, why get so obsessive?
A couple of possible reasons:
  • You may want to do an import from another account or something like that. So you label everything currently in your account with “main” and then when you import, you find all unlabelled messages, label them “imported” and then delete the “main” label.
  • You just like every single thing to have a label because you’re that kind of person.
I’ll be honest — when I wrote this, I wanted everything to have a label. Why? Because in the past I had used clients like Thunderbird and Outlook that had the concept of folders, but not labels. The inbox is just a folder. So if you want to get something out of your inbox, it has to go in a different folder.

Gmail is different

Gmail is different. For one, the search function is really good. This means that I am more likely to look for something via search, than by drilling down through folders/labels.
In short, I do not ever use this method myself anymore. Instead, I have a manageable number of labels and a lot of filters. Most recurring emails get a label automatically and then I archive when needed. I  label things that I need for taxes or with a project name, but typically I have a filter that just applies the label. Then I depend on search. Automate what you can. Forget 90% of the rest.
That doesn’t mean people don’t have very good and compelling reasons for wanting to find unlabeled messages, but you might ask yourself whether or not you’re bringing an Outlook/Thunderbird mindset to a Gmail and just let it go.
I originally wanted everything labeled, but I was wrong. What I found was that I was better at remembering conversation keywords than I was at remembering labels and searching for conversation keywords is Google’s core competency. I decided that organizing my email manually was a waste of time and a study by IBM confirms this.
Yes, I have been assimilated by the Gorg!

Quick Version: GMail Filters for Finding Unlabeled Messages

This article has evolved a lot as Gmail evolved and I’ve tried to rationalize it a bit and get to the point (it was littered with dated “updates”). Here’s the short answer. If you want to understand what these filters mean, how we came up with them, or how to use them, scroll down for the original article which gives all the practical and theoretical background on custom Gmail filters.
All right already, show me the method!

Method 1 — Simple and mostly reliable

Still in June 2016 as I edit this, the best method currently seems to be to exclude anything that has custom labels or one of the standard built-in labels as follows (thanks Tony Franks for the comment from 2013-07-01):
-has:userlabels -in:sent -in:chat -in:draft -in:inbox
Ron Wolf suggests in the comments (Nov 29, 2016), to add -from:me to the search to avoid getting your own messages. I find these are the ones that often don’t get labelled and are the ones I’m searching for, but again, it all comes down to how you use Gmail.
You just enter these searches into the search box, click the Select All checkbox and then click the link to select all messages matching your search, as in the screenshot below (click to view full size):

Entering your search in Gmail to find unlabeled emails
You’ll notice that at the bottom of the screenshot, the last item is in the Inbox and should not get found. That’s part of a threaded conversation with multiple participants where people aren’t using Reply All. That seems to result in having Gmail see some of the subthreads with some participants as having been archived and meeting the search criteria. So it isn’t perfect.

Method 2 — Simple but not very reliable.

If that doesn’t work, you can try a method JonG posted on 2013-01-14. Unfortunately, that method seems to be flaky for many people and has never worked at all for me, but it does work for others:
has:nouserlabels -in:sent -in:chat -in:draft -in:inbox

Method 3 — highly reliable but a pain in the butt

My original method is more labor-intensive but at the time, the special “userlabels” pseud-label wasn’t available, so you had to laboriously build a search and save it for future use. This method is a pain in the neck, but works reliably precisely because it doesn’t really depend on any tags with special meaning. The key is that you can combine single-word labels in braces, but multi-word labels seem to need their own entry with the spaces replaces with dashes, like so:
-label:{label1 label2} -label:label-three -label:label-four -in:sent -in:chat -in:inbox
If you do this more than once, typing in all your labels in the arcane syntax Gmail uses gets old. So what I’ve done is simply create a shortcut, which you can do quite easily and it works up until you add a new label, but then it’s just a simple matter of editing the bookmark.

Method 4 — Desperate Measures

Finally, if all else fails, you can use this roundabout method from Federico (comment from 2013-06-08):
  • First search for every message that HAS a label (has:userlabels)
  • Label ALL of the results with a new “LABELED” label (or whatever you want)
  • Now search for has:nouserlabels
  • Ba Dum, you got them!
  • Delete your “LABELED” label.

Conversation Mode and labels

With all methods, Danimal suggests turning off conversation mode (threaded conversations) as that can make things confusing.
I like leaving conversation mode on because I generally want to catch only entire conversations with no labeled messages at all. If any message in the thread is labeled, that’s good enough because I’ll find the conversation via the label.
But if you want to find not unlabelled threads, but all unlabeled individual messages, then you need to do as Danimal says.

Details: Understanding GMail Filters

You have a full syntax and a compact syntax and, as far as I can tell, the compact syntax does not work with multi-word labels. So if you have Gmail labels with spaces in them, you have to use the full syntax and substitute hyphens for spaces.
So let’s say you have the following labels:
  1. Label1
  2. Label2
  3. Label Three
  4. Label Four
First, we want to exclude all messages that have those labels. To exclude a labeled message from your search, you use the -label: operator.
For the single-word labels, we’ll use the short syntax. This allows you to group terms within curly braces without repeating the “-label:” qualifier. So it looks like this in your Gmail search box
-label:{Label1 Label2}
Simple as that.

Multi-word labels are a bit more complex

Now for the multi-word labels, in theory, I merely need to add quotes around the terms, and they should work within the curly braces. Not so for me. If you create a filter and look at the test search, that’s not how it does it either. So based on that, what I found worked for Label Three and Label Four was:
-label:Label-Three -label:Label-Four
So the entire search, with both single-word labels and multi-word labels, looks like this
-label:{Label1 Label2} -label:Label-Three -label:Label-Four
Now, that will create a URL that looks like this
http://mail.google.com/mail/#search/-label%3A%7BLabel1+Label2%7D+-label%3ALabel-Three+-label%3ALabel-Four

Save your search for later use

Now you can save this as a bookmark or shortcut and instantly access your unlabeled Gmail messages. Sometimes Gmail will add a zx parameter to your URL that looks like zx=afeoasdxou3swf that is just a random string so that if your ISP is caching data, it will see this as a unique URL and won’t give you cached data for Gmail. Since this effectively creates a single-use URL, if that appears in your URL when you do your search, you should edit it out before saving the bookmark.
Note that if a message has two labels and you are only excluding one of those, the message will still show up in your search. So if you have something labeled Label1 and Label5, and you use the search above, it will still show up in your results.
Also, sometimes a conversation that is labeled shows up because one message is unlabeled or is still in the Inbox. If you select the whole conversation in the list view and label it, that takes care of that issue.

Additional Operators and Pseudo-Labels

As you saw above, there are additional operators (in and has) and special built-in pseudo-labels (userlabels).
Depending on what you are trying to do, you can shorten or refine your search by using in with any built-in label (inbox, trash, etc) and has with userlabels.
  • -in:inbox (don’t find anything if it’s still in the inbox)
  • in:trash (if you want to search the Gmail trash, you need a special operator because by default it’s excluded)
  • in:anywhere (lets you do a search that includes spam, trash, inbox and, well, messages found anywhere)
  • -has:userlabels should be the same as has:nouserlabels, but in practice people report that they are not.
There are tons more options. See the Google Gmail Label Documentation for a full and current list.

Labeling Your Backlog

As per Karen’s suggestion below (see comments), if you’re trying to identify your unlabelled email just once and label your backlog, then you can view All, apply a label like “NoLabel” to it (or move them all to the Inbox as Karen suggests).
Now go into every other label folder, select all and remove the “NoLabel” label (or Archive if you put them in the Inbox). Now if you go to the NoLabel folder, you have all your unlabelled email. If you’re going to do this on any kind of regular basis, though, you’ll want a bookmark as described above, otherwise this will be pretty time-consuming.

Dealing with Child Labels and Labels with Special Characters

James asks, what happens if you have special characters like underscores or slashes in your Gmail labels? If you are using the Gmail sublabel feature, you will automatically have slashes because Gmail separates parent and child labels with slashes (look at Gmail in the Basic HTML mode and you can readily see this). First off, most special characters are just entered as such. Slashes must be entered as hyphens.
So let’s say you have the following setup:
  • Main
    • test1
      • test2
    • test3/test4
    • test*,:-test-./test
In that case, your search syntax will be, respectively
  • -label:main
  • -label:main-test1-test2
  • -label:main-test3-test4
  • -label:main-test*,:-test-.-test
Note that a label called “test3/test4” which is a single label, behaves exactly the same as test2 which is a child label of test1. And for anything except slashes and spaces, which are both replaced by hyphens, you just use the character as it appears in the label. That’s even true for the colon, even though it’s part of the search syntax.

A Question for YOU (please read)

Did you really make it all the way to the end of that article? You have endurance and perseverance, which makes me think I can ask one little favor. Would you be so kind as to add a comment below or go to my contact page and tell me what trouble you have with email currently. What is your top email pain point? I’ve been giving a lot of thought to the topic over the last couple of years and am preparing some articles on the subject and would love to hear from you about what you would like help with or your best ideas for reducing the pain of email hell.
I guarantee that you will not get added to any mailing list and, in fact, your email address won’t even get saved except insofar as WordPress saves it with all comments and form submissions as a spam-fighting tool.