Drill 5: Custom Mappings

Screencast

Review of Last Week

Last week we talked about defining marks, and using them to jump around files.

They come in handy when there's a reason you'll want to get back to a line of code, but Vim couldn't possibly know what that reason is. Vim can't peer inside your head to know that the line you mark is important to the bug you're on, or the paragraph you're editing. But by marking that line as 'a' or 'b', you've given Vim the hint that it needs to help you on your way.

Be sure to use marks liberally. Sitting on a function for a bit, and then switching away? ma first!

If you find yourself searching on the function definition a few times? Do yourself a favor and ma next time!

Another quick note on naming your marks: Feel free to use short-term mnemonics to name them. Dealing with the get_foo() and set_foo() methods? mg and ms sound like perfect names to help you remember what's where.

This Week's Drill

This week, we're not going to focus on any specific command in Vim, and instead focus on our usage of the commands we know.

Over the course of the day, you do a lot of similar work. For me, this includes things like opening & closing NERDTree, changing directory to my main project, toggling spelling correction, committing to git, and more. So we're going to be focused on making the things we already do much easier.

How Mappings Work

Ok, I lied, we will need to learn a few specific commands this week. We need to learn how to make mappings before we can think about the ones that can be of the most use to us.

Vim Mapping Modes

We all know that Vim has a ton of modes, normal, insert, visual, command, and others. There's a mapping command for each one.

noremap - Global Mapping (No Remap)

nnoremap - Normal Mode Mapping

inoremap - Insert Mode Mapping

vnoremap - Visual Mode Mapping

cnoremap - Command Mode Mapping (ie, the mode you're in are after hitting :)

There are a few others, but don't worry about them for now.

How to use Vim Mappings

nnoremap <key combo> <action> - This is the general syntax for mappings. The key combo can be several items, and the actions can be arbitrarily long as well.

For the key combo, keys can be literal. For instance vnoremap x c would change the meaning of x in visual selection mode from its normal definition to what the c key normally does.

In order to use a modifier key, you use a slightly different syntax. If you want to make Control-s save the file, while in normal mode, the command would be:

nnoremap <C-s> :w

<A-x> would be the Alt key.

<D-x> would be Apple's Command key (in MacVim at least)

The other special key is the pipe character. Because it has special meaning in Vim script, you have to instead use <bar>.

On the right hand side, another special character to know about is how to hit return <CR> does that.

Leader Key

Vim's designed with the idea that most keys are already taken. So it keeps a key designated as mapleader. By default it's set to \

In a mapping, you can use it with <leader>. It is common practice to keep most of your utility mappings under the <leader>

What No Remap Does

Say you have a mapping imap x xyz and imap y x, then when you type 'x', then it will expand to 'xyz'. During that expansion, when it hits the 'y', it will expand to 'x', which in turn expands into 'xyz' again. Leading to an infinite loop.

Most of the time, this is not what you want. So instead, the noremap version of mappings prevents that specific failure case from happening by preventing nesting of mappings. In the original example, the 'y' in 'xyz' wouldn't

Some of my favorite mappings

Directory Jumps

" Quick project access
nnoremap <leader>vd :cd ~/Projects/vimdrills<CR>

Clear Search Highlights

" Hide highlighted search terms
nnoremap <silent> <c-space> :nohl<CR>

Toggle Settings

" Setting toggles
nmap <leader>sn :set number!<CR>
nmap <leader>ss :set spell!<CR>

Quickly Grab the Pivotal Story ID for a Commit Message

See the screencast for a demo of this.

" Quickly extract the first chunk of the branch name - useful in commit messages
nmap <leader>gc /branch<CR>wyt_ggi[#<ESC>pA] <ESC>

Fix the spelling of the word under the cursor

I stole this function, but it's been one of my favorites for a long time. The mapping itself is at the bottom, but you can see how easy it is to do arbitrarily complex work with a simple keystroke.

" ---------------
" Quick spelling fix (first item in z= list)
" ---------------
function! QuickSpellingFix()
  if &spell
    normal 1z=
  else
    " Enable spelling mode and do the correction
    set spell
    normal 1z=
    set nospell
  endif
endfunction

command! QuickSpellingFix call QuickSpellingFix()
nmap <silent> <leader>z :QuickSpellingFix<CR>

Practice

Pay attention to common activities you do in the course of your work. Skim through other people's vimrc's and see if anything is either worth stealing, or triggers an idea for your own modification.

I'll be posting my Vimrc later this week for you to peruse.

Comments

comments powered by Disqus