# Kevin's Vim Tips and Tricks

## Overview

This document is a set of tips and tools that I've found to be useful over the years of using vim. This is by no means a "tutorial" - see :help tutor for that. I also don't claim to be a vim expert. These are just some of the most useful things I've discovered so far with vim.

## Remapping the escape key

The first thing you need to do to make using Vim easy is to remap the Caps Lock key to be a Ctrl key. This serves two purpose:

• It makes it much easier to invoke CTRL commands in vim, the shell, and all your applications.
• More signficant is that CTRL-[ is equivalent to hitting the Esc key. This means you can toggle back to command mode by just moving your left pinky to the Caps Lock key and your right pinky up to the [ key. Try it - with a little practice you'll find it much faster than leaving home row to hit the Esc key.

You can remap the Caps Lock key in one of several ways:

• On a recent Debian/Ubuntu system (using console-setup), you can edit /etc/default/keyboard:
XKBOPTIONS="ctrl:nocaps"

• Using a directive in /etc/X11/xorg.conf:
Option "XkbOptions" "ctrl:nocaps"

• Using an xmodmap. Create a file in your homedir called .Xmodmap with the contents:
remove Lock = Caps_Lock
remove Control = Control_L
keysym Caps_Lock = Control_L


## Windows and Buffers

Now that you can quickly navigate between insert and command mode, you should learn how to quickly navigate between files and buffers.

### The Basics

The first tip is pretty basic but essential: window creation and manipulation is done with the CTRL-w prefix. The commands you should know by heart are:

 CTRL-w n Creates a new window above the current window CTRL-w j Moves the cursor to the window below the current one CTRL-w k Moves the cursor to the window above the current one CTRL-w s Splits the current window - creates a new window editing the same file as the current window CTRL-w o Make the current window the only window. Closes all other windows.

There are many other commands, but these are the basics for using windows.

### Some useful keyboard maps

A lot of times, I'll have three or four windows open in vim. This presents a couple problems. First, each of the windows is small - you can't see that much information. Second, if I want to go from the bottom window to the top window, I have to hit CTRL-w k three or four times. To solve these two problems at once, I have two additional keyboard maps in my .vimrc file:

set winminheight=0      " Allow windows to get fully squashed

"
" Switch between windows, maximizing the current window
"
nnoremap <C-J> <C-W>j<C-W>_
nnoremap <C-K> <C-W>k<C-W>_


With these settings, if you type CTRL-j, you will move to the window below and maximize it in a single keystroke. Conversely, CTRL-k will move up a window and maximize it. This makes it much more efficient to switch between windows when you have many of them open.

Note the winminheight=0 setting. This allows the inactive windows to squish down to one line.

### Bufexplorer

One of the most useful extensions for vim is Bufexplorer. Download the zip file and extract it inside your ~/.vim/ directory (if you don't have one yet, create one - we'll be putting other stuff in there). Note that the later versions only work for vim 7.0 and above. If you are using an earlier version, install version 7.0.11 from the site.

Once you've installed it, rerun vim. Open a few different files using :n, and then type \be. This will open a list of all the buffers (files) you have edited. There are lots of options on how to sort and display the files, but personally I prefer the defaults.

Once you get the hang of bufexplorer, you won't need to close vim. Just leave it open, and it will accumulate buffers. You can use bufexplorer to re-open a file you had open earlier with ease.

Speaking of opening multiple files, there is a file/directory browser built in to vim. All you have to type is :n .<Enter> and a listing of the current directory will be displayed. Hit Enter on a directory to change to that directory and Enter on a file to edit that file. Recent version of Vim even have the ability to browse and edit files across the network!

## Tags

Tags are one of the best ways to navigate around a large set of files in vim. I'm sure you are familiar with the concept of tags, but there are a few tweeks you can make in vim to make them even more useful.

### Find the tags wherever you are

If you have multiple tag files (across different projects), or your current working directory changes, it is useful to have vim search recursively upwards for the tags file. To enable this, add to your .vimrc:

set tags=tags;/


(I used to like for vim to automatically change directory for the current open file, but I now employ a few macros in place of this. Having a stable working directory makes plugins happier and allows for things like :make to work better. The vinegar plugin also provides a handy "-" mapping to open the directory browser.)

### Jumping to tags

There are several ways to jump to a tag, the most basic is one of:

• :ta [tagname]
• CTRL-] when the cursor is on a name you want to jump to

but the problem with these two is that they always pick the first match. Many times there will be multiple matches and you then have to use :tn to find the right one. A better way (IMO) is using the :tj[ump] command. If there is more than one result, it presents a list of matches, otherwise it jumps straight to the result. Ways to invoke this are:

• :tj [tagname]
• g CTRL-] with the cursor positioned on the tag.
• CTRL-w g CTRL-] jumps to the tag in a new window.

By the way, :tj supports completion using Tab and CTRL-D. If you like completion, you may want to turn on wildmenu in your .vimrc.

## Searching

Vim offers a way to highlight the results of the current search: set hlsearch. The problem is that it stays on. You can turn it off, but you have to type :nohlsearch (over and over and over...).

I find it much more convenient to be able to toggle it on and off as I feel like it. To do this, I put in my .vimrc:

let g:mapleader = ","
set nohlsearch          " turn off highlight searches, but:
" Turn hlsearch off/on with <leader>h


You can then hit ,h to toggle the highlight on/off.

## My Preferences

These aren't tips, so much as a plug for some of my preferences.

### Favorite color schemes

• peaksea for gvim (previously called ps_color). Like the author says, it really is easy on the eyes. I highly recommend it. Put in your .gvimrc
colorscheme ps_color
syntax enable

• xoria256 is also a pretty nice looking. I alternate between it and peaksea.
• xterm16 for my console vim editing.
• Before I used ps_color, some of my previous favorite colorschemes were
* biogoo
* fnaqevan
* lanzarotta.vim. (from http://lanzarotta.tripod.com/vim.html, but isn't hosted there anymore for some reason.

### Favorite plugins

• Bufexplorer provides a great interface for switching between all your active buffers.
• CtrlP allows you to quickly find files recursively by typing just part of their name and/or path. Almost takes the place of tags! I used to recommand Command-T, but CtrlP is better: no need to compile anything, and it accomodates different working directory strategies.
• DirDiff performs a recursive diff on two directories in vim.
• Matchit provides much more enhanced parenthesis and keyword matching. Indispensible.
• the NERD tree provides a tree explorer sidepane. I don't use this a bunch, but sometimes it is useful when ctrlp and tags don't do the trick.
• Obsession provides continually updating session files. I use this for my main running vim editor so it picks up right where I left it each time I restart it.
• patchreview allows easier review of a patch file that spans multiple files.
• pathogen allows you to separate various plugins into their own directory inside a bundle directory.
• Rails provides a lot of small but nice features for working in a rails environment.
• snipMate provides frequently used "snippets" of code with easy editing and customization. I haven't had a lot of time to play with it yet, but this looks like an awesome plugin.
• Taglist provides a side pane with all the functions and classes listed for the current file you're editing.
• vinegar Simplfies the netrw/directory browser and provides a handy "-" mapping to open the browser.
• Xmledit makes editing HTML and XML files a breeze, with automatic tag matching and completion for you.
• YankRing implements the Emacs style kill ring in vim.