Tuesday, November 25, 2008

Tabs

It took tabs a while to grow on me when using Vim. This is mainly due to the flexibility that buffers and split windows provide; however, once I realized tabs could act as a convenient way of grouping split windows, I was sold. There's nothing complicated or confusing about using tabs, but a few mappings will make your life easier. Add the following to your vimrc.

let mapleader = ","
map <leader>tt :tabnew<cr>
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove
map <leader>tn :tabnext<cr>
map <leader>tp :tabprevious<cr>

I've already been called a n00b once on this blog for not seeing the value of the comma motion (repeat latest f, t, F or T in opposite direction). If you use it regularly, you'll obviously want to choose a different map leader.

Now that your mappings are setup, using tabs should be quick and intuitive.

* Press ,tt to open a new tab
* Press ,tc to close the current tab
* Press ,tm [number] to move to tab [number]
* Press ,tn to move to the next tab
* Press ,tp to move to the previous tab

In standard Vim, the tab list will appear at the top of your editing window. Gvim provides the industry standard GUI tabs. Obviously there are more tab commands available, but these basics should get you pretty far.

6 comments:

Anonymous said...

I think you need to add <leader> to the mappings if you want the comma in front.

Travis Whitton said...

You're absolutely correct. I forgot to escape the less-than and greater-than symbols. Thanks for catching that.

Jade Robbins said...

I love tabs! If you don't remap them though they are actually just as easy as you have set up here.

If you do the :tabnew command you can specify a file name (I guess i rarely open an blank file in tabs, usually an existing one), and gt moves you to the next tab and >number<gt moves you to the specified.

I guess I just don't think the key mappings really save that much time! Then again though, I'm a little OCD about keepings things vanilla :D

Jade Robbins said...

Whoops, I was backwards on my less than and greater than there, sorry :D

graywh said...

Not only can "tabs [act] as a convenient way of grouping split windows", but that is their primary (and arguably, only) purpose--:help tab-page-intro. Lots of vim newbies try to use the tabline as a replacement for :buffers/:files/:ls.

And there's not much reason to remap :tabnext and :tabprevious because there's already gt/gT and ctrl-pgup/ctrl-pgdown.

Mathias Stearn said...

MiniBufExpl Provides a tab-like interface similar to what you'd expect from other editors. I like to bind shift-H and shift-L to :bp and :bn so its easy to switch "tabs". (ctrl-[hjkl] is great for window navigation)