ReefPointsBlog

Vim: Staying on Home Row via Map

Doug Yun

Here at DockYard, the majority of us are using Vim. I don't want to write about the benefits of using this sweet editor, as that would take too long, but instead, I'd like to share a couple of my favorite mappings for escaping and saving files.

Vanilla Vim: Escaping and Saving

Escaping out to Normal mode from the other modes in Vim is straightforward: simply hit the Esc key. Saving files is accomplished by, from Normal mode, pressing :w and then Enter.

So... What's the Problem?

During a session, especially when I'm writing large pieces of text, I'd find myself in a repetitive rut:

See where I'm getting at?

Let's Talk About Map

Before we review and copy-pasta the portion of my .vimrc, let's briefly go over the very basics of the pertinent map commands. You can find the entire map documentation here or by typing :help map within a Vim session.

Protip: To open help texts into a full buffer, :h map | only or to open them in a separate tab :tab h map.

Recursive Map

First, we're going to talk about recursive map commands. A recursive command will transform one result to another result, if there is another binding to that key. An example can be found at the .vimrc below.

Here are the basic recursive map commands.

You can prepend the first letter of the desired mode to map.

For example, if I had this within my .vimrc:

1
2
3
4
5
6
7
8
9
10
" ~/.vimrc
"
" Note: double quotes signifies comments

nmap 0 gg
imap n N

" Time for a little recursive map
imap d D
imap D wat

Since 0 is mapped to gg within Normal mode, I'll be sent to the top of the file by pressing 0. Moreover, while in Insert mode, every character n that I type will turn into N. Lastly, because of the recursive mapping, typing d in Insert mode will return wat. You can think of it as something like: d => D => wat.

Thankfully, there's a non-recursive map.

Non-recursive Map

Non-recursive map commands are signified by adding nore after the mode modifier.

1
2
3
4
" ~/.vimrc

inoremap c C
inoremap C nope

Now, in Insert mode, if we type c, we will return C; the transformation of c to nope will not occur.

Enter the .vimrc

Now that we got the basics out of the way, here is an example of my .vimrc.

1
2
3
4
5
6
7
8
9
" ~/.vimrc
" *** The Two Hand system ***
"
" <Cr> signifies the "return" key

inoremap ;a <Esc>
inoremap ;d <Esc>:update<Cr>
inoremap ;f <C-O>:update<Cr>
nnoremap ;f :update<CR>

I'm using :update here, which is "like :write, but only write when the buffer has been modified."

Let's go over these mappings.

The first one, inoremap ;a <Esc> maps the semi-colon and a key together when in Insert mode. By pressing ; and then a immediately afterwards, we mimic the functionality of the Escape key.

The second map, inoremap ;d <Esc>:update<Cr> maps the semi-colon and the d key. Pressing ; and then d immediately afterwards returns the sequence of:

The third map command, inoremap ;f <C-O>:update<Cr>, allows us to type ; and then f to return:

Finally, the nnoremap ;f :update<CR> mapping means by typing ; and then f in Normal mode, it will result in:

The snippet below restricts these commands to your right hand.

1
2
3
4
5
6
7
" ~/.vimrc
" *** The Right Hand system ***

inoremap ;l <Esc>
inoremap ;k <Esc>:update<Cr>
inoremap ;j <C-O>:update<Cr>
nnoremap ;j :update<CR>

As you can see, I kept ; as a prefix to my map commands. This conveniently keeps me at homerow. I've played with mapping everything with my right hand, but it just didn't feel "right" (apologies for the bad pun).

Overall, this snippet makes me happy and I believe this will make your day as well. If there are some other tricks concerning escaping and saving files, please let me know in the comments! Thanks!