Windows with Gvim: open current Vimwiki document in Obsidian

Here is a Vim function that can open the current file in Obsidian. <F3> is mapped to trigger this simple function.

" Open current file in obsidian
    " Assumes the current directory as the "Vault" in Obsidian
    " Takes the filename and patches ".md" in composed_HTML_schema
function! OpenFileinObsidian()
    " Source for the expand command: https://vim.fandom.com/wiki/Get_the_name_of_the_current_file
    let filename = expand("%:t:r")
    let foldername = expand('%:p:h:t')
    let composed_HTML_schema = "obsidian://open?vault=".foldername."&file=".filename.".md"
    " echo composed_HTML_schema "Toggl this line for debugging
    exe '!start /MIN cmd /c "start "" "' . composed_HTML_schema '"'
        " Note, the closing double quote is important to include.
        " `start "" "command"` is for cmd, which executes the command bit
endfunc
" Combine the open-in function to <F3>
nnoremap <F3> :call OpenFileinObsidian()<CR>

Credits: This script is mostly inspired by severoraz’s post for Vimwiki, in Issue 71.

Additionally, with the help of the Vimwiki community, there are similar modifications to the VimwikiLinkHandler function for opening OneNote pages natively in Vimwiki, as linked here. It is enjoyable to find that the onenote:https://d.docs..... links are natively supported in Obsidian already.

2 Likes