How to open two different files using the vi editor?

18,690

Solution 1

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:

Solution 2

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 %

Solution 3

If you like to view the two files tiled horizontally, use

vi -o /home/rs/rest.pl /home/dev/grd.pl

enter image description here

Share:
18,690

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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
  • nalzok
    nalzok almost 7 years
    +1 for the :w !sudo tee % trick!