Vim Key Remapping

Last September, at a Philadelphia JavaScript Developers meetup, An attendee at the meetup had done a presentation that evening on LightTable, a new code editor. At the time, I was looking at trying a new editor, so I was very interested in his presentation. After the presentation, I had the opportunity to talk to him, and he mentioned that even though he was presenting on LightTable, he was actually an avid Vim user, and he recommended I learned it.

Any new coder who has worked with Vim before can tell you that it is not very intuitive and has a sharp learning curve. That being said, I tend to do things the hard way, and dove right in to learning it. After going through vimtutor, I decided to make Vim my one and only code editor.

Through today, I have been using Vim exclusively for writing code. I have gotten very quick at it, and I feel it has increased my productivity. I recently was able to boost my productivity further after reading about reassigning your leader key to the space bar, and then creating any space + key actions that you need. So, a few weeks ago, I went remapping crazy and added a bunch of quick shortcuts to my .vimrc. This has really sped up my work, and I highly recommend Vim users try some or all of these.

In all of the examples below, <space> is the leader key. All of these work in normal mode, and a few in visual mode.

Working with Files

  • <space> w to save
  • <space> q to save and quit
  • <space> Q to quit without saving
  • <space> e to reload the file
  • <space> E to discard all changes to the file

Windows

  • <space> h, j, k, or l to move left, down, up, or right a window
  • <space> H, J, K, or L to move a window to the extreme left, down, up, or right of the screen
  • <space> o to rotate windows clockwise
  • <space> O to rotate windows counter-clockwise
  • <space> ] to make the window 10 columns wider
  • <space> [ to make the window 10 columns narrower
  • <space> + to make the window 10 rows taller
  • <space> - to make the window 10 rows shorter
  • <space> = to make the windows sized eqally
  • <space> s to split the window horizontally
  • <space> v to split the window vertically
  • <space> n to open the NERDTree window

Shell

  • <space> t and enter a shell command to run it.
  • <space> r to repeat the last-run shell command
  • <space> b to switch to the shell

Pasting

  • <space> p to paste the last yanked item in front of the cursor
  • <space> P to paste the last yanked item behind the cursor

Movements

(also works in visual mode)

  • ; to move a page down
  • ' to move a page up
  • <space> 4 to jump to the end of the line
  • <space> 6 to jump to the beginning of th eline

Indentation

(also works with multiple lines in visual mode)

  • . to indent to the right
  • , to indent to the left

Miscellaneous

  • <space> ; to add a semi-colon to the end of a line (as a JavaScript developer, this is golden).

I’m sure I will think of others, but these have really helped. Now, in it’s full glory, the key mapping section of my .vimrc. Feel free to copy/paste/edit. If you’ve got a great Vim key mapping, leave it in the comments!

let mapleader = "\<space>"
nnoremap <leader>w :w<CR>
nnoremap <leader>n :NERDTreeToggle<CR>
nnoremap <leader>q :wq<CR>
nnoremap <leader>Q :q!<CR>
nnoremap <leader>b :sh<CR>
nnoremap <leader>r :w<CR>:!!<CR>
nnoremap <leader>e :e<space>%<CR>
nnoremap <leader>E :e!<space>%<CR>
nnoremap <leader>h <C-W>h
nnoremap <leader>j <C-W>j
nnoremap <leader>k <C-W>k
nnoremap <leader>l <C-W>l
nnoremap <leader>H <C-W>H
nnoremap <leader>J <C-W>J
nnoremap <leader>K <C-W>K
nnoremap <leader>L <C-W>L
nnoremap <leader>o <C-W>r
nnoremap <leader>O <C-W>R
nnoremap <leader>p "0p
nnoremap <leader>P "0P
nnoremap <leader>g :%s/
nnoremap <leader>t :!<space>
nnoremap <leader>[ <C-w>10<
nnoremap <leader>] <C-w>10>
nnoremap <leader>- <C-w>10-
nnoremap <leader>+ <C-w>10+
nnoremap <leader>= <C-w>=
nnoremap <leader>s :sp<CR>
nnoremap <leader>v :vsp<CR>
nnoremap . >>
nnoremap , <<
nnoremap <leader>4 $
nnoremap <leader>6 ^
vnoremap <leader>4 $
vnoremap <leader>6 ^
vnoremap , <gv
vnoremap . >gv
nnoremap ; <C-d>
nnoremap ' <C-u>
vnoremap ; <C-d>
vnoremap ' <C-u>
nnoremap <leader>; $a;<esc>
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *