Command Line Interface to open folders (and files) in Obsidian from the terminal

Edit: it works even when Obsidian is closed, by not until the Vault is added trough Vault manager.

Not sure if someone already shared how to do this on Linux, but this works for me on Linux Mint XFCE at least:

open "obsidian://<path>"

So, for example:

open "obsidian:///home/myself/journal"

The more complex examples above did not work for me.

I made this work using a simple bash script:

#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Usage: $0 <vault_name>"
    exit 1
fi

vault_name="$1"
encoded_name=$(printf '%s' "$vault_name" | jq -sRr @uri)

nohup xdg-open "obsidian://open?vault=$encoded_name" >/dev/null 2>&1 &
1 Like

Use case or problem

I would like a vault to be able to open from the command line. For example if I didn’t have the vault in a given computer, and it was freshly copied from another place, then the vault isn’t already in the cache or config of Obsidian.

Here are the suggestions I found to open via the command line (this needs the URL for Obsidian application produced in advance, which is not possible in my case).

How to open vault selector from command line on windows 10/11 - Help - Obsidian Forum

Can I open Obsidian from command line like VSCode? - Help - Obsidian Forum

Proposed solution

Ideally be able to open a folder as a blank vault, or as the vault as if it was opened through ā€œFileā€ > ā€œOpen vaultā€¦ā€ option.

This way Obsidian will be super useful for Markdown documentation management for various scenarios. Right now I use BBEdit, but it doesn’t manage the links well as the Obsidian does.

So please enable a folder to be opened as a Vault using Obsidian.


My findings:
ā€œLibrary/Application Support/obsidian/obsidian.jsonā€ seem to have the entries to the vaults, and if I update it by adding new vault’s location entry (eg: ,"fdc0b3a4e945872e":{"path":"/absolutePathTo/MyNewVault","ts":1720229748200,"open":true}) which I want to open while Obsidian is not open, then I can use the commandline open "obsidian://open?vault=MyNewVault". But drawback here is Obsidian has to be offline while doing this. Moreover I don’t want to meddle with obsidian configuration manually as it may cause many other issues as many would agree.

2 Likes

Problem

Right now to open a vault you need to: press the button ā€œOpen another vaultā€ (left inferior corner), then ā€œOpen folder as a vaultā€, navigate to the folder path and select the folder (4 steps).

Solution

It would be much easier navigate to your folder path with the file manager app or the terminal and open it:

  1. just click right button and select open with Obsidian
  2. open via terminal like Visual Studio Code code . (open the current directory)
5 Likes

Did you find any solution?

This is kind of how I want to use Obsidian also… as a markdown focused view on top of my repositories. This would go hand in hand with needing to blacklist and/or whitelist directories from obsidian’s content graph & index.

Have another issue about it here: https://forum.obsidian.md/t/actually-excluding-paths-from-obsidian-entirely/48060/3

hi it can help you

Oh thanks, I’m on Linux with KDE and I can’t do that (right click on the Obsidian icon and see the vaults).

But I found a better way using a similar method.

Just create a bookmark in your browser with the protocol Obsidian (obsidian://) on the directory path like user V.

You can save the link as a bookmark by typing in the name and url or by Ctrl+D once it is open. In this case:

  • the name is: User V
  • the url: obsidian:///Knowledge/PKM/User-V/

Once done, just click on the link and it will open the Obsidian vault. This is not a way to use the terminal, but you can save it and use the (obsidian://) protocol.

From terminal you can open it with:

$ xdg-open obsidian:///0/Knowledge/PKM/User-V/
# or
$ firefox obsidian:///0/Knowledge/PKM/User-V/

but I get some warnings with electron and gtk

The method with the URL generally works, but not in my case;
I use Obsidian inside of a container and therefore need to start it with the console parameter --no-sandbox, therefore a command like obsidian [config parameters] --vault ~/Documents/obs-vault would be nice to have

I would like when I create a vault to automatically create a file inside the folder that launches obsidian with the vault itself.
Similar to when in Visual Studio I open a sln.

It would take a command line argument to open the vault in the current folder, so I could create a link in the root folder of a vault, to open it. I was able to create a shortcut (although it doesn’t work from cmd) with obsidian:///full_path_folder but I would have to create a new (different) one for each vault and I would have to modify it if I moved it.

https://yakitrak.github.io/obs/

Hey hi, you can right click the folder and select open with obsidian or use a terminal command like code similar to Visual Studio Code.

The simplest solution I found - Mac only, sorry - was Hookmark; select the folder, H, select hook to new, select obsidian, done.

Or applescript, which I use with BetterTouchTool but would work with Alfred, Raycast, etc:


tell application "Finder"
	
	if selection is not {} then -- Check if something is selected
		set selectedFolder to item 1 of (get selection) as alias -- Get the selected item
		set folderName to name of selectedFolder
		-- display dialog folderName -- Display the folder name
	else
		display dialog "No folder is selected."
	end if
	
end tell

do shell script "open  obsidian://open?vault=" & folderName


Thanks, for this. I ended up adjusting it a bit to fit my needs. You can pass any compatible file into this script and it should open in your vault.

#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Usage: $0 <file>"
    exit 1
fi

DEFAULT_VAULT_NAME="default"

if ! [ -z "$2" ]; then
    VAULT_NAME=$2
else
    VAULT_NAME=${DEFAULT_VAULT_NAME}
fi

FILE="${1##*/}" # delete everything up to including the last "/"

ENCODED_FILE_NAME=$(printf '%s' "${FILE}" | jq -sRr @uri)
ENCODED_VAULT_NAME=$(printf '%s' "${VAULT_NAME}" | jq -sRr @uri)

OBS_URI="obsidian://open?vault=${ENCODED_VAULT_NAME}&file=${ENCODED_FILE_NAME}"

nohup xdg-open "${OBS_URI}" >/dev/null 2>&1 &

Problem Summary/History

I was using this method which I discussed in this forum a while back. That means to create a link to the vault, and the open that vault through the command line whenever possible.

However, it is been sufficient time for me to experience this and now I see the need for the ability to open any folder through the terminal using Obsidian.

I vote for this change +1 any amount of times now. So team, can we prioritise this please? Initial version could just open a folder, create and .obsidian config with default parameters as it likes to help the default environment setup for it. No need of extra config/plugins etc.

What I am after is the ability to super simply look at the Markdown documentation, simplify image attachment through clipboard, have my way of folder naming convention (am happy to do it after opening the folder myself).

Current functionality is that if a given folder was already opened as a vault, then it would work with command

open "obsidian://"`pwd`

But otherwise you would get an error that says "Vault not found.".

Thank you team, for prioritising this!

Note, there seems to be a few threads around this topic. I’m listing them below for the reference:

  1. Can I open Obsidian from command line like VSCode? - Help - Obsidian Forum
    https://forum.obsidian.md/t/can-i-open-obsidian-from-command-line-like-vscode/65042/8

  2. Open folders as vaults from the CLI - Feature archive - Obsidian Forum
    https://forum.obsidian.md/t/open-folders-as-vaults-from-the-cli/89714/2

  3. Open current vault or folder in terminal - Plugins ideas - Obsidian Forum
    https://forum.obsidian.md/t/open-current-vault-or-folder-in-terminal/41818

Suggestion

When Obsidian is opened from the command line, if the given folder structure doesn’t have an already registered vault, just create one, update the registry and open the given folder as a vault.

If there’re any objections to this that say the user may mistakenly send a folder that actually isn’t a vault or something, a prompt to verify asking this is not an existing vault, are you sure you want to open yes/no would do.

In Electron JS, there seem to be a possibility to get this done. Here’re some suggestions from ChatGPT.

Electron Question - ChatGpt.zip (148.7 KB)

1 Like

I’ve wanted something like this for a while too. I started tinkering with a wrapper script in bash that would:

  1. check current and parent directories for a .obsidian folder indicating that it’s already a vault, and if so, just open that (or if a filename was specified instead of a directory, open that file within the detected vault). Otherwise…
  2. check a (my own) config for a reference to a ā€œtemplate vaultā€, which is a vault configured the way I want any directory I open with this to look (i.e. plugins, appearance, etc)
  3. create a temporary .obsidian directory in the user’s current directory and copy the relevant files from the template vault, making the current directory a vault with a proper config.
  4. create a temporary obsidian application config that knows about this temporary vault
  5. lauch obsidian with an overridden XDG_CONFIG_HOME containing my temporary obsidian config
  6. watch the launched obsidian process and clean up temporary files and folders when it exits

This approach is very dependent on a linux (or at least bash) setup so it’s of no use to anyone on other platforms, and there are a bunch of moving parts and steps, each of which could fail for multiple reasons and leave things in a weird state. So while I did get some proof of concept work done, that’s as far as I took it.

I also considered copying or symlinking other files from my ā€œtemplate vaultā€, e.g. so that the temporary vault could run templater and other plugins with the files they might need. Never even started that though.

My experiments were a while ago so perhaps today a lot could be done by throwing the problem at chatgpt or claude. But it sure would be nice to have something like this built-in to Obsidian and without all the jank I’m going through!

In Mac, if I use a folder with open "obsidian://open?vault=$VAULT_NAME"

I get the ā€œUnable to find a vault for the URLā€

If I did open -a "Obsidian" "$FOLDER_PATH", I don’t get any error, nor it would open the vault either.

Any suggestions there @mlamb ?

Okay I got a script to work. This will close the running Obsidian, then update the config to include the supplied folder and reopen passing the supplied folder.

Let’s save this script with the name open_as_obsidian_vault.sh. Once you’ve added this to the path, call as follows to open the current folder (or change to a path as you prefer):

open_as_obsidian_vault.sh `pwd`

open_as_obsidian_vault.sh code as follows:

#!/bin/bash

# Close Obsidian if it's running, because we need to update config file
osascript -e 'quit app "Obsidian"'
sleep 0.2  # Small delay to ensure it fully closes

# Get the Obsidian configuration path (default for macOS)
OBSIDIAN_CONFIG="$HOME/Library/Application Support/obsidian/obsidian.json"

# Check if a folder is provided
if [ -z "$1" ]; then
    echo "Usage: $0 /path/to/folder"
    exit 1
fi

# Resolve absolute path of the vault folder
FOLDER_PATH=$(cd "$1" && pwd)

# Ensure the folder exists
mkdir -p "$FOLDER_PATH"

# Initialize as an Obsidian vault if not already
if [ ! -d "$FOLDER_PATH/.obsidian" ]; then
    mkdir "$FOLDER_PATH/.obsidian"
    echo "Initialized new Obsidian vault at: $FOLDER_PATH"
fi

# Ensure Obsidian configuration file exists
if [ ! -f "$OBSIDIAN_CONFIG" ]; then
    echo '{}' > "$OBSIDIAN_CONFIG"
fi

# Add the new vault to Obsidian's known vaults
python3 - <<EOF
import json
import os
import hashlib
import time

config_path = "$OBSIDIAN_CONFIG"
vault_path = "$FOLDER_PATH"

try:
    # Generate a unique ID for the vault (hash of the path)
    vault_id = hashlib.md5(vault_path.encode()).hexdigest()[:16]

    # Read existing config
    with open(config_path, "r+") as f:
        config = json.load(f)

        # Ensure "vaults" section exists
        config.setdefault("vaults", {})

        # Add new vault entry if it doesn't exist
        if vault_id not in config["vaults"]:
            config["vaults"][vault_id] = {
                "path": vault_path,
                "ts": int(time.time() * 1000),  # Current timestamp in milliseconds
                "open": True
            }

            # Write updated config back
            f.seek(0)
            json.dump(config, f, indent=2)
            f.truncate()

            print(f"Added vault '{vault_path}' to Obsidian configuration.")

except Exception as e:
    print(f"Error updating config: {e}")
EOF

# Open Obsidian with the new vault
open -a "Obsidian" "$FOLDER_PATH"

It goes without saying I have tested only in my Macbook, so use with care. Take a backup of the ~/Library/Application Support/obsidian/obsidian.json file before you run this too, just in case.

Here’s the Github, if there’re any updates you like to suggest: GitHub - madukan/obsidian_tools: Tools to help Obsidian

Enjoy!

Thanks for this Mzee. I wrote a similar thing to use on Linux a while back. I haven’t posted it because it seems insane to me that I have to kill an existing Obsidian process for something as simple as opening a folder as a vault. It feels broken, as does having to install 3rd party tools (even if I’ve written them) for such a trivial feature.