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

Posted on in Categories , , , , , , , last updated October 18, 2015

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.

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

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

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

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

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

Leave a Comment