Searching Obsidian through Gnome

After I started using Obsidian and Gnome

I recently started using Obsidian as my main notes app, and Linux with Gnome as my main OS. One of my favourite things about Gnome is the unified “search” mode it enters when you hit the “super” key (what Linux calls the Windows key) — from here you can type the names of apps, unicode characters, contacts, or files, you can do calculations, search the web, install new software… just about anything your computer can do, you can do through Gnome search.

This is all very nice, except that if I search for something in my Obsidian vault, it opens the note in the Gnome default code editor rather than Obsidian. This isn’t bad. In many ways it’s great — because Obsidian stores its data just as Markdown files, Gnome can index them and they open just fine in Code. This happens out of the box, and it’s fine, and nobody wrote a single line of code to make it happen, it’s just the result of everyone supporting a few reasonable standards.

But I wanted them to open in Obsidian — and I wanted other Markdown files I might have on my system to open in Code as normal. And the other great thing about everyone using reasonable standards is that if we do write a few lines of code, we can make that happen quite easily. Here’s how. You’ll need to tweak a few details to get it running on your system but I think it should be easy enough.

First, create a bash file. I put mine at ~/bin/md-opener.sh:

# it gets passed in with quotes, delete them and normalise the path
path="$(realpath "${1:1:-1}")";

# Update this to the path to your Obsidian vault:
if [[ $path = /home/andrew/Documents/Obsidian/* ]] ;
then
	# Update the 31 here to the *length* of the path to your Obsidian vault, not including the trailing slash:
	vaultPath="${path:31}";
	escaped=${vaultPath//[ ]/%20};
	# Update this to the name of your vault (mine is just called "Obsidian"):
	xdg-open "obsidian://open?vault=Obsidian&file=${escaped}";
else
	# Update this if you want to open non-Obsidian Markdown files in VS Code or what have you.
	io.elementary.code "$path";
fi

Remember to make the file executable — for me that was chmod +x bin/md-opener.sh.

Next, you need to get the file into your “open with” menu. I used Main Menu, which you can probably install from the Software app depending on your distro, because life is too short for messing about with desktop files. Create a new launcher, and enter md-opener.sh "%F" in the “command” box. Give it any name, description and icon you like. If you set it to hide from the menu it’ll vanish from the list in Main Menu as well, you can show hidden entries from the hamburger menu.

Screenshot of Main Menu showing the settings I used

Lastly, associate this new “app” you created with Markdown files. Open Files, find a Markdown file, right-click, and select “open with”. Choose your Markdown Opener entry, and turn on “always use for this file type”.

Screenshot of Open With showing the settings I used

From now on, if you open a Markdown file via Files or via Gnome search, then it’ll open in Obsidian if it’s in your Obsidian vault, and Code otherwise.