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

last updated in Categories , , , , , , ,

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?

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.

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


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


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

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

To switch to next file:
To switch to previous file:
To open specific file buffer called 10 (use :ls command to list all open file buffers):
You can also switch between all open files, using the following syntax:
:b foo
:b bar
Modern text editor such as vim supports enhanced tab completion to get list of open file:
To save currently open file:

Visual editor

To split the current window vertically:
To split the current window horizontally:
To switch between vertically open windows i.e. navigate through open windows:
To switch between horizontally open windows i.e. navigate through open windows:
Once switched you can load file using any one of syntax as discussed above:


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

13 comment

  1. 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.

  2. 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.

  3. 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.

  4. 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`

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

  6. 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.


    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-^

  7. 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.

    1. On a Linux server? Over SSH? Yeah! I’m going to install a GUI and Wine on a production server and forward X over SSH so that I can use Notepad++. Sounds like fun! :)

    Still, have a question? Get help on our forum!