vim

Opening and Switching Between Multiple Files and Windows in VI

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:

$ vim file1 file2

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.

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:

:e filename

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:

:n

Or

:bn

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

:N

Or

:bp

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

:bf

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

:bl

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

:b number

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

:b filename

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

:e#

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

Or, you can use the following shortcut:

Ctrl+^

To save all open files, issue the following command:

:wall

To quit the current file, issue the following command:

:bw

To quit all open files, issue the following command:

qall

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

:qall!

To list all open files, issue the following command:

:ls

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:

$ vim -o file1 file2

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

Splitting Windows Vertically

To split the windows vertically, use the following syntax:

$ vim -O file1 file2

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

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:

:split

You can also use the following shortcut:

Ctrl+w,s

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:

:split filename

Splitting Windows Vertically

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

:vsplit

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

Ctrl+w,v

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

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:

ctrl+w,w

To close the current window, use the following command:

:close

You can also use the following shortcut:

Ctrl+w,c

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

:only

You can also use the following shortcut:

Ctrl+w,o

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

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. He blogs at LinuxWays.