Article

Vi Editor Tips

article
Reads:

2643

Score:
5
5
1
 
Comments:

3

Vi Editor Tips

To open multiple files on a window

  1. Horizontal split

    vi -o file-one file-two
    	
  2. 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

  3. 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 occurence of string

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

Replace All occurence of string

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




User Comments

mfaris01's picture

vi Rulez.

Submitted by mfaris01 on 21 July 2009 - 4:03pm.

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

A couple of notes

Submitted by tmstone835 on 4 August 2009 - 9:26am.

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.

One more clarification....

Submitted by aburgemeister on 14 August 2009 - 10:40am.

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.

© 2009 Novell, Inc. All Rights Reserved.