Last Updated: February 25, 2016
·
8.534K
· amoniker

Vim-style movement (and more) in Sublime Text 3

Sublime Text 3 is a great editor, and it comes with a package called Vintage which emulates some vim features. However, I didn't want most of the functionality of this package and I also have my own idea of how my keys should be mapped.

Rather than emulating vim modes, I like to use a simple modifier key. So, Control + [ h j k l ] will move my cursor around à la vim. My Caps Lock key has been remapped to Control, so this is a nice and convenient way to move around without having to reposition my right hand to the arrow keys.

Setting this up is pretty easy. In my ST3 keymap file (Sublime Text > Preferences > Key Bindings - User) I added the following bindings:

Vim-style movement

{
  "keys": ["ctrl+h"], "command": "move", "args": { "by": "characters", "forward": false }
},
{
  "keys": ["ctrl+j"], "command": "move", "args": { "by": "lines", "forward": true }
},
{
  "keys": ["ctrl+k"], "command": "move", "args": { "by": "lines", "forward": false }
},
{
  "keys": ["ctrl+l"], "command": "move", "args": { "by": "characters", "forward": true }
},

Hey, wouldn't it be nice if I could add also use these new movement keys to select text? I decided that using Command + Control + [ h j k l ] should highlight text as I moved:

Vim-style movement with highlighting

{
  "keys": ["ctrl+super+h"], "command": "move", "args": { "by": "characters", "forward": false, "extend": true }
},
{
  "keys": ["ctrl+super+j"], "command": "move", "args": { "by": "lines", "forward": true, "extend": true }
},
{
  "keys": ["ctrl+super+k"], "command": "move", "args": { "by": "lines", "forward": false, "extend": true }
},
{
  "keys": ["ctrl+super+l"], "command": "move", "args": { "by": "characters", "forward": true, "extend": true }
},

Ok, now we're getting somewhere. This is all well and good for the right hand, but the left-hand doesn't have much to do other than hold down the Control key. Surely we can give our sinister friend another job.

My pinky finger is on the control key, so I've got three others that have easy-access to A S D and Q W E. I think it's useful to make Control+A go to beginning of a line, while Control+D will go to the end. This seems logical, don't you think?

Similarly, Control+Q should function as "page up" while Control+E will function as "page down"

Left-hand movement

{
  "keys": ["ctrl+a"], "command": "move_to", "args": { "to": "bol" }
},
{
  "keys": ["ctrl+d"], "command": "move_to", "args": { "to": "eol" }
},
{
  "keys": ["ctrl+q"], "command": "move", "args": { "by": "pages", "forward": true }
},
{
  "keys": ["ctrl+e"], "command": "move", "args": { "by": "pages", "forward": false }
},

It'd be useful to highlight lines as well, right?

Left-hand movement with highlighting

{
  "keys": ["ctrl+super+a"], "command": "move_to", "args": { "to": "bol", "extend": true }
},
{
  "keys": ["ctrl+super+d"], "command": "move_to", "args": { "to": "eol", "extend": true }
},

While it's useful to highlight to the beginning or end of a line, it doesn't seem very useful to highlight by pages at a time. Instead, maybe Control+Command+Q and Control+Command+E could move between open files.

Moving between files

{
  "keys": ["ctrl+super+e"], "command": "next_view"
},
{
  "keys": ["ctrl+super+q"], "command": "prev_view"
}

Nice.

Oh - there's one other function that's a little inconvenient on a mac keyboard: deleting the character in front of the cursor. Reaching for Fn + Delete puts both hands out of position. Let's fix that by making the same thing happen with Control+S

Forward delete

{
  "keys": ["ctrl+s"], "command": "right_delete"
},

Of course, you don't have to use my keybindings - remix them and come up with your own style. Have a better idea or a cool shortcut? Let me know in the comments!

The unofficial ST documentation has been a great help in setting these up: http://docs.sublimetext.info/en/latest/

Also, if you'd like Vim-style movement in iTerm2, check out my protip here: https://coderwall.com/p/c9vs3q

Edit: If you want vim-style movement in any app check out the amazing http://www.keyboardmaestro.com

Enjoy!

1 Response
Add your response

Excellent @nchudleigh, I'm glad they were helpful!

Following up on this, I loved the keys in ST3, but I found myself missing them in other applications so I ended up using Keyboard Maestro to set Vim shortcuts (and more) on browsers, email, IRC, etc. No doubt there's a Linux program that can do this too.

over 1 year ago ·