Command Line Interface to open files/folders in Obsidian from the terminal

Just ping me if you’ll ever (or someone, maybe me?) will do that thing! That we’ll be useful because I’m active user of Keypirinha.

1 Like

Will do. It’s certainly something I will look into as soon as Obsidian is capable of it. :slight_smile:

Thanks for creating imdone :pray: :pray: :pray:

I just started using it and need exactly this!

1 Like

I’d love this feature.

+1

(And some other random characters)

This feature is key to integrate Obsidian with all kinds of automated flows, and have it as an option for replacing current note editors.

Two notes:

  1. Being able to use the existing instance of the app is the key to make this useful. In some editors this is a configurable behavior that’s enabled by default.
  2. A nice bonus would be to optionally specify a line to jump to, i.e. obsidian file.md +155 would open file.md at line 155 (this is the syntax gedit uses).

For example, obsidian file.md +155 can have the following behaviors:

  • If there’s no open instance of Obsidian, launch the app and open file.md when the cursor is at line 155.
  • If there’s an open instance of Obsidian and this file is open, jump to where this file is open and go to line 155.
  • If there’s an open instance of Obsidian and the file is not open, open it and go to that line.
5 Likes

In the spirit of accessing, linking and automating externally, this is also related: URL Scheme

I also think any discussion in either of these feature requests could inform the other.

2 Likes

Perhaps a couple of examples might illustrate why CLI support for opening files & vaults is valuable.

How would this feature change how you use Obsidian?

For me, I would like to use some keyboard bindings to open certain vaults, which uses terminal command line to launch those obsidian with the select vaults. I have multiple vaults: Zettelkasten, Notes, and Journal. It would really nice to have this feature implemented as it will reduce the redundant work of selecting the right vault manually from the UI. Thanks!

1 Like

I was imagining having indexes living in excel spreadsheets. Use python scripts and/or manual work to generate spreadsheets with custom columns, formatting, and hyperlinks which go to specific markdown files. Click on hyperlink, opens with default app for .md filetype, opens in current Obsidian instance.

Alright, here’s a hack that would work:

  1. create a wrapper script/executable to use as command line opener for obsidian
  2. (optional) associate the script/executable with OS default app for .md files
  3. assume the script takes the desired file to open as an argument
  4. have the script killall obsidian instances (could be more sophisticated…)
  5. have the script edit $vault/.OBSIDIAN/workspace and set "file": "the_file.md" under “main” (most obvious for this POC anyway; and maybe another cmdline argument could be path to $vault so it only kills and modifies that one vault)
  6. have the script restart obsidian

Hopefully the whole thing takes no more than a second.

I tried manually doing this without writing a wrapper. Tried skipping step 4, or not killing obsidian, but the UI didn’t update simply after saving changes to the workspace file. Restarting obsidian was required

You’re welcome!

Personally, this would make it much easier for me to customize how I navigate my computer. (Windows user.) I use autohotkey, so this would let me whip up an interface to go to any file on my computer that I want to edit, in the app I want to use for editing it:

  • VS Code for code
  • Obsidian for notes
  • [undetermined] for doodles and drawings
  • A DAW for music
  • etc.

This would make Obsidian more integrated with the whole flow of using my computer, making it an element of the whole software instead of its own isolated/siloed app.

That’s the significance of this:

1 Like

URL Scheme was implemented on October 30, 2020.

This provides a command line interface, via the terminal open command.

For example, I have custom QMK Firmware on my keyboard, giving me a keystroke that triggers an Alfred workflow to open today’s scratch file in Obsidian. The relevant code is a Bash script:

proj='/Volumes/All/Global/Log'
obsidian='obsidian://open?vault=Log&file='

path=$( date "+${proj}/%Y/%m/%d" )
file=$( date "+${path}/%d.md")
url=$( date "+${obsidian}%Y%%2F%m%%2F%d%%2F%d")

mkdir -p "${path}"
touch "${file}"

if ! pgrep Obsidian >/dev/null
then
  open -a 'Obsidian'
  sleep 1
fi
open "${url}"

Obsidian needs to be already open to honor a specific page request, hence the double-clutching in the script.

13 Likes

Love this feature! Any thoughts about adding a line param to the interface? I like being able to jump to the specific spot I leave an imdone card.

Is it possible to open files from imdone in obsidian now? If yes cann somebody share a screenshot of the configuration pleas :slight_smile:

1 Like

Yes, download version 1.8.2.

1 Like

Now that Obsidian 0.11.0 adds custom URI actions, here’s a simple plugin that adds an open-with-line URI, which opens a file (in the current vault and relative to it) with a line number parameter.
That’s a feature that I needed for a long time, so here it is in case others find it useful.

import { App, Plugin, ObsidianProtocolData, MarkdownView } from 'obsidian';

const actionName = 'open-with-line';

export default class SuperUriPlugin extends Plugin {
	onload() {
		this.registerObsidianProtocolHandler(actionName, (params: ObsidianProtocolData) => {
			if (params.action == actionName) {
				if (params.file) {
					this.app.workspace.openLinkText('', params.file).then(() => {
						var cmEditor = this.getEditor();
						if (params.line) {
							cmEditor.setCursor(parseInt(params.line), 0);
						}
						cmEditor.focus();
					});
				} else {
					console.log('No path specified for open-with-line')
				}
			}
		})
	}

	onunload() {
	}

	getEditor() {
		var view = this.app.workspace.activeLeaf.view;
		if (view.getViewType() == 'markdown') {
			var markdownView = view as MarkdownView;
			var cmEditor = markdownView.sourceMode.cmEditor;
			return cmEditor;
		}
		return null;
	}
}

6 Likes

Is it fair to say this feature request has now been implemented? Or is there more to it?

The current implementation works for me. (I use the CLI daily to open files from KeyPirinha.)

@ mrjackphil

FYI I’ve setup Keypirinha to index my Obsidian vault. Each individual .md file is added to the Keypirinha index. (Letting me open files by name from Keypirinha.)

I used the files catalogue plugin:

[profile/Obsidian]
activate = yes
include_files = yes
include_dirs = no
paths = 
    D:\Obsidian\MyVault\*
file_item_label = Obsidian: {title}
python_callback = obsidian_callback

Add the obsidian _callback function is defined as:

# \Keypirinha\portable\Profile\Packages\FilesCatalog\filescatalog_user_callbacks.py

import keypirinha as kp
import os

def obsidian_callback(entry, profile, plugin):
  """
  *entry* is a `_globex.GlobExEntry` object, which is an improved version of
  `os.DirEntry`.

  *profile* is a `namedtuple` defined in :file:`filescatalog.py` as
  ``ScanProfile``.

  *plugin* is the `FilesCatalog` plugin object itself.
  """

  if not profile.include_hidden and entry.is_hidden():
      return None
  if not profile.include_dirs and entry.is_dir():
      return None
  if not profile.include_files and not entry.is_dir():
      return None

  include = profile.filters_default
  for filter in profile.filters:
      if filter.match(entry):
          include = filter.inclusive
          break
  if not include:
      return None

  if entry.is_dir():
      item_label_tmpl = profile.dir_item_label
      item_desc_tmpl = profile.dir_item_desc
  else:
      item_label_tmpl = profile.file_item_label
      item_desc_tmpl = profile.file_item_desc

  formatter = _LazyItemLabelFormatter(entry, profile, plugin)
  item_label = formatter.format(item_label_tmpl, fallback=entry.name)
  item_desc = formatter.format(item_desc_tmpl, fallback="")

  note_name = f'{entry.path}'.split('\\')[-1]
  vault = f'{entry.path}'.split('\\')[-2]

  return plugin.create_item(
      category=kp.ItemCategory.URL,
      label=item_label,
      short_desc=f'obsidian://vault/{vault}/{note_name}',
      target=f'obsidian://vault/{vault}/{note_name}',
      args_hint=kp.ItemArgsHint.ACCEPTED,
      hit_hint=kp.ItemHitHint.KEEPALL)
6 Likes