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;
}
}
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.)
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)
Chiming in with another use case here; I’ve been finding that I much prefer to use Windows’ two-pane-plus-preview file explorer over Obsidian’s single pane explorer, but it’s not really possible to open the files in Obsidian by just adding Obsidian as a markdown-supporting Windows app.
I could probably whip up a script and then add stuff to the Windows registry to do it (and probably will), but it would be nice if Obsidian.exe windows-filename-here just worked to detect the vault and note.
As it is, it’s currently necessary to split the file name into vault and file and then use an obsidian://open URL. But at least that can be done by searching upward for an .obsidian directory… though I’m not sure that will give you a definitive vault name. It appears that obsidian.json under AppData/Roaming/obsidian (on Windows) gives the vault paths and their IDs, and that obsdian URLs support specifying a vault by ID, so that would be a viable approach to generating a URL.
But since all that is pretty fragile and has to be duplicated for each platform as an external tool, it would be nice if Obsidian just supported the “open from native file manager” use case to start with.
What I would like is to use Obsidian from cmd line as I use for example VSCode or Sublime Text.
I am not sure of all of the use cases in here. But what I would like that obsidian works from cmd line just as any regular editor.
Opening the vault
This opens the vault in that path (if not already open) and closes the current vault. obsidian myvault
This would open vault in new window. obsidian -n myvault
Of course if you prefer the above behaviour could be flipped. So then you would specifically need to ask to open the new vault into the current window.
Opening individual files in the vault
Open specific file in that vault in current active pane
If vault is not already open, then also open the vault myvault obsidian myvault/file.md
Opens file in new pane (so same as cmd+click on obsidian) obsidian -np myvault/file.md
I guess we could have also possibility to open file in any current pane
with using a number to refer it
1 = first pane from left, 2 = second pane from left, 3 = third pane from left and so on
So this would open it in 2nd pane from the left. obsidian -p 2 myvault/file.md
Summary
I would just recommend looking how code and subl commands work and improve from my suggested approach from that point of view.
What do you think? Can some of this actually already implemented?
I know Obsidian itself can be opened in macOS with open -a 'Obsidian'
Just in case its not entirely obvious, if you are in windows and you want a command line option for opening to a vault or a file, you can of course just build a batch file to do it for you.
Thus the command line is simply (with %20 being where spaces are):
start obsidian://open?vault=My%20Dev%20Vault
For instance, here is a autobuilder batch file in plugin development to automatically launch obsidian and then select a plugin to build and then starts a build watch. See obsidian-z2k-utils/AutoBuilder.bat at main · z2k-gwp/obsidian-z2k-utils · GitHub for how it is done. Note: if you put it in a batch file, you’ll need to double up the %% for any %20 used for spaces.
But this doesn’t hide it again when it is focused like it does with all other apps and my terminal
Currently working around it with a “hide window” hotkey but not really efficient
So please let this be true one day.
This opens the vault in that path (if not already open/hides if open) and closes the current vault.
`obsidian myvault`
This would open vault in new window.
`obsidian -n myvault`
I like to pin my commonly-used items to windows taskbar.
If I do that with obsidian, if I happened to have opened the documentation vault (actually if I closed it last), then next time I run Obsidian, that will be the vault opened. I’m a newbie so that’s happening a lot at the moment and it’s very frustrating.
Although @truth is correct that a url-driven batch file can be written and that will open the correct vault if double-clicked, that can’t be pinned to the taskbar in the same way.
If obsidian supported vault being specified on the commandline I could simply change the properties of the pinned obsidian shortcut to “/path/to/obsidian.exe {my-vault}” then the pinned item would work as intended - if obsidian not running, open the wanted vault, if already running, switch to it. Job done, and that’s “the windows way”…
… which means I can assign a shortcut key to it (with no other software needed) or if it’s one of my key apps (I’d like it to be!) that’s one of the first 9 items in the taskbar, I just do “win-#” to open/switch to.
Please please implement windows commandline support for obsidian.exe {myvault}
+1000 for this suggestion. Virtually every other editor of every kind does this, so it’s extremely frustrating that Obsidian doesn’t. The addition of this simple feature would allow amazing desktop and workflow integrations. Linux user here.
For example, one could have different panel icons launch different vaults.
A more complex, real-world (for me) use case: I have a scripted (shell functions) “job” control feature for when I run different engagements for different clients. New engagement = new “job”. So in my workflow I use "job-new " and it creates a brand new template directory structure for me from a skel directory: it contains empty and template files I might need.
The same workflow also contains a “notes” command which opens the notes file for the current job: currently it opens a Cherrytree document, which is a template to begin with but contains job-specific information as time goes on.
I’d like “notes” to launch Obsidian instead so I can use that instead of Cherrytree. Currently that’s not feasible because there’s no way to tell Obsidian “use this Vault I’ve just created”, or “use this Vault from this job”. You just get whatever vault you last worked on, which is incredibly inflexible.
This is possible on MacOS, and probably Linux. I’ll test the latter later.
# Open Obsidian
open -a "obsidian"
# Open note
open "obsidian://vault={vault}&file={filename}"
To get vault and filename, right-click a note in Obsidian and “Copy Obisidian URL”. The URL scheme can also be used to search and create new notes. It is nicely documented on the website.
limitation of opening a vault with the URI like this is, I have to have already opened/registered the vault with obsidian. I can’t e.g. use a script to create a new vault and open it
You may be able to if you get a bit creative and are flexible. You seem to want to be able to open ad-hoc files. But Obsidian, like VS Code or JetBrains IDEs, is designed to work with workspaces, not isolated files.
So what you could do is pre-create a vault specifically for isolated files; let’s call it “Random”. Then we write a CLI script that when invoked on a file:
walks up the directory tree and looks for an .obsidian directory. If none is found, then the file is considered outside a vault.
If outside a vault, create a symlink to that file from inside the Random vault.
Invoke the Obsidian URI with the Random vault as in above posts.
I rapidly add and search Obsidian notes from the terminal using Fuz. It’s an extremely fast file fuzzy searcher, which I point to my Obsidian directory, opening files at selected lines.
The search results update instantly for every keypress, making it very low friction to add and search notes. While there is Markdown support, it doesn’t support e.g. images and linking like Obsidian does, so the two complement quite well.
My use case: I want to move my personal wiki (> 1000 entrances) from WikidPad to Obsidian. I don’t want to migrate in one go, but rather write new entries in Obsidian and move Wikidpad entries I’m updating. Wikidpad is still (and will be for years) the first stopping point when searching for notes. Therefore links from Wikidpad to new and updated entries in Obsidian would be useful.
So, when you work with the terminal alongside with Obsidian, to perform other things Obsidian can’t do yet, it would be helpful to open files easily from the terminal.
It’s totally ok that Obsidian can’t handle everything. I don’t really need that Obsidian adds Git support, or whatever other support, when I just have a terminal. In any case, Obsidian will never support everything, so in any case I want to work with the terminal.