Added yanking to system clipboard, Vimrc comments and blank lines

This commit is contained in:
Erez Shermer 2020-11-17 15:19:22 +02:00
parent 4a1bcfe3e9
commit b06ff5ba84
4 changed files with 54 additions and 6 deletions

View file

@ -13,11 +13,17 @@ 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:
```
" Have j and k navigate visual lines rather than logical ones
nmap j gj
nmap k gk
" I like using H and L for beginning/end of line
nmap H ^
nmap L $
" Quickly remove search highlights
nmap <F9> :nohl
" Yank to system clipboard
set clipboard=unnamed
```
## Supported Commands
@ -25,9 +31,11 @@ nmap <F9> :nohl
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.
In addition to that:
- The plugin skips blank lines and lines starting with Vimscript comments (`" ...`).
- Special support for yanking to system clipboard can be activated by `set clipboard=unnamed` (`unnamedplus` will do the same thing).
Also, commands that fail don't generate any visible error for now.
Commands that fail don't generate any visible error for now.
## Installation
@ -46,6 +54,10 @@ Things I'd love to add:
## Changelog
### 0.2.0
Added support for yanking to system clipboard (see above), comments and blank lines.
### 0.1.1
Fixed [an issue](https://github.com/esm7/obsidian-vimrc-support/issues/2) caused by the plugin injecting the Vimrc on every file load.

40
main.ts
View file

@ -1,6 +1,9 @@
import { App, Plugin, TFile, MarkdownView } from 'obsidian';
export default class MyPlugin extends Plugin {
export default class VimrcPlugin extends Plugin {
private lastYankBuffer = new Array<string>(0);
private yankToSystemClipboard: boolean = false;
onload() {
console.log('loading Vimrc plugin');
@ -10,6 +13,14 @@ export default class MyPlugin extends Plugin {
then((lines) => this.readVimInit(lines)).
catch(error => { console.log('Error loading vimrc file', VIMRC_FILE_NAME, 'from the vault root') });
}));
this.registerDomEvent(document, 'click', () => {
this.captureYankBuffer();
});
this.registerDomEvent(document, 'keyup', () => {
this.captureYankBuffer();
});
}
onunload() {
@ -22,9 +33,22 @@ export default class MyPlugin extends Plugin {
var markdownView = view as MarkdownView;
var cmEditor = markdownView.sourceMode.cmEditor;
if (cmEditor && !CodeMirror.Vim.loadedVimrc) {
CodeMirror.Vim.defineOption('clipboard', '', 'string', ['clip'], (value, cm) => {
if (value) {
if (value.trim() == 'unnamed' || value.trim() == 'unnamedplus') {
if (!this.yankToSystemClipboard) {
this.yankToSystemClipboard = true;
console.log("Vim is now set to yank to system clipboard.");
}
} else {
throw new Error("Unrecognized clipboard option, supported are 'unnamed' and 'unnamedplus' (and they do the same)")
}
}
});
vimCommands.split("\n").forEach(
function(line, index, arr) {
if (line.length > 0) {
if (line.trim().length > 0 && line.trim()[0] != '"') {
CodeMirror.Vim.handleEx(cmEditor, line);
}
}
@ -36,5 +60,17 @@ export default class MyPlugin extends Plugin {
}
}
}
captureYankBuffer() {
if (this.yankToSystemClipboard) {
let currentBuffer = CodeMirror.Vim.getRegisterController().getRegister('yank').keyBuffer;
if (currentBuffer != this.lastYankBuffer) {
if (this.lastYankBuffer.length > 0 && currentBuffer.length > 0 && currentBuffer[0]) {
navigator.clipboard.writeText(currentBuffer[0]);
}
this.lastYankBuffer = currentBuffer;
}
}
}
}

View file

@ -1,7 +1,7 @@
{
"id": "obsidian-vimrc-support",
"name": "Obsidian Vimrc Support",
"version": "0.1.1",
"version": "0.2.0",
"description": "Auto-load a startup file with Obsidian Vim commands.",
"author": "esm",
"authorUrl": "",

View file

@ -1,6 +1,6 @@
{
"name": "obsidian-vimrc-support",
"version": "0.1.1",
"version": "0.2.0",
"description": "Auto-load a startup file with Obsidian Vim commands.",
"main": "main.js",
"scripts": {