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.

66 Comments

  • wings io

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

  • solitaire tripeaks

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

  • I Driver Updater

    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

    Sie sind ausgezeichnet, ich bewundere Ihre Arbeit.

  • LeptoConnect

    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

    how do you synchronize scrolling?

  • spider solitaire

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

  • free fonts

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

  • Bas

    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

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

  • roofing contractors

    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

    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

    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

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

  • Deck Builder

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

  • Bend oregon hvac

    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

    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.

  • breastfeeding counselor

    Vim and touch typing have greatly improved my productivity, Thanks man.

  • Furnace Repair

    Great article-super helpful and well written piece

  • Fencing Lincoln NE

    Your article is very meaningful and informational. I really like your writing style.

  • retaining wall installation

    Does this work for our website about concrete services? It should be a nice addition if it does.

  • brick repointing

    Vim is a ground-breaking manager. Yet, in the same way as other ground-breaking things, now and then it tends to be utilized in under ground-breaking ways. One of those more fragile ways is to just alter each record in turn, skipping to and fro among proofreader and the shell or document wayfarer. Vim can without much of a stretch handle various documents simultaneously, and this post will show you a portion of the inherent orders that are accessible to assist. Altering numerous documents at the same time has numerous advantages. To start with, it forgoes the overhead of startup time and finding the perfect spot in the document. This typically isn’t a ton of time, however it can accumulate as you flip between various documents.

  • cookie clicker

    ​Really I appreciate the effort you made to share the knowledge. The topic here I found was really effective to the topic which I was researching for a long time.

  • bendhvacpros.com

    Thanks for this nice effort which you put here in the shape of this.

  • fence contractor

    Great website. Glad to found this good information.

  • Joffer

    Fabulous post, you have denoted out some fantastic points, I likewise think this s a very wonderful website. I will visit again for more quality contents and also, recommend this site to all. Thanks

  • tree service Ashburn VA

    I am learning Vim and it is looking good. I hope to master Vim by the end of the week.

  • Signs Little Rock

    Thanks for sharing this great information here.

  • guitar lessons

    Good article, but it would be better if in future you can about it. Keep the display

  • intermodal trucking

    Your blog post is quite interesting. It was really nice seeing this kind of informative post and I really enjoyed reading your post. Thank you for sharing.

  • life insurance

    Good job! Excellent article! Good reading! Just GREAAAAT!

  • driveway pavers in Newcastle

    This article was written by a real thinking writer. I agree with many of the solid points made by the writer.

  • ccr gov

    Awesome post! Thanks for the share.

  • Ving

    Great content, thanks for the update

  • Hellos

    This is great advice, thank you

  • bug sweep

    This is so informative.