Tech

Guides
 

Use tabs to open multiple files in vim

By Vincent Danen, Special to ZDNet Asia
Monday, August 17, 2009 11:07 AM
Read about the basic tab commands and find out how to combine them with key bindings to make the most of a powerful editing tool.

Most people use a text editor to edit one file at a time. If more than one file needs to be edited at once, a GUI user can have multiple windows or tabs open with the files, but someone on the command-line will usually swap between different consoles or terminal windows.

With vim, you can use tabs also, just like you would in gvim or any other GUI editor.

You can open files in multiple tabs in two ways. The first is to execute vim with each file specified:

$ vim -p file1.txt file2.txt file3.txt

This will open three tabs, each containing the specified file. The second way to open files is to create new tabs within vim and open files using :tabnew file.txt in command mode, which opens a new tab and edits the specified file (if you omit the filename, you will get a new empty document).

To close a tab, use :tabc. To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively).

You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast). Finally, :tabs will give you a list of open tabs.

Each tab is a separate editing instance, which means that buffers are not shared between tabs. This also means that you can have multiple windows, or view ports, open in each tab (i.e., one file to edit on the top half of the screen, another file in the bottom half). For instance, if you have multiple tabs open and in one of them want to refer to vim's help system (i.e., :help tabs), you will have all of your tabs open, and in the foremost tab a split-screen editing view.

Using the various :tab* movement commands will still work, and using the [Ctrl]+W key bindings to manipulate windows (such as [Ctrl]+W then the down or up arrow to move between windows), will also work. Between tabs and windows, a single vim window can suddenly conveniently handle a large number of files at once.

Finally, to close a tab, close the file in it using :q or :wq if you need to save changes first.

Throw in some key bindings, and using tabs becomes even more convenient. For instance:

map <C-t><up> :tabr<cr>
map <C-t><down> :tabl<cr>
map <C-t><left> :tabp<cr>
map <C-t><right> :tabn<cr>

Adding the above to ~/.vimrc provides for a very convenient way to move between tabs. When in insert mode, press [Esc] to get to command mode, then use [Ctrl]+T plus a directional arrow to go to the tab you want: up to go to the first tab, down to the last, and left or right to go to the previous or next tab.

Using tabs is a great way to edit multiple text files in vim, without having to swap between terminals or consoles. Combining tab and window views, with some nice key bindings, makes it a simple, powerful, and very usable tool.

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years.



WORTHWHILE?

0

0 votes
Blog

Talkback 0 comments

There are currently no comments for this post.


Guest user

Guest user

Level: 
Joined: —
Already a member? Log in »



 

Loading...

Whitepapers/Case Studies

Downloads

Open Source News



Tech Jobs Now!

Tags

  1. apache software foundation
  2. canada
  3. fedora project
  4. gnome
  5. linux
  6. linux distribution
  7. mandriva
  8. mysql
  9. open source
  10. operating systems
  11. password
  12. red hat inc.
  13. repository
  14. security
  15. server
  16. ssh
  17. tool
  18. ubuntu
  19. vincent danen
  20. web