Modifying VIM emulation in Visual Studio Code to Work Better

February 11, 2021 tech full-stack

I just started using Visual Studio Code. I’ve been using vim for a long time. I liked some of the features of VS Code, but I can’t go back to a mouse.

So, of course VSC has vim emulation, but the thing is, I tweaked vim a lot, and there were a few things that didn’t quite work how my fingers wanted them to work.

If you are in the same boat, maybe this will help you tweak your keys and get this working.

Install “vscodevim” for the emulation.

This gets you 99% if the way there.

clipboardPasteAction

Now, we need to modify our setup. Open up the shortcuts area by holding down: Cmd+Shift+P. Type “keyboard”, enter to open “keyboard shortcuts”

Change Keybinding

I am changing the keybindings for “moving to the next tab”, when I have more than one open. For years, I’ve done this in vim with “(” and “)” – basically “shift-9 and shift-0” while in normal mode. Why “(” and “)”? Somewhere along the way, I just chose those. In normal mode, they weren’t mapped to anything I was using, and for years, it’s worked.

Here’s how it looks in the my .vimrc file:

nnoremap ) :bn
nnoremap ( :bp

I never need ( and ) while in normal mode otherwise, they are right next to each other. Now, my fingers go there in Visual Studio Code, but it doesn’t do anything, and then I have to grab my mouse… eh.

Anyway, let’s make this change.

Now that you are in the keyboard shortcuts editor, search for “Open Next”, there might be a couple of shortcuts for it.

Right-click one of the lines called “View: Open Next Editor”. We’ll do two things.

  1. Change Keybinding: choose this option, then just hit your key combination, I hit shift+0, which is “)”. Hit enter.
  2. Right click again. You need to change the When Expression, as in, “when this will work”. If you set it globally, and you are in vim insert mode, you won’t be able to type a “)” and, um… you’re going to want to! After a bit of research, I found that this works:

editorTextFocus && vim.active && vim.mode != 'Insert'

Copy and paste that into the “When” box and hit enter. For the record, that means “When the editor has focus AND when the vim plugin is active AND when the vim.mode is not Insert”.

Now do the same for “View: Open Previous Editor”

This time, I used “shift+9” keybinding, which is “(“, but used the same “When” expression. Enter saves, and you’re all set.

If you don’t use “(” and “)” like me, then pick your combo. I hate leaving the normal keyboard. Complex commands using several modifier keys slow me down (every keyboard seems to be slightly different). This, to me, is the best solution, and now my fingers can be happily jumping from editor to editor.

Copy and Paste, Yanking and Putting

I also tired of having to highlight and copy to get the stuff to the keyboard, and go into insert mode and press Cmd-v/Control-v to paste. So, I modified the behavior like this:

editorTextFocus && vim.active && vim.mode != 'Insert'

Now that we’ve been over it a few times, can you get “Copy” on your own? If not, hit me up on Twitter for some tips.