Applying Thoughts

"Sometimes I Win, Other times I learn. but I never lose."

February 23, 2011


vi

vi (pronounced vee-eye) is UNIX's standard editor. The purpose of the following exercises is to expose you to some of vi's features. vi provides a window of 20 lines into the file being edited. You can move to any line and make changes (insert, delete, over-write).
Unix was designed to support simple ASCII terminals, thus the original editors (ed) were line based. vi is a command line editor, in that it has two basic modes of operation, command mode and edit mode.
The default mode for vi is the command mode. In this mode, editing of user text cannot be performed. vi awaits the appropriate command from the user before performing the operation.
In general, vi will return to the command mode after executing the command.
The advantage of this approach is that commands can be executed from files, and documents can be formatted or re-arranged by simply running a menu script of commands in vi.
Pressing the ESC key will cause the terminal to beep. When this happens, you are in command mode.
Loading vi
To load vi and begin an editing session, type,
        vi   newfile
This command loads the editor and because the file does not already exist, creates it ready for use. The editor is now in command mode and is awaiting a command.
Moving the cursor
The following keys control cursor movement. DO NOT TRY TO USE THE CURSOR ARROW KEYS TO MOVE THE CURSOR!
        k       up
        j       down
        h       left
        l       right
When vi is loaded on a new file, the cursor is restricted to the upper left position on the screen, and cannot be moved using the cursor keys.
Text input mode (append, insert, open)
The commands
        a       appends to the last line of the file
        i       inserts at the current cursor position
        o       inserts one line down from the current cursor position
        O       inserts one line up from the current cursor position
allow text to be entered starting at the upper left corner of the screen, for a new file. When used on an existing file, the commands act like,
Deleting and Changing text
The three most used commands to alter text are,
        x       erase the character at the cursor
        r       replace the character at the cursor
        dd      delete the entire line where the cursor is
All three commands are executed in command mode, and return to the command mode after executing.
Undoing changes
There are times that you make changes and immediately realize a mistake has been made. The vi editor provides means to undo previous commands.
The u command undoes the previous command.
The U command undoes all the changes made on the current line.
Saving the editor buffer and remaining in vi
You are strongly encouraged to save editing changes regularly. The command to write the editor buffer is
        :w
If you decide that you do want to over-write the existing file, but rather save the changes to a new file, then follow the :w command with the name of the new file,
        :w  newfile2
Leaving vi
The commands used to leave vi are,
        ZZ      writes the buffer to disk into the original file, then returns to the shell
       
        :wq     same as ZZ
       
      :q!   quits the editor, abandons the buffer, and returns to the shell

No comments: