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

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
Difficulty level Easy
Root privileges No
Requirements vi or vim
Est. reading time N/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.

$ 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:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 13 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • Sean - Linux Admin Show Nov 20, 2012 @ 13:41

    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.

  • thameera Nov 20, 2012 @ 15:19

    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.

  • Johan Nov 20, 2012 @ 15:40

    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.

  • Sunil Kopparapu Nov 21, 2012 @ 0:47

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

  • bAsh Nov 21, 2012 @ 1:27

    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`

  • Jalal Hajigholamali Nov 21, 2012 @ 5:08

    vim is very powerful utility…

  • Przemo Nov 21, 2012 @ 13:46

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

  • mrxtian Nov 21, 2012 @ 15:37

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

    • Sachin Sep 3, 2014 @ 15:37

      Thanks for your support. It worked for me.

  • Scott Nov 21, 2012 @ 19:02

    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.

  • A Kong Nov 23, 2012 @ 20:13

    Or you could use emacs. :)

  • B Kong Jul 13, 2013 @ 18:44

    Or you could use Notepad++. :)

    • Dawid Oct 18, 2015 @ 15:45

      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! :)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum