How to work with tabs in vim

As you may know, vim is one of the most powerful text editors. It's been around for a long time and most advanced UNIX administrators do all their tasks with it. I had tried working with vim a few times, but I always ended up using eclipse and quanta plus for developing my web application. Lately, I noticed that both eclipse and quanta plus use a lot of resources and my 1GB RAM was not enough for running eclipse, quanta plus, Amarok, Firefox, pidgin, etc simultaneously. Moreover, I started working in a company that I had to develop my code over SSH. That's why, I decided to give vim another try and learn more about it. At the end I realized that vim is developers' best friend, not only for editing files over SSH, but editing local files.

One of the best things I found in vim, was tab editing capability of it. It finally made me uninstall eclipse and quanta plus from my computer. In this article, I'm going to introduce you with the tab editing feature of vim.

Managing Tabs: The first thing you want to know about working with tabs is that how you open them. The following commands can be used to open a new tab and edit a file in a new tab respectively.

  • :tabnew
  • :tabedit your_file

As you might notice, you can use tab after tabedit command to have faster access to files.

Navigating Through Tabs: Once you open new tabs, there should be a way to navigate through them. Fortunately, there are many ways for that. The easiest way for navigating between your tabs is to use the mouse. You can also use your keyboard to navigate through them by pressing gt in normal mode. This command takes you to the next tab immediately.

Do you think this command is always good? I don't think so. Let's supposed that you have 15 tabs open and you're in tab number 10, and you need to edit something in tab number 5 real quick, With gt, you have to press it 10 times to reach that tab, which is a big pain in the ass. But, don't worry. Vim enables you to specify a number before your gt command, so that you can jump to your desired tab immediately. In this case, you can use 5gt to jump to 5th open tab.

Do you think that's enough with tab navigation? Well, not for me. What if you want to jump to 8th tab and you don't want to count to find about the number of your desired tab? My solution to this is to edit your vimrc file and map two shortcut keys to :tabnext and :tabprevious commands. That is, I added the following entries to my vimrc:

  • map :tabnext
  • map :tabprevious

Tab Commands: You can use pretty much all commands that you use with files in tabs, as well. For example, if you want to close a tab, simply type :q or if you want to save a file use :w. You can use :qall or :wall to close or save all tabs at once.

There is a very interesting command called :tabdo which enables you to execute a command in all your tabs at once.

For example, I use YUI (Yahoo User Interface) for developing my web apps. Yahoo hosts all necessary Javascript files in their servers. That is, I just point all my stylesheet files to their Javascript files. They release new updates once in a while, and I always want to keep my code up-to-date with their latest versions.

I load different YUI files in different style-sheets, and they all have the same version of YUI. That is, I can easily update each of my file to the new version without changing everything manually by using the following method:

  • vim -p `grep -l 2.5.2 *.xsl`
    • -p tells vim to open all files in tabs
    • grep -l 2.5.2 *.xsl, looks for all xsl files in my current directory which contain 2.5.2(older version of YUI). Adding -l tells it to return only the name of the file.
    • Putting the second command inside `` makes its output the input for the second command

    Using these command, I was able to open all xsl files that contain 2.5.2 in tabs. Note that, you can only open a specific number of tabs using vim, which can be set by adding the following setting to your vimrc file:

  • set tabpagemax=30
  • Now, I want to replace all 2.5.2 to 2.6.0. I can do that by using :tabdo and replace command in vim like this:

  • :tabdo %s/2\.5\.2\/2\.6\.2/gc
    • f you use %s/2\.5\.2\/2\.6\.2/gc in a single file without :tabdo, it will find all 2.5.2 and ask you if you want to replace them by 2.6.2. If you press a, it will replace everything at once.

    Note that because “.” is a special character in vim, you have to specify “\” before it, if you're looking for it in a document.

    Saving your tabs in sessions: Sometimes you open a whole bunch of files in tabs, but it's late, you're not done with your work and your wife or girlfriend is waiting for you at home. You can use the following command to create a session to stores information about all open tabs and other important settings for the current instance of vim:

  • :mksession your_session.vim
  • The day after, you go back to work and use the following command to open your last night session:

  • vim -S your_session.vim
  • There are a lot more you can do with vim. In this article I just wanted to share with you those featuresof vim that I find most interesting and use them extensively in my development work. Please feel free to add any trick that you find interesting.

    Published On: July 8, 2009 --- Views: 10195

    Tags: vim tabs


    There are 2 comment(s):

    On June 2, 2011, Mozy Review says:

    Are tabs preferred to just using multiple buffers? Always confused me about VIM when compared to Emacs.

    On June 22, 2011, Mohammad says:

    "Are tabs preferred to just using multiple buffers? Always confused me about VIM when compared to Emacs." Not necessarily. You're free to use whatever suits you better. Personally I use a combination of tabs and splits.


    | More

    Back To The Blog Page

    All Rights Reserved
    Powered By Mohammad Mohtasham