Mastering Vim: Working with multiple files

In this blog post, I’d like to show you how to use Vim to open and edit multiple files.
Use the right techniques and you’ll be even more productive in handling multiple files at the same time.

This is the follow up of Mastering Vim: Opening files.

Opening multiple files

Opening multiple files in vim is straight forward. Just append all the filenames as CLI args and you’re ready to go:

vim /path/to/file1 /path/to/file2 /path/to/file3

With the Vim opened, you can now see the first file.

To jump between the files you can use the following vim commands:

:n(ext)  # jumps to the next file
:prev    # jumps to the previous file

Easy, isn’t it? But now you can’t really see what files are available for editing, right? Just keep on reading and learn more about tabs.

Using tabs

Vim is awesome, and because of that it is also supporting tabs out of the box – at least since Vim 7.0.

So let’s stick to the last example, but this time we want to open the 3 files in separate tabs by using the -p CLI flag:

vim -p /path/to/file1 /path/to/file2 /path/to/file3

Vim is launched as before, but this time all files will be opened in tabs instead of hidden buffers.
The tab bar is displayed on the top of the editor / window.

Of course you can also open a new tab when you’re already in Vim in the normal mode:

:tabe [/path/to/file] (command-line command)

Interfer with tabs by staying in normal mode and using the following commands / keystrokes:

  • Jumping to the next tab is either gt (normal mode command) or :tabn[ext] (command-line command)
  • Jumping to the previous tab is either gT (normal mode command) or :tabp[revious] (command-line command)
  • Jumping to a specific tab is ngt (normal mode command), where n is the tab index starting by 1
  • Closing the current tab is :tabc[lose] (command-line command)

Looks even more promising. But you still might ask yourself how you can edit multiple files in the same window, right? Let’s see that!

Splitting the window

Of course vim can also display multiple files in one single window / workspace.
Just use the integrated split feature of Vim.

To split the window you can use one of the following command-line commands in Vim:

:sp[lit]   [/path/to/file]  # splits the window horizontally [and opens the file]
:vs[plit]  [/path/to/file]  # splits the window vertically [and opens the file]

Now you’ve multiple windows open in Vim. Just see them as “multiple Vim’s” aligned in a single terminal window.

Handling the windows can be tricky, but you’ll get used to it after a short time 😉
Just stay in the normal mode and use the following commands / keystrokes:

  • Jumping between windows is Ctrl-w <cursor keys>Ctrl-w [hjkl], or Ctrl-w Ctrl-[hjkl]
  • Jumping to the next window is Ctrl-w w or Ctrl-w Ctrl-w
  • Jumping to the previous window is Ctrl-w W
  • Jumping to the last accessed window is Ctrl-w p or Ctrl-w Ctrl-p
  • Closing the current window is Ctrl-w c or :clo[se]
  • Make the current window the only one and close all other ones is Ctrl-w o or :on[ly]

Tabs, windows and workspaces

Now as you know windows & tabs, please note the following.

Tabs are not classic “file tabs” as in most editors. Instead of it, tabs are like workspaces or window layouts. By default, each tab has exactly one window, and each window has exactly one file. It looks a bit like this:

Vim editor -> Tab(s) -> Window(s) -> File

Tabs are our workspaces, and you can easily split the window within the tabs, which means you can have multiple windows in a single tab. So a tab can “host” one or more windows, but not the other way around! Each tab is responsible for its own workspace / window layout.

To display the contents, windows and tabs simply use the following command-line command:

:tabs

Using the mouse

Now, remembering all those keystrokes can be tricky and moving around in windows & tabs can be frustrating in the beginning.
Though, if your terminal has integrated mouse support, then you might be able to use the mouse instead of the commands.
Just activate the mouse support first:

:set mouse=a

After that you should be able to change windows and/or tabs via mouse click.
If it doesn’t work, make sure your terminal has mouse support and it’s activated.

31 Comments

  • wings io Reply

    If your terminal has integrated mouse support, then you might be able to use the mouse instead of the commands.

  • solitaire tripeaks Reply

    THIS is the right answer! Very nice to have consistent tab key shortcuts.

  • I Driver Updater Reply

    Legitness of our list belongs to our staff, who personally tried dozens of driver updaters and then reported their unbiased reviews. The list is based on the reports sent to me by different developers by personally trying several potential software. If you are struggling for the best driver updater, then follow this comprehensive article carefully.

  • Rocketpayz Reply

    Sie sind ausgezeichnet, ich bewundere Ihre Arbeit.

  • LeptoConnect Reply

    I know this is one of the most meaningful information for me. And I’m animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help.

  • sourabh Reply

    how do you synchronize scrolling?

  • free fonts Reply

    Your subject may be a source of insight for me, thank you.

  • Bas Reply

    This article helped me getting more productive with Vim, thanks for that.

    One note on ngt for jumping to a specific tab: 0 is the index of the first tab (using vim 8.1.2269)

  • Hvac Software Reply

    Thanks for showing on how to do it. Very useful.

  • roofing contractors Reply

    I do enjoy the manner in which you have framed this specific issue and it really does provide me some fodder for consideration.

  • protège-gouttière Reply

    Many thanks for making the effort to discuss this, I feel strongly about this and like studying a great deal more on this topic. If possible, as you gain knowledge, would you mind updating your webpage with a great deal more information? It’s very helpful for me.

  • Comercial Concrete Reply

    This articles helps me more.Thanks for your sharing, I will pay more attentions to your blog. Looking forward to your better and better articles. See you next time.

  • Once you have the three or four (or twenty) files open, you can leverage Vim’s built-in session saving ability to take that entire set up and save it for later. This is particularly useful when switching between tasks in a software project.

  • io games Reply

    Your article is very special because its content is very good.

  • Deck Builder Reply

    What a wonderfully written piece.
    I enjoyed reading this very much.

  • Bend oregon hvac Reply

    For someone new like me, you’ve introduced the topic fairly effective that I can easily grasp your points. I look forward to more practical entries like this. thanks 🙂

  • Jamaican musician Reply

    I am grateful to you for sharing a phenomenal article here, old buddy. There’s some excellent substance in this article, and I’ll try to impart this article to my companions. That would be the best thing for me.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.