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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 9 comments… read them below or add one }
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.
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.
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.
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`
Hi,
thanks..
vim is very powerful utility…
Thanks for the post!
Ctrl-W Ctrl-W switches between files as well – it’s really handy for working with 2 files.
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,
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-^
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
makes using this approach far more convenient when many buffers are open.
Or you could use emacs. :)