SUSE Conversations


Vi Editor Tips



By: mendesdomnic

July 21, 2009 9:59 am

Reads:910

Comments:3

Rating:0

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
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

Tags:
Categories: SUSE Linux Enterprise Desktop, Technical Solutions

Disclaimer: As with everything else at SUSE Conversations, this content is definitely not supported by SUSE (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.

3 Comments

  1. By:mfaris01

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

  2. By:tmstone835

    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.

  3. By:aburgemeister

    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.

Comment

RSS