Initial commit

This commit is contained in:
Erez Shermer 2020-11-01 13:17:27 +02:00
commit c11a4c19ec
7 changed files with 161 additions and 0 deletions

13
.gitignore vendored Normal file
View file

@ -0,0 +1,13 @@
# Intellij
*.iml
.idea
# npm
node_modules
package-lock.json
# build
main.js
*.js.map
obsidian.d.ts

39
README.md Normal file
View file

@ -0,0 +1,39 @@
# Obsidian Vimrc Support Plugin
This plugin loads a file of Vim commands from `VAULT_ROOT/.obsidian.vimrc`.
For users of the Obsidian.md Vim mode, this is very useful for making various settings (most notably keymaps) persist.
## Usage
Just put a file named `.obsidian.vimrc` in your vault root.
If you're using multiple vaults, you'll need this file on each one.
Here's a simple & useful `.obsidian.vimrc` that I'm using:
```
nmap j gj
nmap k gk
nmap H ^
nmap L $
nmap <F9> :nohl
```
## Supported Commands
The commands that can be used are whatever CodeMirror supports.
I couldn't find a formal list anywhere but you can look for `defaultExCommandMap` in [the source code](https://github.com/codemirror/CodeMirror/blob/master/keymap/vim.js), or play around with trying commands in Obsidian's Vim mode.
Note that the file parsing is as simple as it gets -- it just sends each line to the CodeMirror Ex-command parser. Therefore comments or other features you may expect don't work.
Also, commands that fail don't generate any visible error for now.
## Wishlist
There are many things that I wish the CodeMirror implementation would allow.
Many of these can be added using the CodeMirror [API for extending its Vim mode](https://codemirror.net/doc/manual.html#vimapi_extending) and maybe I'll work on these at some point.
Things I'd love to add:
- Implement some standard `vim-markdown` motions for Obsidian, e.g. `[[`, or implement for CodeMirror the 1-2 missing Ex commands required to define these keymaps in the Vimrc.
- Relative line numbers.

36
main.ts Normal file
View file

@ -0,0 +1,36 @@
import { App, Plugin, TFile, MarkdownView } from 'obsidian';
export default class MyPlugin extends Plugin {
onload() {
console.log('loading Vimrc plugin');
this.registerEvent(this.app.workspace.on('file-open', (file: TFile) => {
const VIMRC_FILE_NAME = '.obsidian.vimrc';
this.app.vault.adapter.read(VIMRC_FILE_NAME).
then((lines) => this.readVimInit(lines)).
catch(error => { console.log('Error loading vimrc file', VIMRC_FILE_NAME, 'from the vault root') });
}));
}
onunload() {
console.log('unloading Vimrc plugin (but Vim commands that were already loaded will still work)');
}
readVimInit(vimCommands: string) {
var view = this.app.workspace.activeLeaf.view;
if (view.getViewType() == 'markdown') {
var markdownView = view as MarkdownView;
var cmEditor = markdownView.sourceMode.cmEditor;
if (cmEditor) {
vimCommands.split("\n").forEach(
function(line, index, arr) {
if (line.length > 0) {
CodeMirror.Vim.handleEx(cmEditor, line)
}
}
)
}
}
}
}

9
manifest.json Normal file
View file

@ -0,0 +1,9 @@
{
"id": "obsidian-vimrc-support",
"name": "Obsidian Vimrc Support",
"version": "0.1.0",
"description": "Auto-load a startup file with Obsidian Vim commands.",
"author": "esm",
"authorUrl": "",
"isDesktopOnly": false
}

23
package.json Normal file
View file

@ -0,0 +1,23 @@
{
"name": "obsidian-vimrc-support",
"version": "0.1.0",
"description": "Auto-load a startup file with Obsidian Vim commands.",
"main": "main.js",
"scripts": {
"dev": "rollup --config rollup.config.js -w",
"build": "rollup --config rollup.config.js"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.1.0",
"@types/node": "^14.14.6",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"rollup": "^2.33.0",
"tslib": "^2.0.3",
"typescript": "^4.0.5"
}
}

19
rollup.config.js Normal file
View file

@ -0,0 +1,19 @@
import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
export default {
input: 'main.ts',
output: {
dir: '.',
sourcemap: 'inline',
format: 'cjs',
exports: 'default'
},
external: ['obsidian'],
plugins: [
typescript(),
nodeResolve({browser: true}),
commonjs(),
]
};

22
tsconfig.json Normal file
View file

@ -0,0 +1,22 @@
{
"compilerOptions": {
"baseUrl": ".",
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "es5",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"importHelpers": true,
"lib": [
"dom",
"es5",
"scripthost",
"es2015"
]
},
"include": [
"**/*.ts"
]
}