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.

14 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.

I was looking for exactly this! The applescript works excellently whereas I found vim-anywhere not ideal as it doesn’t work for neovim at this stage.

I modified it slightly so that it would launch iTerm in a maximised mode however, with it calling it a keyboard shortcut that I linked to full screen iterm relative stack

Thanks for the share

Very late to this thread. Noncoding writer sorta new to Obsidian. Vim-anywhere is mentioned further down in the discussion, but I’m unclear what role it plays in your solution.

I had not heard of it before. I’d love to get it working for me in other contexts as well as Obsidian. I have it installed, but the Control+Command+V but it doesn’t call vim.

I have MacVim. I don’t run it in a terminal. Is this possible?

I don’t need help with this anymore. I’ve found something better that works straightforwardly: Karabiner Vim Mode Plus ~harmtemolder/karabiner-vim-mode-plus - A complex modification for Karabiner Elements that mimics Vim's navigation throughout your entire Mac - sourcehut git

Karabiner is a keyboard remapper for macOS. One user created a fairly comprehensive mapping of basic Vim commands. It makes them available in all apps.

After working with Karabiner-Vim Mode-Plus for a while I’m not finding it as convenient as I hoped it would be. There are a limited number of controls. Some that I have especially relied on are missing. They could be added, but digging into how to do is way beyond me. And likely to be a consuming distraction from I pursued it.

I would like to be able to call Vim itself when I have any writing to do. So, if Vim Anywhere is still a thing I would very much like to get it working for me.

I have MacVim. Vim Anywhere is installed. Cmd-Opt-V has no effect.

(1) External Terminal App

(1) Install the fantastic Shell Commands plugin.

(2) Create the following Shell Command and bind it to a hot-key, e.g. “Alt-E”:

nvim --server /tmp/neovim.pipe --remote {{file_path:absolute}} && nvim --server /tmp/neovim.pipe --remote-send ':call cursor({{caret_position:line}}, {{caret_position:column}})<CR>'

(3) Separately, launch your favorite terminal app and open a instance of NeoVim that “listens” to the above pipe:

$ nvim --listen /tmp/neovim.pipe 

Now, simply hitting “Alt-E” on an open note “sends” it to the listening instance of NeoVim. You can also set it up a Shell Command that opens a new shell/NeoVim instance:

cd {{vault_path}} && gnome-terminal -- nvim "+call cursor({{caret_position:line}}, {{caret_position:column}})" {{file_path:absolute}}

(2) Use a Plugin That Integrates a Terminal in Obsidian and Invoke NeoVim There?

1 Like

I would very much like to get Vim Anywhere working for me. I am not smart enough to begin trying things. I wouldn’t know where to start. Would very much appreciate suggestions. Even hints.