Friday, July 23, 2010

Mapping the Leader and Some Useful Leader Commands

I mentioned the 'leader' in my previous screencast. The leader is a key that is central to many commands in normal mode. Think of the leader as another control key for Vim commands in normal mode. You should centralize many of your command maps around the leader as this is, by nature, an unused key. Here's how you map the leader and some useful commands to go with it:

The line necessary to map the leader is this:

let mapleader = "\\"

In this case, the leader is set to '\' (the other backslash is to escape that one so it doesn't escape the quote). This is the default, but you may have to explicitly use this. That's what I had to do. Even if you don't, it's probably good to put that in your vimrc file so you know you can always count on it. Also note that the leader doesn't have to be '\'. In fact, many people use '_'.

Here are some useful leader commands. As you can see, they are things you might see yourself doing frequently, and much easier, with the help of the leader. You can copy and paste this directly into your vimrc or into the command mode in Vim. For those of you that don't know, the quotes represent comments.


" Tab commands using the leader
map <leader>tn :tabnew<cr>
map <leader>tc :tabclose<cr>
map <leader>tx :tabnext<cr>

" NERDTree shortcut
map <leader>nt :NERDTree<cr>

" Shortcut for opening vimrc
map <leader>rc :sp ~/.vimrc<cr>


NERDTree is a plugin that closely resembles the tree-like file browser in things like TextMate or Aptana Studio. I'll talk more about this one later. Also, some of you may be unfamiliar with tabs. But don't fret! I'll also talk about that in another post.

Please, post any requests for tutorials, tips, tricks, or anything you can think of. I'd be more than happy to talk about them!

No comments:

Post a Comment