Search This Blog

Friday, December 23, 2011

Basics of VI Editor of Linux


_________________________________________________________________________________

                       Opening a file     


vi filename: Creating text
Edit modes: These keys enter editing modes and type in the text
of your document.

i     Insert before current cursor position
I     Insert at beginning of current line
a     Insert (append) after current cursor position
A     Append to end of line
r     Replace 1 character
R     Replace mode
<ESC> Terminate insertion or overwrite mode
_________________________________________________________________________________

                 Deletion of text


x        Delete single character
dd      Delete current line and put in buffer
ndd    Delete n lines (n is a number) and put them in buffer
J        Attaches the next line to the end of the current line (deletes carriage return).
u        Undo last command
_________________________________________________________________________________

                 CUT AND PASTE


yy      Yank current line into buffer
nyy    Yank n lines into buffer
p        Put the contents of the buffer after the current line
P       Put the contents of the buffer before the current line
_________________________________________________________________________________

                CURSOR POSITIONING

^d              Page down
^u              Page up
:n               Position cursor at line n
:$               Position cursor at end of file
^g               Display current line number
h,j,k,l          Left,Down,Up, and Right respectively. 
                  Your arrow keys should also work if your keyboard mappings are anywhere near same.
_________________________________________________________________________________

                STRING SUBSTITUTION


:%s#string1#string2#[g]       Substitute string2 for string1 on lines n1 to n2. If g is included (meaning global), 
                                           all instances of string1 on each line are substituted. If g is not included,only
                                           the first instance per matching line is substituted.
    
    ^ - matches start of line
    . - matches any single character
    $ - matches end of line

These and other "special characters" (like the forward slash) can be "escaped" with \
i.e to match the string "/usr/STRIM100/SOFT" say "\/usr\/STRIM100\/SOFT"

Examples:

:1,$:s/dog/cat/g                   Substitute 'cat' for 'dog', every instance
                                   for the entire file -lines 1 to $ (end of file)

:23,25:/frog/bird/                 Substitute 'bird' for 'frog' on lines
                                   23 through 25. Only the first instance
                                   on each line is substituted.
_________________________________________________________________________________

              SAVING, QUITTING and other "ex" Commands


These commands are all prefixed by pressing colon (:) and then entered in the lower left corner of the window. They are called "ex" commands because they are commands of the ex text editor - the precursor line editor to the screen editor
vi. You cannot enter an "ex" command when you are in an edit mode (typing text onto the screen)
Press <ESC> to exit from an editing mode.

:w                         Write the current file.
:w new.file             Write the file to the name 'new.file'.
:w!                        Existing.file Overwrite an existing file with the file currently being edited.
:wq                       Write the file and quit.
:q                          Quit.
:q!                         Quit with no changes.
:e filename             Open the file 'filename' for editing.
:set number            Turns on line numbering
:set nonumber        Turns off line numbering

_________________________________________________________________________________

No comments:

Post a Comment

Thank you for your Feedback.