Table of Contents

Vi Text Editor

 

Vi Text Editor Modes

The vi text editor has three modes namely
1. Writing mode:
2. command mode:
3. command line mode:

When vi editor is opened, it has a command mode. In order to go to writing
mode, one can press a or i for entering text. <ESE> key is used to go back to vi
command mode. In order to enter from vi command mode into the vi command
line mode, you can type :. <ESC> key will bring you back to normal command
mode. Some basic commands of vi editor are listed below.

 

How to start vi editor:

  • vi opens vi editor environment.
  • vi myfile creates or opens the file named myfile starting at line 1.
  • vi -r myfile recovers myfile that was being edited when the system
    crashed.
  •  <Esc> changes the writing environment of vi into command mode.

 

How to quit vi editor:

  • :w! <return> (enter key) saves the changes.
  •  :q! <return > (enterkey) quits the file without saving changes
    to exit from the editor.
  • :wq! <return> first saves the changes and then quits the file to
    exit from the editor.
  • :x <return> also quits vi editor saving the changes.
  • :ZZ also exits from vi environment saving the changes.
  • :w! myfile <return> If the vi editor is opened but it has been
    not given any file name then one use this command to save the file as
    my file.

 

Moving the Cursor in vi editor:

  • w moves the cursor at the beginning of the next word.
  • b moves the cursor back at the beginning of the previous word.
  •  h or -> moves the cursor forward.
  • l or <- moves the cursor backward.
  • j or <return (enter) > or downward arrow moves the cursor downward.
  • k or upward arrow moves the cursor upward.
  • 0 moves the cursor at the starting of the current line
  • $ moves the cursor at the end of the current line.
  • :0<return> or 1G moves the cursor at the start of the first line.
  • :n<return> or nG moves the cursor at the start of the nth line.
  • :$<return> or G moves the cursor at the start of the last line.
  • :f<return> or <CTRL>G displays the current file and the number of current line at the end of the screen

 

Inserting text in vi editor:

  •  i mean insert before the cursor. After typing i, you can insert text
    before the cursor, until <Esc> hit.
  • I mean insert at the beginning of line.
  • a means append after the cursor. After typing a, you can insert text after the cursor, until      <Esc> hit.
  •  A means append at the end of the line.
  •  o means open a line below. This command opens and puts the text in the new line below the current line, until <Esc> hit.
  •  O means line above. This command opens and puts the text in a new line
    below the current line, until <Esc> hit.
  •  s means substitute a character.
  •  S means substitute entire line.

 

Changing text in vi editor:

  • r: After typing r, you can replace a single character under the cur-
    sor.no need to hit <Esc>.
  • R: After typing R, you can replace characters starting with the current letter under the cursor until you hit <Esc>.
  • cc: After typing cc, change (replace) the current entire line, stops
    when you hit <Esc>.
  • C: After typing C, change (replace) the characters in the current line,
    until you hit <Esc>.
  •  u: u is used to undo your last action on the vi editor.
  • <CTRL>r: <CTRL>r is used to redo the changes which were undone.

 

 

Deleting text in vi editor:

 

  •  x deletes a single character under the cursor. No need to hit <Esc>.
  •  dw deletes a word, no need to hit <Esc>.
  •  dd deletes entire line, no need to hit <Esc>.
  •  ndd deletes n lines, no need to hit <Esc>.
  •  D deletes the remainder of the line starting with the cursor.

 

Copying and pasting the text:

 

  •  yy copies the current line.
  • nyy copies n lines including the current line.
  • p puts and paste the copied line after the line where the cursor is.

 

 

Moving and manipulating the screen of vi editor:

 

  •  <CTRL>f scrolls the screen on times forward(downward).
  • <CTRL>b scrolls the screen on times backward(upward).
  • <CTRL>d scrolls the screen on times downward(forward).
  • <CTRL>u scrolls the screen on times upward(backward).

 

 

Searching and replacing some text or string in vi editor:

 

  • :/nextWord<return> search forward the occurrence of the next nextWord
    and puts the cursor at the starting of the next word.
  • :?nextWord<return> search backward the occurrence of the next
    nextWord and puts the cursor at the starting of the previous word.
  • n moves the cursor to the next occurrence of search string in the same
    direction of the search.
  • N moves the cursor to the next occurrence of the search string in the opposite
    direction of the search.
  •  :s/word1/word2/g<return> replaces all occurrences of “word1” with
    word2” on current line.
  •  :%s/word1/word2/g<return> replaces all occurrences of “word1
    with “word2” in whole data.
  • :1,$s/word1/word2/g can also be used to make a global substitution
    of word1 by word2.
  •  :%s/love//g<return> removes the word “love” from the whole data.
  • :%s/”//g<return> removes the sign from the whole data.
  • :/^ The searches for the next line that starts with “The“, for example,
    The“, “Then“, “There“.
  • /^ The n > searches for the next line that starts with the word “The“.
  • /END$ searches the next line that ends with the word “END“.

 

List of some other important commands in vi editor:

 

  •  : is used to invoke an ex command.
  •  :set nu <return> will number all the lines of the text.
  • :set nonu <return> will turn off the line number of the text.
  •     brings the old ex commands at the executive position.
  • J is used to join two lines.
  •  / is used to search forward for a pattern.
  •  ? is used to search backward for a pattern.
  •  :.= <return> returns the line number of current line at the bottom of the screen.
  •  :=<return> returns the total number of lines at the bottom of the screen.