Is it possible to search using negation without excluding whole notes from results?

So I’ve been using Obsidian whilst playing Blue Prince and am trying to search all notes for certain words or letter combinations, but there are so many results that I tried negating some words to shorten the list. That worked a little too well though as I realised it was excluding whole notes from the list because of the negated words, even if the letter combinations I was searching for were in those notes. I’m probably not explaining it very well so an example would be that I want to search for all instances of “lu”, but I don’t want the results to show “blue” so I negate that, but then any notes that do contain “lu” are removed from the results list if they also contain “blue”, which is frustrating.

I’ve looked at the help docs and tried different combinations of search terms, but some of the rules and formatting read like code to my non-techy brain! There’s also so much one can do with Obsidian that it is overwhelming reading through all the documentation so apologies if I’m missing something obvious, or if I’m just being daft.

Thank you for in advance for any help or ideas (:

Hi @Anony26, welcome to the Obsidian community!

This is a tricky search, one that I’d solve by using regular expressions. Since you said you’re “non-techy”, they may not be a good fit for you, but here’s an example you might be able to customize for your needs.

A regular expression is a pattern that you enter into the Obsidian search bar that tells it exactly what sequence of letters you are (and aren’t) looking for.

In this case, you want to search for “lu” but not “blue”. We can do this by entering the following regular expression (including the slashes) into the Obsidian search bar:

/[^b]lu[^e]/

Here’s what it means:

  • /: begin the regular expression.
  • [^b]: Match any single letter except b.
  • lu: Match the letters “lu”.
  • [^e]: Match any single letter except e.
  • /: end the regular expression.

In my vault, this finds “Volumes”, “columns”, and “rollup”, but not “blue”.

Regular expressions are complex, but extremely powerful – this is just a taste of what they can do. You can read more about them in Obsidian’s help docs.

Hope this helps!

Craig

1 Like

Thank you, Craig (:

May I ask what makes it a tricky search?

You explained that so well and clearly that I got excited and started delving into regex…..until my mind got boggled by it all - complex indeed :sweat_smile: So I hope it’s okay if I ask you a follow-up question: is it possible to perform the search for multiple instances at the same time? E.g. if I wanted to exclude “volume” and “rollup” as well as “blue”, but keep “columns”.

It’s tricky because you’re trying to incorporate both a positive search (“lu”) and a negative search (“blue”) at the page level, so it’s easy to accidentally remove pages you want from your search. Even with regular expressions, adding more exclusions gets complicated quickly.

However, your question reminded me about Obsidian’s line search option. This isn’t as powerful as regular expressions, but it’s much easier to use and might be a good fit for what you’re trying to do.

The line option tells Obsidian to restrict its searches to individual lines in your vault, rather than entire pages. So a similar search to your original query would be:

line:lu -line:blue

This means:

  • line:lu: Find all lines containing the letters “lu”.
  • -line:blue: But filter out lines containing the letters “blue”.

In my vault, this gives me a similar result as our regular expression:

And it’s easy to exclude “volume” and “rollup”:

line:lu -line:blue -line:volume -line:rollup

I recommend trying the line option out to see if it meets your needs.

I hope this helps!

Craig

If you want to search a term only as a whole word and not as part of another word, you can use quotes: "lu". Otherwise, regular expressions as suggested above can help.

It is unfortunately not possible to search using negation without excluding whole notes from results (unless there is a plugin for it) because Obsidian’s unit of search result is whole notes. The search previews that show each match make this easy to miss. There may be a feature request for more granular search. I believe there is a request for filtering search results.

That’s not what it does. It finds

files that contain at least one line matching [the search term that follows it]. (Search - Obsidian Help)

So it’s mainly for finding things that are in the same line together, which is sometimes a useful way to narrow a search.

line:lu -line:blue should produce the same results as lu -blue. I tested line:Asdfghjkl -line:zxcvbnm and it did not find a file containing Asdfghjkl and zxcvbnm in separate paragraphs.

Hmm, then is there perhaps a way to filter the results of a postive only search if they were somehow collated somewhere?

Unless I’m being a muppet, which is entirely possible, I don’t think the line option is working as it still seems to be excluding whole notes even when “blue” is a couple paragraphs away from a “lu”.

Thank you for your speedy reply, and apologies for the trouble my question is causing!

Unfortunately most of the time “lu” is part of another word.

Ah okay, that’s a shame, but makes sense. I’m glad the feature request means I’m not just being daft and missing something obvious (at least in this instance) :joy:

“line:lu -line:blue” and “lu -blue” didn’t have the same number of results when I searched - it looks like the latter also included note names, but perhaps that’s the expected behaviour?

Oof, that is tricky. Even with regular expressions I’m not sure there’s a way to exclude only “blue” without excluding some other words (for example any containing “blue”). It might be possible, or you might have to look at plugins, or use another app when you do this type of search.

Your idea of getting the search results into a file and then working with that could be useful, too. As a rough idea you might do that, then replace unwanted words with nothing (or placeholders), and search what’s left. Not the smoothest, but it could get the job done.

Sounds plausible. Line: would definitely not include them.

Aw, bummer. Thank you for the correction, @CawlinTeffid!

Sorry for leading you astray, @Anony26.

Do you think it could work to define the characters either side of “blue” as whitespace, and therefore it can’t be contained in another word? As for doing the search in another app, did you have one in mind?

Ooh okay, though I don’t have the foggiest of ideas how I would go about getting the search results into a file :sweat_smile:

Good to know, ta.

No apology necessary - I lead myself astray and get things confused even without anyone’s help! So all good (:

That’s a good thought. You’d also probably want to account for punctuation, but I think there may be a way to indicate “word boundary” in a regular expression, which would handle your core idea without worrying about details like spaces and punctuation.

Any text editor that can search with regular expressions across files. VS Code is popular on desktop. There is BBEdit on Mac, IA Writer on various platforms, and probably others. Editors meant for writing code may be more likely to have the feature.

I’m not sure either. If I remember right you can drag results into a note but if so I think it just links to the notes. Actually, you could do that and then use something like the Easy Bake plugin to convert the links to copies of the text in the notes. I’m not sure if you can select multiple search results at once tho; doing it 1 by 1 might be, well, a drag. If you can do it, it would be by using a modifier key like shift or alt or ctrl/cmd (one of those opens notes in a new tab).

1 Like

Good point, and that would be handy, though I’m guessing it might not help with the issue of wanting to exclude multiple words at the same time?

Thank you for the editor suggestions (: Do you happen to know whether their units of search result are not just single files like Obsidian’s? No worries if not though.

I can’t seem to drag the results bit unfortunately, only the note name which indeed just links to the note, and therefore I’d be baking, I mean making, a meganote of all my notes! Unless I’ve misunderstood the idea, in which case please do correct me.

Thank you muchly for your continued time and help. And apologies for the delay in replying, life got busy.

I’ve realised I didn’t actually tag you in my reply above :sweat_smile: But this isn’t a nudge, especially since my last message was delayed - just figured it couldn’t hurt to actually reply in case you hadn’t seen it because of my mistake. Hope that’s okay (: