Using vi, the Unix Visual Editor

vi is a screen-oriented text editor originally created for the Unix operating system. This article will give you some knowledge on how to use vi, the full screen editor found on nearly all Unix systems.

1. vi Mode

vi has 2 mode :

  • Command Mode
    • Command mode is the default mode (vi starting point).
    • Command mode is the mode in which commands are given to move around in the file, to make changes, or to leave the file
    • Commands are case sensitive: j not the same as J
    • Most commands do not appear on the screen as you type them. Some commands will appear on the last line: : / ?
  • Insert (or Text ) Mode
    • The mode in which text is created. (You must press <Return> at the end of each line unless you’ve set wrap margin.)
    • There is more than one way to get into insert mode but only one way to leave: return to command mode by pressing <Esc>

2. Using vi

a. Open file using vi. type : vi filename.txt

  • If filename.txt does not exist, a screen will appear with just a cursor at the top followed by tildes (~) in the first column.
  • If filename.txt does exist, the first few line of the file will appear.
  • The status line at the bottom of your screen shows error messages and provides information and feedback, including the name of the file.

b. Closing vi

  • You must be in command mode. just press Esc if you are still on the Insert Mode
  • You must press <Return> after commands that begin with a : (colon)
Command Function
: x Save and quit from vi editor
:q Exit from vi. however, you have to save (:w) any change you’ve made before you can quit
:wq Save your work, then quit
:q! Quit without saving any change you have made

 

3. Entering, Deleting, and Changing Text

You must be in Insert (text) Mode

List of command :

Command Function
i Insert text before cursor
I Insert text before cursor and put cursor on first line
a Insert text after cursor
A Insert text after cursor and put cursor at the end of line

 

Changing Text

Command Function
r Change one character before cursor
R Change one character starting from cursor

 

Deleting Text

Command Function
x Delete one character after cursor
Nx Delete (N) character starting from character after cursor
dw Delete one word, starting from character after cursor
dNw Delete (N) word starting from character after cursor. (d6w deletes six word)
dd Delete a line
dNd atau Ndd Delete (N) line starting from cursor place. (6dd deletes six lines)

 

4. Other Command

Here’s a list of most common command that can help you editing text file.

  • Searching for Text
Command Function
/text Search forward (down) for text (text can include spaces and characters with special meanings.)
?text Search backward (up) for text
n Repeat last search in the same direction
N Repeat last search in the opposite direction
fchar Search forward for a charcter on current line
Fchar Search backward for a character on current line
; Repeat last character search in the same direction
% Find matching ( ), { }, or [ ]

Counting Line

Command Function
:. Checking line number our cursor in
:= Checking how many lines are in text file
^g (Ctrl +g) Showing description about text file we’re opening
:set number Showing Line Number on left side of vi editor
:N Put cursir on line (N)

 

More complete information about vi editor :