VI Editor – Tutorial

Symptom:

 Need to edit a file in unix cli.

Problem:

 Not familiar with how to achieve this goal. You’ve been using notepad and word and some other ++ version of them, having been spoon fed of the windows establishment all your life. But *Today* you will become S00p3rL33t – just by learning VI. 

Solution:

Basic Concepts

  1. Start by typing vi followed by the file(s) you want to edit.
    Example: vi one.txt two.txt etc.txt
  2. There are two modes: command mode and insert mode. You will begin in command mode. (Unless you have a config file which changes this. Rare.)
  3. Hitting the Esc key always puts you in command mode.
    It never takes you out of command mode.
  4. Commands only work in command mode. Get used to hitting Esc a lot.
  5. Esc is your friend. If you begin to issue a command and then change your mind, always hit Esc a few times to cancel.
  6. Remember: ALL COMMANDS ARE CASE-SENSITIVE!!!

Basic Editing Commands

  1. Hitting i or a from command mode puts you in insert mode. i starts inserting text before the cursor. a starts appending text after the cursor.
  2. Use h j k l to move left, down, up, and right, respectively.
  3. Use w e b to move forward one word, forward to the next end of a word, and back one word, respectively.
  4. Use x to delete one character, dw to delete one word, or dd to delete one whole line.
  5. Use a number before a command to repeat the command that many times.
    Example: 3dw deletes three words starting from the current cursor position.
  6. u (lowercase) means undo the last change.
  7. ZZ (uppercase) saves any changes and quits.
  8. :q! abandons any changes and quits.

Cut and Paste

  1. yy yanks the current line into the buffer. This is like copying the current line to the clipboard.
    Example: 3yy yanks three lines into the buffer starting with the current one.
  2. p (lowercase) pastes the buffer contents to the line under the current one.
  3. P (uppercase) Pastes the buffer contents to the line above the current one.
  4. Lines deleted with dd also end up in the buffer and can be pasted.
  5. Words deleted with dw also end up in the buffer and can be pasted.

Advanced Cut and Paste

  1. There are 26 lettered buffers, labeled from a to z (lowercase), in addition to the usual “clipboard” one.
  2. Lettered buffers are accessed with the double quote.
    Example: “a3yy yanks three lines into the a buffer starting with the current one.
    Example: “bp pastes the contents of buffer b to the line under the current one.

Going to a Line or Column

  1. Type a number followed by G (uppercase) to go to that line number.
    Example: 24G goes to line 24.
    Example: G goes to the last line of the buffer.
  2. Type a number followed by | (pipe) to go to that column in the current line.
    Example: 40| goes to column 40.

Repeat the Last Action

  1. Use . (period) to repeat the last action.
    Example: If you hit i to insert, then type “hello world” followed by the Esc key to enter command mode, then move to another line and hit . to repeat, the words “hello world” will appear before the current cursor position.

Search for a String

  1. Use / (foward slash) followed by a string to search for that string.
    Example: /foobar would find the first occurrence of foobar after the cursor position. It would also find, for example, something like foobariciousness.
  2. After the first occurrence of the string is found, use n (lowercase) to find the next occurrence. Continue hitting n as needed.
  3. Use N (uppercase) to find the previous occurrence of the string.
  4. Use ? (question mark) followed by a string to search backwards through the file for that string.

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.