Article

mendesdomnic's picture
article
Reads:

8670

Score:
4.5
4.5
2
 
Comments:

3

Vi Editor Tips

(View Disclaimer)

Vi Editor Tips

To open multiple files on a window

  1. Horizontal split
  2. vi -o file-one file-two
    	

  3. Vertical split
  4. 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

  5. Split Open one file at a time
  6. 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

Disclaimer: As with everything else at Cool Solutions, this content is definitely not supported by Novell (so don't even think of calling Support if you try something and it blows up).

It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.




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...

tmstone835's picture

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.

aburgemeister's picture

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.

© 2012 Novell