How to open two different files using the vi editor?
I have two files in different locations and I want to open these two files with the same vi
command. How can I do it?
- File 1 location:
/home/rs/rest.pl
- File 2 location:
/home/dev/grd.pl
As noted by Zanna, use multiple arguments to Vi. However, Vi(m) by default doesn't show the files at once. They're loaded into buffers and and you can switch to the next (or previous) buffers using :bn
and :bp
. If you want to see the files at the same time, use windows (splits):
vim /some/file1 /some/file2 -o # horizontal split
vim /some/file1 /some/file2 -O # vertical split
Or tabs:
vim /some/file1 /some/file2 -p # Open up to 10 files in tabs
However, buffers are what Vim actually uses for manipulating files, tabs and windows are merely ways to visually arrange them. Do get used to using buffers directly, instead of via multiple windows or tabs.
(These options also apply to Ubuntu's vi
, which is vim.tiny
by default. vim-tiny
is built with +windows
, so tabs and windows are enabled.)
See also:
- Vi & Vim: What is a more efficient way to use buffers?
- Open an existing file from Vim
- SO: Why do Vim experts prefer buffers over tabs? and Using Vim's tabs like buffers
- Vim help: Editing with multiple windows and buffers
You can call call vi (or vim) with multiple arguments
vi /home/rs/rest.pl /home/dev/grd.pl
You are in the first file, but both are open. You can switch between them using :n
(next file) and :N
(last file) (press esc to get out of insert mode if necessary).
I notice those files are in different users' home directories, so you may not have permission as a normal user. To edit files that your user does not have sufficient permissions to edit, you will need sudo
. You can run the command with sudo
initially, or use a trick when saving the file :w !sudo tee %
If you like to view the two files tiled horizontally, use
vi -o /home/rs/rest.pl /home/dev/grd.pl