Tuesday, June 29, 2010

Tricks to work with multiple files in Linux using Vi editor

First create the multiple files using touch command and now try to open these files at a time using vi editor as

vi file1 file2

This command will takes you to the first file, i.e. file1, that means you can edit the first file "file1" now. Once done, save the file and now you want to move to the next file, i.e file2.
For that you can use ":n" into your vi editor and press ENTER, then you will move to next file, that is file2.
Now you can do your work with file2 and now you want to come back to file1 again, for that use ":rew" into your vi editor, then you will be at the first file.

This sounds good if you are working with 2 files simultaneously, but what to do if you have number of files as file1, file2, file3, file4, file5,etc

Then for this also use the same command as above for opening the multiple files using vi as

vi file1 file2 file3 file4

Now you can follow the same procedure as mentioned to work with all these files, but one problem will arise this time is that when you are at the fourth file, i.e file4 and you have given ":rew" to come back to previous file, it will directly move to the first file, file1.

What will you do if you want to go to file2 or any particular file, for that you have to use

:e filename

e.g
:e file2 or :e file3

then you can move smoothly to the required files.

From this article, you can edit multiple files using vi editor.

2 comments:

  1. Yes is this information is very useful to deal with multiple files..
    Thanks Ravi

    ReplyDelete
  2. To deal with multiple files, I usually take multiple sessions. Now, it's very easy for me to deal those in same session.
    Thanks a lot !!! You saved much efforts for me.

    Regards,
    Sam

    ReplyDelete