Vi Editor Tips

To open multiple files on a window

 

  • Horizontal split

 

vi -o file-one file-two

 

  • Vertical split

 

vi -O file-one file-two

Switch between the panes you are using.

CTRL + W + Left key to activate left windows
CTRL + W + Right key to activate right windows
CTRL + W + Up key> to activate to windows above current one
CTRL + W + Down key> to activate to windows down current one

 

  • Split Open one file at a time

 

vi file-one

To open the second file, go to the command mode (Esc)

:new file-two

OR

:split file-two

Search and Replace

Go in command mode and type:

Replace First occurrence of string

:%s/string-to-replace/replace-with-string/

Replace All occurrence of string

:%s/string-to-replace/replace-with-string/g
(Visited 1 times, 1 visits today)
Tags:
Category: SUSE Linux Enterprise Desktop, Technical Solutions
This entry was posted Tuesday, 21 July, 2009 at 9:59 am
You can follow any responses to this entry via RSS.

Comments

  • mfaris01 says:

    True command line.
    Thanks. Never knew about the split part – Sweet!
    Mike…

  • tmstone835 says:

    VI is a very powerful text editor as you can see but I think there are a couple of other options for people using your tip information.

    You can simply toggle between document windows by pressing CTRL + ww instead of using the arrow keys.

    You can also open more than just two documents at a time, just add a third file name to the vi -o or vi-O command. Example vi -o test1 test2 test3 test4.

    You can quit or write quit them all at once by adding an “a” after your quit command. Example: Use :qa or :wqa

    Tom S.

  • aburgemeister says:

    Just to make sure it is clear to those starting out with vi (especially those with prior regular expression experience) the search/replace works as described above for varying definitions of “works”. 🙂

    The ‘%’ at the beginning of the search/replace line (in command mode) means that the regex should be tried on all lines. The ‘g’ and the end of the regex tells whether or not the pattern should be applied multiple times PER line. So with the following input:

    this is a test this is a test
    this is a test this is a test
    this is a test this is a test

    The following would replace ‘this is’ with ‘this is not’ the first time on all lines, but not the second time:

    %s/this is/this is not/

    While the next example would replace ‘this is’ with ‘this is not’ on the first line ONLY, but it would do it for both instances of ‘this is’ and not just the first one:

    1s/this is/this is not/g

    Like most things in ‘vi’ you can put the numbers at the beginning to specify which lines or how many times or whatever so combining those two options (number at the beginning, ‘g’ at the end) you get quite a bit of power typical with a regex.

  • Leave a Reply

    Your email address will not be published. Required fields are marked *