Linux / Unix vi / vim: Open and Switch Between Multiple Files

by on November 20, 2012 · 12 comments· LAST UPDATED November 20, 2012

in

I am new to Linux sysadmin work. I am using vi / vim text editor to edit files located in /etc/ directory. I open files using

vi /etc/resolv.conf /etc/hosts

I can edit the /etc/resolv.conf file only. How do I switch between the two open files while using vim text editor under Linux / BSD / Apple OS X / Unix like operating systems?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsvi or vim
Estimated completion timeN/A

There are various commands and ways to handle multiple open files under vi / vim text editor running on Unix like operating systems.

Sample data files

Create a two text files as follows for demonstration purpose:
$ cat /tmp/foo
You can avoid reality, but you cannot avoid the consequences of avoiding reality.

And
$ cat /tmp/bar
If a man does his best, what else is there?

Syntax

To open multiple files at a time using a text editor called vi / vim use the following syntax:
$ vi file1 file2 fileN
OR
$ vi *.py

Examples

Open demo files, enter:
$ vi /tmp/foo /tmp/bar
OR
$ vi /tmp/{foo,bar}
To list open files:
:ls
Sample outputs:

  1 %a   "/tmp/foo"                     line 1
  2      "/tmp/bar"                     line 0

To switch to next file:
:n
OR
:bn
To switch to previous file:
:p
OR
:bp
To open specific file buffer called 10 (use :ls command to list all open file buffers):
:b10
You can also switch between all open files, using the following syntax:
:b foo
OR
:b bar
Modern text editor such as vim supports enhanced tab completion to get list of open file:
:b [HIT-TAB-KEY]
To save currently open file:
:w

Visual editor

To split the current window vertically:
CTRL+W v
To split the current window horizontally:
CTRL-W s
To switch between vertically open windows i.e. navigate through open windows:
CTRL-W h
and/or
CTRL-W l
To switch between horizontally open windows i.e. navigate through open windows:
CTRL-W k
and/or
CTRL-W j
Once switched you can load file using any one of syntax as discussed above:
:n
:b10
:p

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 12 comments… read them below or add one }

1 Sean - Linux Admin Show November 20, 2012 at 1:41 pm

vim -o file1 file2

will open the files in two windows instead of needing to flip between the files in one window.

Also, to add to your comments about switching between windows,

:sp file2

will open up file 2 below the current window, and

:vsp file2

will open up file 2 to in a vertical split.

Reply

2 thameera November 20, 2012 at 3:19 pm

Why not use tabs in Vim? IMO, that’s the easiest way to operate with multiple files. You can open files in tabs like this:

vim -p foo bar

And traverse the tabs using gt and gT.

Reply

3 Johan November 20, 2012 at 3:40 pm

You may also use `vim -p file1 file2` to open files in tabs instead.
Use :tabn, :tabp to switch between files (also possible to make appropriate key bindings for theses), or the mouse depending on how you’ve configured vim.

Reply

4 Sunil Kopparapu November 21, 2012 at 12:47 am

This is good especially for someone who uses vi and not the new vim!

Reply

5 bAsh November 21, 2012 at 1:27 am

As Johan suggested, one can use `vim -p file1 file2`, then use `gt` or `gT` to switch between tabs. If you have mouse mode enabled, one can also click on a tab title. To open more tabs, use `:tabe file3`

Reply

6 Jalal Hajigholamali November 21, 2012 at 5:08 am

Hi,
thanks..
vim is very powerful utility…

Reply

7 Przemo November 21, 2012 at 1:46 pm

Thanks for the post!
Ctrl-W Ctrl-W switches between files as well – it’s really handy for working with 2 files.

Reply

8 mrxtian November 21, 2012 at 3:37 pm

For many years I have used a similar vi Trick.

Vi has been able to flip between two files, the second being the alternate file use CTRL-^ to flip, even to the exact line and position on the line is retained.

vi 

Then you want to peak at your second file,

:e otherfile

Work away, find what you want, copy a region into a buffer, then hit CTRL-^ On a qwerty kbd, you actually have to press CTRL-SHIFT-6 and you are back to before you started editing the new file.

CTRL-^ flips you back. So you can flip back and forth just by remembering only one extra command.

The alternate file can be edited using :e # and you can have a number of alternates, :e 2# to edit the 2nd alternate file. But thats too much to remember, stick with CTRL-^

Reply

9 Sachin September 3, 2014 at 3:37 pm

Thanks for your support. It worked for me.

Reply

10 Scott November 21, 2012 at 7:02 pm

There is a subtle difference between using windows and tabs. Tabs are a lot like virtual desktops. In general, commands that operate on all windows affect the windows in the current tab. For instance, the :cd command will change the directory for all windows in the current tab that have not been changed with the :lcd command, but will leave windows in other tabs unaffected.

This is useful when you want to have more than one multi-file project open at once without directory changes affecting the windows belonging to other projects. But tabs do not affect the visibility of buffers; the buffers from all open tabs are visible in the buffers list from any other open tab. Hidden buffers belonging to one project may be viewed in any tab initially associated with a different project, which could cause some confusion if the projects have similar files.

If you need the current directory to remain consistent between all open files, use a single tab with multiple windows, or use the :buffers command to track which files are open and the :buffer command to switch between them. A convenient mapping, such as

nnoremap b :buffers:b

makes using this approach far more convenient when many buffers are open.

Reply

11 A Kong November 23, 2012 at 8:13 pm

Or you could use emacs. :)

Reply

12 B Kong July 13, 2013 at 6:44 pm

Or you could use Notepad++. :)

Reply

Leave a Comment

Tagged as: , , , ,

Previous Faq:

Next Faq: