Thursday, April 30, 2009

Using tabs in vim - short tutorial


With Vim 7.0, you can use tabs within vim. So now you can consolidate all your sessions into one window. Lets see some of the commands which will help you to explore the tab feature.

How to open a new tab in vim ?

:tabnew
:tabedit

:tabnew or :tabedit alone will open a new tab with empty buffer
:tabnew filename or tabedit filename will load the file in new tab

How to open files in separate tabs at start-up?

The following will open the 4 files in 4 separate tabs.

$ vim -p file1 file2 file3 file4

How to move between tabs ?

:tabnext (or :tabn)
:tabprevious (or :tabp)

or use gt in normal mode (Go to the next tab page. Wraps around from the last to the first one.)

:tabfirst (jump to the first tab)
:tablast (jump to the last tab)
:tabrewind

vim numbers the tabs from 0. So

:tabmove 2 (or :tabm 2)

will move to the 3rd tab.

Operation on all tabs ?
Now if you need to do a "search and replace" on all the tabbed files you have opened, tabdo will help you.
e.g. if you want to replace unit16 with unit32 in all the tabs

:tabdo %s/unit16/unit32/g

How to close tab(s)?

:tabclose (Close current tab page)
:tabonly (Close all other tab pages)

Help ?
For more on vim tab feature help

:help tab-page-intro

Also help page from vimdoc

9 comments:

Unknown said...

tabedit .

# This opens a new tab and prompts for the file to open in it. Use the arrow keys to navigate and press enter on top of the file you wish to open.

Unknown said...

@bicchi,thanks for the tip. :-)

Yoander Valdés Rodríguez said...

Nice short tu...

Adithya Kiran said...

After opening tabs, one can switch between the tabs with the help of mouse, just like firefox browser tab navigation. Here is the tip

1.Keep vim in command mode by pressing Esc and then type
:set mouse=a
and press enter.

2 You are done!!. From now on wards click on any tab page for instantaneous open, instead of :tabmove command

Cheers!!

Unknown said...

@Adithya, this is a great tip. Thanks. Keep in touch.

kezesb said...

Also worth to note that you can switch to the most recently used tab using gl with this little script (I don't know who was the original author):

" Most Recently Used Tab Page
au TabLeave * let g:MRUtabPage = tabpagenr()
fun MRUTab()
if exists( "g:MRUtabPage" )
exe "tabn " g:MRUtabPage
endif
endfun
noremap <silent> gl :call MRUTab()<Cr>

Senthil Kumaran said...

Hi Jadu,
Not sure if there is a command line way to switch between the opened tabs.

Unknown said...

@Senthil,

do you mean

"How to move between tabs ?" mentioned above ?

Senthil Kumaran said...

Okay, got it. Just like anything in vim, you can have numeric prefixes.
1gt will go to 1st tab
4gt will go to 4th tab
ngt will go to nth tab

that is much better than :tabnext and :tabprev

© Jadu Saikia www.UNIXCL.com