I spent some time playing around with the temporary Volcano plugin loader for Obsidian, and figured it’s actually very easy to make this Vimrc feature happen!
So no guarantees whatsoever, but the following seems to work (I called it vimrc.js
under ~/volcano/plugins
):
class VimrcLoader {
constructor() {
this.id = 'vimrc-loader';
this.name = 'Vimrc loader plugin';
this.description = 'Auto-loader for Obsidian Vim commands on startup.';
this.defaultOn = true; // Whether or not to enable the plugin on load
}
init(app, instance) {
this.app = app;
this.instance = instance;
this.enabled = false;
}
onEnable(app, instance) {
instance.registerEvent(app.workspace.on("file-open", this.onFileOpen, this));
this.enabled = true;
}
onDisable(app, instance) {
this.enabled = false;
}
async onFileOpen(file) {
var activeView = this.app.workspace.activeLeaf.view;
const FILE_NAME = ".obsidian.vimrc";
if (this.enabled && activeView && activeView.sourceMode) {
var cmEditor = activeView.sourceMode.cmEditor;
var vaultPath = this.app.vault.adapter.basePath;
var configPath = require('path').join(vaultPath, FILE_NAME);
this.readVimInit(configPath);
}
}
readVimInit(filePath) {
var fs = require("fs");
var cmEditor = this.app.workspace.activeLeaf.view.sourceMode.cmEditor;
fs.readFileSync(filePath).toString().split("\n").forEach(
function(line, index, arr) {
if (line.length > 0) {
CodeMirror.Vim.handleEx(cmEditor, line)
}
}
);
}
}
module.exports = () => new VimrcLoader()
Example .obsidian.vimrc
file (in the vault root):
nmap H ^
nmap L $
nmap j gj
nmap k gk
nmap <F9> :nohl
Notes:
- You’d need your Vim commands file to be named
VAULT_ROOT/.obsidian.vimrc
. I’d prefer this file to reside in the Obsidian config directory but I couldn’t find a portable way to get it from the app object. - The vimrc is loaded every time a file is loaded in Obsidian. It might be better to do it just once.
- The vimrc parsing is extremely simplistic, every line is sent as-is to the CodeMirror Ex-command parser. So even standard Vim comments would generate an error.
- Speaking about errors, they are not properly reported anywhere.
- There are probably many other gotchas, I just hacked this together rather quickly to get it working and I don’t really know what I’m doing