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