By default, Vim starts with a single window, which is enough for editing a single file. But sometimes, you may have to work on multiple files. Vim makes it easier to work with multiple files at once with its window management system, allowing you to work on multiple files at the same time within a single Vim session. You can choose to view one file at a time, or you can split your workspace to view multiple files side by side.This article will explain how to open multiple files and windows in Vim, as well as how to switch between multiple files and windows.

Opening and Switching Between Multiple Files

Editing multiple files in the same session can speed up your work. When editing multiple files, it takes some time to exit one session and then open a new session for another file. Staying in the same session helps you to efficiently work on multiple files at the same time.

Opening Multiple Files in Vim

You can open multiple files at the start of the Vim editing session from the command line, or at any time from inside the Vim editing session.

From the Command Line

To open multiple files in Vim at the start of the Vim editing session, specify the files as follows:

By default, the first file (file1) will open in Vim. Go ahead and edit this file, and once you are finished, save it using the :w command. After saving, you can switch to the next file.

Opening and Switching Between Multiple Files and Windows in VI vim

From Inside Vim

You can also add additional files at any time during the Vim editing session. For instance, while editing file1 and file2, you can add a file3.

To edit another file during the Vim session, first save the current file using :w in Normal mode, and then use the following command:

Switching Between Files in Vim

Note that to switch to another file, you will first have to save the current file, if you have made some changes to it. If you have not saved the current file, Vim will not let you switch files, unless you specify with the exclamation mark (!). This notation will discard the changes made to the current file and forcefully switch to the other file. For instance, if you have made some changes to file but you decide to discard those changes and switch to another file, use the exclamation mark (!) following the commands listed below.

Note: Issue all the below commands in Vim’s Normal mode.

To switch to the next file, issue either of the following commands:

Or

To switch to the previous file, issue the following command:

Or

To switch to the first file, issue the following command:

To switch to the last file, issue the following command:

To switch to a specific file number, issue the following command (for file number 3, use the command “b 3”):

To switch to a specific file filename, issue the following command (for file name file1, use “:b file1”):

To switch back and forth between two files, issue the following command:

Where the # symbol represents the number of the alternate file.

Or, you can use the following shortcut:

To save all open files, issue the following command:

To quit the current file, issue the following command:

To quit all open files, issue the following command:

To forcefully quit all the files, discarding the changes, issue the following command:

To list all open files, issue the following command:

Opening and Switching Between Multiple Files and Windows in VI vim

In the ls: output, the % refers to the current file, while the # refers to the alternate file.

Opening and Switching between Multiple Windows

By default, the Vim opens only a single window, even if you have opened multiple files. However, you can view multiple windows open at once in Vim. This can be achieved by splitting the windows horizontally to create two windows of the same width, or vertically to create two windows of the same height. Using this method, you can view multiple windows at once in a single Vim session.

From the Command Line

You can open multiple windows from the command line at the start of the Vim editing session. To do so, split the window either horizontally or vertically using the following methods.

Splitting Windows Horizontally

To split the window horizontally, use the following syntax:

This command will split the Vim editing session horizontally, into two equal-sized windows.

Opening and Switching Between Multiple Files and Windows in VI vim

Splitting Windows Vertically

To split the windows vertically, use the following syntax:

This command will split the Vim editing session vertically into two equal-sized windows.

Opening and Switching Between Multiple Files and Windows in VI vim

Splitting Windows inside of Vim

You can also split windows during the Vim editing session. To do so, open a file in Vim, and then split it either horizontally or vertically using the following methods.

Splitting Windows Horizontally

To split windows horizontally in Vim, use the following command:

You can also use the following shortcut:

When no argument specified, the current file will be displayed in both split windows. To open another file in a new window, use the command :split, followed by the desired filename, as follows:

Splitting Windows Vertically

To split windows vertically in Vim, use the following command:

You can also use the following shortcut to split the windows vertically:

With Vim, you can split the windows even further, either horizontally or vertically.

Opening and Switching Between Multiple Files and Windows in VI vim

Switching between Horizontal Windows

To switch to the next window below the current window, use either of the following shortcuts:

Ctrl w, j or Ctrl w, Down arrow

To switch to the next window above the current window, use either of the following shortcuts:

Ctrl w, k or Ctrl w, Up arrow

Switching between Vertical Windows

To switch to the next window to the right of the current window, use:

Ctrl w,l or Ctrl w,Right arrow

To switch to the next window to the left of the current window, use either of the following shortcuts:

Ctrl w,h or Ctrl w,Left arrow

To cycle through all Vim windows, use the following shortcut:

To close the current window, use the following command:

You can also use the following shortcut:

To close all windows except the current window, use the following command:

You can also use the following shortcut:

Conclusion

With Vim’s window management system, you can handle multiple files at the same time quickly and efficiently. This article showed you how to open and switch between multiple files and windows in the Vim text editor. Hopefully, this should make it easier for you to use this amazing text editing tool.

About the author

Opening and Switching Between Multiple Files and Windows in VI vim

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.