Using Vim like keybindings in Scrivener Mac

This is purely applicable to the Mac version – not sure if this is the right forum for it, but please move it to the correct one if it’s not.

Now and then people express the desire for Vim keybindings to function in Scrivener’s editor, and I’m one of those people… I understand why they’re not included, of course, but I still miss them when editing large amounts of text. I know we can use the built-in Emacs bindings (ctl-A, E, K, N, P etc) which helps a little, but the default bindings for back and forward word with and without select are as bad as using the arrow keys, which is what we’re trying to avoid in the first place.

Anyway, I’ve recently come across a method of creating custom keybindings to be read by any OS X text field (I think), so it works well in Scrivener. It’s a little geeky, so it’s quite possible that everybody who’s geeky enough to want vim bindings already knows about this, but I thought I’d mention it anyway, just it case it helps somebody.

Basically, you create your own file DefaultKeyBinding.dict in a folder KeyBindings inside ~/Library, add your chosen mappings, then restart the application you want to use the new bindings with.

Obviously you can’t emulate Vim’s modal editing, but you can use a single modifier to get most of the more common movement commands. I’ve chosen to put them on the opt / alt key, because I only ever use the opt key for modifying vowels. Therefore I can use opt-h/j/k/l for single movements, opt-w/b for word movements, opt-g for beginning of file etc. Add shift to these bindings and they select the text as well. I’ve also added ctl-f/b for page down/up. Obviously, you can’t make every binding an exact analogue of the vim version (you don’t seem to be able to replicate gg, so I use opt-g for beginning of file and ctl-g for end, for example) but you can get quite close… You can also use the same file to add shortcuts for common symbols such as ∵ and ∴.

Here’s my current version of the file. ~ = opt key, ^ = ctl, @ = cmd. Upper case letters are obviously those with Shift applied.

[code]{
/* vim arrows */
“~h” = “moveBackward:”;
“~H” = “moveBackwardAndModifySelection:”;
“~l” = “moveForward:”;
“~L” = “moveForwardAndModifySelection:”;
“~k” = “moveUp:”;
“~K” = “moveUpAndModifySelection:”;
“~j” = “moveDown:”;
“~J” = “moveDownAndModifySelection:”;

// Page
“^b” = “pageUp:”;
“^B” = “pageUpAndModifySelection:”;
“^f” = “pageDown:”;
“^F” = “pageDownAndModifySelection:”;

// Word
“~w” = “moveWordForward:”;
“~W” = “moveWordForwardAndModifySelection:”;
“~b” = “moveWordBackward:”;
“~B” = “moveWordBackwardAndModifySelection:”;
“~g” = “moveToBeginningOfDocument:”;
“~G” = “moveToBeginningOfDocumentAndModifySelection:”;
“^g” = “moveToEndOfDocument:”;
“^G” = “moveToEndOfDocumentAndModifySelection:”;

// Paragraph
“@~j” = (moveToEndOfParagraph:, moveDown:);
“@~J” = (moveToEndOfParagraphAndModifySelection:, moveDownAndModifySelection:);
“@~k” = (moveToBeginningOfParagraph:, moveUp:);
“@~K” = (moveToBeginningOfParagraphAndModifySelection:, moveUpAndModifySelection:);
“~p” = “selectParagraph:”;

// Misc
“~1” = (“insertText:”, “\U2234”);
“~9” = (“insertText:”, “\U2235”);

// move line Up
“^@\uf700” = (selectParagraph:, setMark:, deleteToMark:, moveLeft:, moveToBeginningOfParagraph:, yank:, moveLeft:, selectToMark:, moveLeft:);
“^@k” = (selectParagraph:, setMark:, deleteToMark:, moveLeft:, moveToBeginningOfParagraph:, yank:, moveLeft:, selectToMark:, moveLeft:);
// move line Down
“^@\UF701” = (selectParagraph:, setMark:, deleteToMark:, moveToEndOfParagraph:, moveRight:, setMark:, yank:, moveLeft:, selectToMark:);
“^@j” = (selectParagraph:, setMark:, deleteToMark:, moveToEndOfParagraph:, moveRight:, setMark:, yank:, moveLeft:, selectToMark:);

}[/code]

The actual commands (selectParagraph etc) are OS X’s own functions, of course. NB: the file is quite fragile — a single error in the format breaks it, so you need to check it carefully, but once you’ve got it working you don’t need to change the file again.

A detailed treatment of what’s possible can be found here: http://osxnotes.net/keybindings.html

I’ve only started to skim the surface of this, and no doubt I’ll modify it over time. It’s by no means a complete solution, but I’m finding it useful, so I wondered if others would too… Apologies for the length of the post, but fortunately, this hack works for Safari text boxes too, so I could use vim bindings…

If I read your post correctly this becomes GLOBAL and not scriv specific. Is that correct?

If it is, wouldn’t is be simpler, even if less elegant, to simply open the files in vim using “open in other editor”? I see where scrivening mode (my personal camping area in scriv) and other unique scriv places would break, but for general editing…

Great find either way.

It’s mostly global – depends on how the editor is written, I think. It works in the Apple Text Engine (Scriv, TextEdit, Safari, Tinderbox, DTPO text fields) but not in Mail, as far as I can tell, for example.

‘Open other editor’ just opens the file in the other side of a Scrivener split screen doesn’t it, and Open in External Editor is for image and other files such as Scapple, not for text? I’d love to be wrong on that…

I’ve used several workarounds in the past:

  • QuickCursor, which was the simplest solution but which is sadly dead – it copied the text into your editor, then copies the new / amended version back afterwards
  • a Keyboard Maestro macro which emulates QuickCursor
  • syncing with external folders using plain text files.

The problem with them all is that you lose the rest of Scrivener’s features while you’re ‘out’. Obviously the syncing with External Folders allows you to emulate ((annotations)) and {{footnotes}}, which is brilliant, but it’s not the same.

So I thought that the additional keybindings approach is a useful compromise.

I’ve found the scriv text editor sufficient for me, and that’s the excuse I’ll use for getting the menu command wrong.

And the “out” issue… that’s what I was hinting at. The only thing that would bother me with this approach is that it is global. I actually like to keep some things “stupid” to allow a change of mental reference. And the boss (wife) uses my account on occasion. I don’t even want to think the mess that might make for me. Not that she would mess anything up, but she would be less than appreciative.

I’m not expecting a huge amount of take up for the idea, TBH :wink: I just get irritated having to use the arrow keys…

I’d not seen it mentioned on the forums before, so I thought I’d mention it in case somebody else is suitably affllicted!

It’s a great idea. I’m still debating “do i do this?” but then I’d have to remember to Vim everything.

I’m surprised Ioa hasn’t chimed in on this.

I’m buried in new user manual stuff. :slight_smile:

I did once try a global vi keymapping system that was also coupled with (I think, it’s been a long time) an InputManager so you could get real bimodal editing going on and decent vi compatibility. It was a little annoying having to remember to hit ‘i’ before before typing into literally every text field you click into, though, and Esc is often useful in various fields and contexts beneath the text entry layer (such as clearing a search field). So for me that was better in theory than practice, but it was nice having familiar keystrokes, even including quantified commands like 2dd.

That is nothing like the keymap you posted, and I don’t think the above is possible any longer. InputManagers were made obsolete years ago. The last time that method worked reliably was probably 10.4.

The main issue I see with this keymap is that Opt- is used a fair bit, which means the symbols you’d get with those combinations would be inaccessible, no? If you never use special characters like “©” (~g) then that’s no big deal.

QuickCursor was killed off by sandboxing when Apple made that mandatory for MAS stuff. It is open source, but roughly in the same broken state it was left in, so not even worth the trouble to get it compiling as far as I can tell. I do miss that one!

I miss QuickCursor too… The KeyboardMaestro substitute is fairly good though – I can post that if anybody’s interested. It’s not my idea though, of course: borrowed from somebody over the years.

Yes, you have to be careful about which modifier key you use. I only ever really use accents over vowels, so as long as I avoid them (and cmd-c for ç), it’s good. If I have to use © and ™ I’ll rely on Autocorrect to do it – or remap them in DefaultKeybinding.dict by using the same method I’ve used for ∵ and ∴.

The good thing about the opt key is that it’s ignored by most standard programs for that very reason, so there’s plenty of scope to find ones that work for you. The only real clash with Scrivener shortcuts is ctl-cmd-k, which I have mapped to Move Paragraph down. I just needed to remap the ‘with Selection as Title’ split function to something else in SystemPreferences to get round that.

The other big keyboard issue at the moment is that Karabiner doesn’t work with macOS Sierra* yet. I used to have an extra Control key mapped onto Enter – tap it for Enter, hold it down for Control – and something similar with the ex-Caps Lock key: tap for Escape, hold down for Left-Control. These are very very useful, especially if you’re using Emacs or Vim.

  • I know the company has a beta product (Karabiner Elements) out, but it’s in a very early state and I don’t want to experiment with it yet.

And meanwhile Apple is getting rid of the Esc key. One more reason to take good care of your current hardware!

Karabiner is one of those things I’ve wanted to look into but never really had the time to. I just used it to replace the iPod Mode button on Fn-F4 since Apple decided nobody should ever change that without a kernel extension. Hmm, I might, once it is working again, take your approach of double-binding the CapsLock. I’ve always mapped Ctrl to that key, so I could either put Esc on quick tap, or Esc on the physical Ctrl and Ctrl on CapsLock. I don’t know though, sometimes it is handy to have all three primary modifiers in one straight row on the left side.

Or I could for as long as possible avoid Apple keyboards that assume emoticons are more important than function keys and escape of all things! That’s like removing the return key in my world.

That’s a good point, and bare Ctrl is also pretty safe (especially if you don’t care for the full Emacs spectrum, I just use the basics, a/e/k).

On the esc key line…

Where is the real money for apple? Tech nerds or my wife, daughter, their friends, etc? Which group uses the esc key for anything real?

Apple is making the right move FOR THEM.

I don’t like it. But I get it.

The function keys and Escape key are all still there, virtually (I think you press the fn key) – you don’t have to use the per-application touch bar icons for everything.

I’d prefer a physical escape key, but I’ll probably remap anyway (as per this thread). I’m not sure I’d use the Touch Bar much, but I don’t use any of the demo’d applications, so it’s difficult to tell until we see what developers do with the features.

But I definitely think the Touch Bar is much preferable to have a full touch-screen – I’d be very reluctant to spend money on a touch-screen enabled laptop. Why would I want to keep having to reach out to touch the screen? (I’m sure there are some use-cases, but not for what I do.)