Have Obsidian be the handler of .md files / Add ability to use Obsidian as a markdown editor on files outside vault (file association)

A standalone Obsidian markdown editor would be excellent, naysayers be damned. +1

4 Likes

uh since my request got merged into this one, Iā€™m going to clarify that opening ANY markdown file even files within the vault folderā€¦ does not open that markdown file using GNOME. It just opens obsidian in whatever prior context it was regardless of the file selected.

simply opening a new tab of the markdown requested would be better behavior.

2 Likes

This is a very fascinating discussion. I think obsidian should eventually implement this natively, where if it can resolve the file to a vault, it opens the file within the vault, else it opens in a vault-less mode that allows editing or previewing of the individual file. Code editors like sublime and vscode have ā€œprojectā€ views and ā€œfileā€ views, and are smart enough to display one or the other depending on what you open in them.

In obsidianā€™s defense, code editors generally allow you to configure plugins system wide (and thus they are available while editing individual files) whereas obsidianā€™s plugins are on a per-vault basis. Users with lots of plugins would get a severely limited experience when opening a vaultless file, since there is no global or default plugin configuration for obsidian to fall back to. This makes this request a unique user experience problem that doesnā€™t have a perfectly analogous solution that the obsidian developers can follow.

In the meantime: I really liked some of the above suggestions. On macOS a separate ā€œmarkdown openerā€ application that could be used as the handler for all md files, or a quick action that could be used as an alternate to opening the markdown file seem like the next best solution. As others have stated the logic in either case would be (1) if the note is part of an obsidian vault, open with obsidian url handler or (2) open with a default editor if not

I figured out a way to create an application on macOS that can do exactly this, now when I double click a markdown file it will either open in obsidian or sublime text. Instructions for Sublime text below. If you wish to use another application (such as TextEdit), just replace the ā€œSublime Textā€ with the name of the text editor you want to use before saving your Automator application.

Instructions

  1. Open Automator:
    • Navigate to your Applications folder and open Automator.
  2. Create a New Application:
    • In Automator, choose to create a new Application.
  3. Add a Run Shell Script Action:
    • In the search bar at the top left, type ā€œRun Shell Scriptā€ to find the action.
    • Drag the ā€œRun Shell Scriptā€ action to the workflow area on the right.
  4. Configure the Shell Script Action:
    • In the ā€œRun Shell Scriptā€ action, youā€™ll see a text area where you can write your bash script.
    • Set the Shell to /bin/bash.
    • Set Pass input to as arguments.
    • Copy and paste the below bash script into the text area provided.
# For each file that we are passed
for f in "$@"
do
    # start at the folder the file is in
    dir=$(dirname "$f")
    # while we are not at the root of the hard drive
    while [ "$dir" != "/" ]; do
        # check to see if we have reached an obsidian vault
        if [ -d "$dir/.obsidian" ]; then
            # If we have, open in obsidian
            open "obsidian://open?vault=$(basename "$dir")&file=${f#$dir/}"
            exit
        fi
        # go up one folder to se if we are in an obsidian vault
        dir=$(dirname "$dir")
    done
    # if we get this far, then we reached the root of the hard drive, and did not find an obsidian vault
    # Fallback to Sublime Text
    open -a "Sublime Text" "$f"
done
  1. Save the Automator Application:
    • Go to File > Save or press Command-S.
    • Save the Automator Application in your Applications folder with a suitable name (e.g., MarkdownHandler.app).
  2. Set the Automator Application as the Default Handler:
    • Find a .md file in Finder and right-click it.
    • Choose Get Info.
    • Expand the Open with: section.
    • Click the dropdown and choose Other....
    • Navigate to and select your Automator Application (MarkdownHandler.app).
    • Click Add.
    • Click Change All... to set this application as the default for all .md files.

Now, whenever you open a markdown file, it will be processed by the inline script in your Automator Application and opened in the fileā€™s containing obsidian vault whenever possible, or your fallback application otherwise.

11 Likes

Fallback editors Iā€™ve tested the above script with:

- Sublime Text
- TextEdit
- iA Writer
- Visual Studio Code
2 Likes

You sir, are the grand master! :raised_hands:
Works like a charm and I truly appreciate you sharing this.
I donā€™t know if something like this could be used by the developers of Obsidian to workaround the issues some people mentioned here. Iā€™m not a developer myself, so whatever others were trying to explain on why Obsidian canā€™t open the .md files, goes over my head 100%.

Anyway, just wanted to thank you for sharing this :muscle:

The .md files now have a different icon based on the MarkdownHandler.app
image

I was able to change this by changing the .icns file inside the MarkdownHandler.app (Right click > Show Package Contents > Contents > Resources > AutomatorApplet.icns).
I delete that file and copied the one inside Obsidian.app and put it in that same Resources folder.
I kept the name ā€œAutomatorApplet.icnsā€ just in case, because I donā€™t know if that would make a difference or not.
And now all my files open using your script, and have the Obsidian icon:
image

image

:slight_smile:

3 Likes

Thereā€™s a feature request to rectify that here: Global Settings / Same settings, themes, and plugins across multiple vaults

Without that, or at least an option to choose a primary vault and use its settings and plugins, the ability to use Obsidian as a markdown editor for files system-wide would be almost pointless.

2 Likes

Thank you! Thank you! Thank you!

I was looking for something exactly like this and I will promote it.

And it works with MarkEdit ( GitHub - MarkEdit-app/MarkEdit: Just like TextEdit on Mac but dedicated to Markdown. ) as well.

It works perfectly out-of-the-box on Windows 11. Thanks!

1 Like

oh, great!
We could probably make a mac OS shortcut out of it.
It would be even easier to set up: just a download link and installā€¦

not sure I can make it myself, but I might give it a try in the next daysā€¦

1 Like

I think it might be possible to solve it this way.

First Iā€™m assuming that all the features in Obsidian are modularized and that itā€™s relatively simple to set a feature to be enabled and disabled.

Next, the Vault configuration file could be placed in a fixed path, for example in My Documents, or in a path specified by the user.

Then, add a new entry point for Obsidian, such as ā€œOpen file in standalone modeā€.

When starting Obsidian from this entry, Obsidian will quit reading the .obsidian folder in other directories, and instead read the contents from the path mentioned above.

At this point, Obsidian can disable all plugins and functions related to Vault features.

Next, itā€™s just a matter of reading and saving the specified markdown file.

I think this might be a simpler way to implement this.

1 Like

Thank you a lot! Thatā€™s exactly what I wanted! Itā€™s perfect and simple. :fire:

Iā€™m on Linux. I use GitHub Desktop and git-cola (diffing my files).
I wanted to be able to quickly open the given file in Obsidian where I have tons of customized goodies for editing (Templater Js scripts, hotkey combos etc., while I donā€™t have those in other editors such as Sublime Text).

Enter obsidian-cli, which works on all platforms.
Iā€™ll just put here a how-to on Linux, but itā€™s a similar setup for other platforms.

Add in your file manager the default markdown opener a shell file, which should contain (provided you installed with brew):

#!/bin/bash

# Full path to the obs command
OBS_COMMAND="/home/linuxbrew/.linuxbrew/bin/obs"

# Check if a filename is provided as an argument
if [ $# -eq 0 ]; then
    echo "Usage: $0 <markdown_file>"
    exit 1
fi

# Get the filename from the first argument
markdown_file="$1"

# Extract the note name from the full file path and remove the leading directory path and the ".md" extension
note_name=$(basename "$markdown_file" .md)

# Open the Markdown file in Obsidian
$OBS_COMMAND open "$note_name"
  • Save the file with .sh extension and make it executable (e.g. chmod +x open_markdown_in_obsidian.sh). You can put this file in a folder in your vault so it will be synced and not lost on a hard drive crash.

The experience is really good. The file opens faster than if I want to open a file inside Obsidian.
The cli has much more to give than just open files, of course. Itā€™s an alternative to Obsidian URI.

1 Like

+1 for this!

3 Likes

Would love to be able to open Obsidian files from Finder or iPhone search and for that file to be opened in the correct vault. +1

1 Like

Loving Obsidian (and Typora for standalone files).
So Iā€™m using a simple-stupid approach (windows - not perfect. only single vault and no pretty icons).

Create a file called obsition-typora.bat (code below ā†’ requires customization). Then right click your .md-file and open with ā†’ obsition-typora.bat. This automatically opens files in Obsidian if they are in VAULT_PATH, and uses ALT_EDITOR for everything else.

:: open a file either in obsidian or an alternative editor (based on path)
:: https://stackoverflow.com/questions/10218451/how-can-i-url-encode-spaces-in-an-nt-batch-file
:: Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time
setlocal enableDelayedExpansion

:: the path with your obsidian notes. Change this to your own path
set "VAULT_PATH=Nextcloud\Notes"
:: the path to your alternative editor. Change this to your own path
set "ALT_EDITOR=C:\foobar\Typora.exe"

set "URLPATH=%*"

:: replace spaces with %20
set "URLPATH=!URLPATH: =%%20!"

ECHO.%URLPATH%| findstr /C:%VAULT_PATH%>Nul && ( 
  Echo.Found %VAULT_PATH%
  start explorer "obsidian://open?path=!URLPATH!"
) || (
  Echo.Did not find %VAULT_PATH%
  start %ALT_EDITOR% !URLPATH!
)

:: use pause for debugging
:: pause

Thatā€™s an interesting workaround, but I still find Obsidian notably superior to Typora and other apps as a markdown editor, and wish I could use it on .md files located anywhere on my computer.

1 Like

I really resonate with the frustration shared here about Obsidian not handling .md files outside of vaults. It feels so limiting when I canā€™t quickly edit files that arenā€™t in my vault or open ones that are without jumping through extra hoops. Itā€™s such a basic need for anyone working with Markdown regularly, and it would make a world of difference in our workflows. Hereā€™s hoping this gets prioritized soon!

2 Likes