Open multiple files with vim
There are many instances when it’s useful to have multiple files open in
, but if you aren’t familiar with this tool you can find yourself needlessly jumping around between multiple windows.
If you are not already using vim start by opening a command prompt and type
, once you’ve become familiar with how to navigate, search, and edit a document with
this post will make more sense to you.
Use vim to see what is different between two files
There are several ways to find differences between two files on a Linux server or desktop. I like to use
when I’m scanning a configuration file for recent changes from an earlier iteration ( assuming of course that there is a backup of the last known, good, configuration).
Comparing two files is a common task and there are several ways to view the differences between multiple files, but occasionally you may want to do this visually side by side.
Create two files
looks like this:
This is a file to test diff file 1
looks like this:
This is a file to test diff file 2
can open two, three, or four files and
will open the files and highlight the line that is different. Try it.
vim -d file1 file2
You should see something similar to this:
Notice how the file opens with the cursor set to the line that is different, this is so that you can quickly get to work at the exact place where the files differ. Also helpful, is the fact that
will automatically highlight the entire line to quickly draw your attention to the differences.
highlights a use for the split command. If you are working on a file and want to look at another file at the same time. Maybe to use some lines as a template, or just to compare an old configuration to a new one you can use the
And then issue the
command to open
command will split the screen horizontally:
and this time look at the vertical split by issuing the
This time, as the name suggests, the window will split horizontally allowing you to view files side by side. Personally I find this format to be the more useful of the two.
You can navigate between each split by using the key combination
and then the direction key in which you want to move. So, to move from
you would hit
and then the right arrow (or the L key if you don’t like moving your hand quite so far).
There are other fancy things you can do with files in
. If you are feeling a little wild look up tabs and buffers, both of which might make your life a little easier if you aren’t already using them.