open multiple files in vim

How to Open Multiple Files in Vim

Vim is a popular file editor available in almost every Linux distribution. It is used extensively by developers and system administrators to edit files. By default vim opens a single file in a single window. Often you may need to work with multiple files at the same time. Vim editor allows you to easily do this. You can open multiple files in different windows, or split an existing window to view the different files. In this article, we will learn how to open multiple files in Vim.


How to Open Multiple Files in Vim

The simplest way to work with multiple files in Vim is to use them in same session. Typically, developers create new session windows, one for each file. This is very tedious and time consuming. It is much easier to open all files in the same vim session. There are two ways to do this – from command line before opening the files, or from within vim editor. We will look at both these ways.


From Command Line

Here is the syntax to open multiple files from within command line. You just need to list the filenames/paths one after the other in a space separated manner.

$ vim file1 file2 ...

For example, if you want to open files data1.txt and data2.txt in same vim session, run the following command.

$ vim data1.txt data2.txt

Please note, if all your files to be opened are not present in your current working directory, you will need to provide full file path instead of mentioning only the filename.

When you run the above command, vim will open the file listed first among command line arguments. You can edit it and hit :wq to save changes, if you want. Only after you have saved the changes on your present file, vim will allow you to switch to the next file. We will look at how to switch files below.


From Within Vim

If you have already opened a file in vim editor, you can add more files to the vim session by entering :e followed by filename or path of the file to be opened.

:e filename

In this case also, vim will take you to the same interface that you get while opening multiple files from command line. Here too, in order to open another file in vim editor, you need to first save the file currently open in vim editor, using :w.


Switch Between Files

In both the above cases, you will be able to view only file at a time, and need to switch from one file to another. Before you switch from one file to another, you need to save changes in file currently open, by entering :w keys, or discard them by hitting exclamation mark (!). Vim provides numerous commands to switch between files. You need to run them in normal mode. Here is a list of commonly used commands to switch between files.

:n Or :bn - switch to the next file

:N Or :bp - switch to the previous file

:bf - switch to the first file

:bl - switch to the last file

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

:b filename - switch to a specific file filename(for file name file1, use “:b file1”)

:wall - save all open files

:bw - quit the current file

qall - quit all open files

:qall! - forcefully quit all the files, discarding the changes


Opening & Switching Between Multiple Windows

In above examples, vim will open multiple files in a single window, so you need to switch from one file to another all the time. But vim also allows you to open different files in separate windows and split your screen horizontally & vertically, to display them all at once on a single screen. Here is the command to open files file1 and file2 in two windows one below the other by splitting your screen horizontally.

$ vim -o file1 file2

Here is the command to open two files file1 and file2 in two windows displayed side by side by splitting your screen vertically.

$ vim -O file1 file2

You can also split your window from within vim editor.

If you already have vim editor open, you can split your window horizontally to display another file, using the following command.

:split filename

If you already have vim editor open, you can split your window vertically to display another file, using the following command.

:vsplit filename

In the above examples, if your files are present in different directories, or they are not present in current working directory, you will need to provide full file paths instead of filenames.

You can use these commands to cycle through your windows, when your screen is split horizontally or vertically.

Ctrl+w, j or Ctrl+w, Down arrow - switch to the next window below the current window

Ctrl+w, k or Ctrl+w, Up arrow - switch to the next window above the current window
Ctrl+w,l or Ctrl+w,Right arrow - switch to the next window to the right of the current window
Ctrl+w,h or Ctrl+w,Left arrow - switch to the next window to the left of the current window
ctrl+w,w - cycle through all Vim windows

Of course, there are many more shortcuts to work with multiple files in vim editor.

In this article, we have learnt how to open multiple files in vim. If you want to split vim editor to show the same file in multiple workspaces, refer to our steps to split vim screen horizontally & vertically.

Also read:

How to Split Screen Horizontally & Vertically in Vim
Fix Unable to Mount NTFS Error in Linux
How to Convert PPK to PEM in Linux
Linux History With Timestamp & User
How to Disable Swap in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *