Open note in Obsidian from within Vim and vice versa

Hi, in case some of you also use or plan to use Obsidian and Vim (resp. vimwiki) side by side as two different viewports on the same notebase, this might be useful[^1] (the following describes solutions for macOS; maybe someone else could provide infos on how to do this on Linux or Windows):

Open note in Vim from within Obsidian

If you use Vim in iTerm, you can just create an App with Automator:[^2]

-- TerminalVim.app
-- This creates a shim Application that will enable you to open files from the Finder in vim using iTerm

-- To use this script: 
-- 1. Open Automator and create a new Application
-- 2. Add the "Run Applescript" action
-- 3. Paste this script into the Run Applescript section
-- 4. Save the application as TerminalVim.app in your Applications folder
-- 5. In the Finder, right click on a file and select "Open With". In that window you can set TerminalVim as a default

on run {input, parameters}
	tell application "iTerm"
		set newWindow to (create window with default profile)
		tell current session of newWindow
			write text ("vim " & quote & POSIX path of input & quote & "; exit")
		end tell
	end tell
end run

If you use Neovim, just replace “vim” with “nvim”. After following all steps, you can open a note from within Obsidian with the “Open in default app” feature (you can create a shortcut in the settings).[^3]

Open note in Obsidian from within Vim

With the URL feature of Obsidian, this is now also possible. I just added the following to my .vimrc / init.vim:

" Open file in Obsidian vault
command IO execute "silent !open 'obsidian://open?vault=VAULTNAME&file=" . expand('%:r') . "'"
nnoremap <leader>io :IO<CR>

You just have to replace VAULTNAME with the actual name of your vault. You can then press <leader>io (for “in Obsidian”) to open the currently opened note in Vim in Obsidian. In case you use Linux, you have to replace the open command with the equivalent command (e.g. xdg-open).

[^1]: As a sidenote: I would recommend using the “relative paths” settings in Obisdian, if you want to use vimwiki as a second (or future fallback) edit environment, because this allows you to follow the links in vimwiki.
[^2]: Source: https://gist.github.com/charlietran/43639b0f4e0a01c7c20df8f1929b76f2
[^3]: This opens a new window in iTerm. It should be possible to open a file in a running instance of Neovim, but due to a bug in macOS I can’t test that right now. I might add this later.

11 Likes

I use Vim Anywhere, which seems easier.

1 Like

Thanks for the link. I didn’t try that yet.

Does it read the already written text from Obsidian into the opened vim buffer?

Well, it works with MacVim, which I don’t have. I use neovim in the terminal. Furthermore, it looks like you have to manually paste the edited text back to Obsidian, since it doesn’t edit the actual file. At least for me, that would be (possibly) 3 disadvantages. But still, it might be useful for others.

MacVim is free, and installing the vim-anywhere service is easy. If you select the text you already wrote before invoking vim-anywhere using the Obsidian hotkey cmd-option-v (assuming you have set MacVim as the default external application for markdown files), it will copy that text to the MacVim buffer. Also, when you save the MacVim file, it will copy the text to the Obsidian file.

Let me add that I’m not sure this route even uses vim-anywhere. Perhaps having MacVim installed and set as the default external application for .md suffices.