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:

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

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

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 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:

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:

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:

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:

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:

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.

One Comment

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.