Thursday, May 31, 2012

Introduction to Vi and Vim

Vi and Vim are modal editors. This is very different than most you are probably familiar with. The Vi and Vim editors have two basic modes of operation:

- Command mode
- Editing mode

Pressing Esc on the keyboard activates command mode. When in command mode, command keys can be used to activate specific editing functions. The following table lists the most common commands for vi and vim.

:w

Save the file

:x

Save and exit

:q
Quit

a
Append text before the current cursor position

A
Append text after the current cursor position

r
Replace text before the current cursor position

R
Replace text after the current cursor position

i
Insert text before the current cursor position

I
Insert text after the current cursor position

yy
Copy current line

p
Paste copied text

/[TEXT]
Search for the specified text

This is only a brief list of the most common Vi/Vim commands. There are a ton of other neat things you can do in command mode. For this tutorial, we will just focus on the basics so you can get up and running quickly.

Now, lets open up the editor and get started. Follow the steps listed below to practice using the Vi or Vim editors.

Note: The vim command is used in the examples below. It works on most BSD and Linux systems. Unix systems use the vi command in place of vim.

Exercise 1 - Getting Started
Step 1: Type vim testfile on the command line and press Enter.
Step 2: Press the i key to enable insert mode.
Step 3: Type Hello world!
Step 4: Press the ESC key on your keyboard.
Step 5: Type :wq and press the <Enter> key.

Congratulations, you have just created your first file in Vim. Type cat testfile on the command line to check out the file's contents. Now work through the remaining exercises to get more familiar with using vim to edit files.

Exercise 2 - Copy and Paste
Step 1: Open the file created in the first exercise by typing vim testfile on the command line.
Step 2: Press yy on the keyboard. This copies the current line into the buffer.
Step 3: Press the p key three times. You should now have several copies of the first line. You could also type 3p to achieve the same result.
Step 4: Press dd. This should delete one line.
Step 5: Type :wq and press <Enter> to save the file and exit the editor.

Exercise 3 - Append text to a file

Step 1: Open the previously created testfile by typing vim testfile.
Step 2: Move your cursor to the end of the last line in the file using the arrow keys.
Step 3: Press the a key and then press <Enter>.
Step 4: Type Testing 123
Step 5: Press the <ESC> key and type :wq and then press <Enter> to save the file and exit vim.

Exercise 4 - Replace text in a file

Step 1: Open the previously created testfile by typing vim testfile.
Step 2: Press <Shift + G> to go to the last line in the file.
Step 3: Press  <Shift + R> and type "I like turtles."
Step 4: Press the <ESC> key and type :wq and then press <Enter> to save the file.

Exercise 5 - Navigating and searching

Step 1: Open the /etc/services file in read only mode by typing view /etc/services.
Step 2: Type /dns and press <Enter>. This will search the file and show you the first line that contains the word dns.
Step 3: Press the the n key to show the next matching result.
Step 4: Press 1 and then <Shift + G> to return to the first line of the file.
Step 5: Now press <Shift + G> to move to the last line of the file.
Step 6: Type :q! and press <Enter> to quit without saving any changes to the file.

This post is intended for readers of my new book Unix, Linux, and BSD Command Line Cross Reference who want to get started using vi or vim. This tutorial barely scratches the surface of what you can do with the vi and vim editors. For more practive check out this cool interactive vim tutorial online at http://www.openvim.com/tutorial.html.

1 comment:

  1. Thanks for blogging about useful commands.
    Ls command basically displays the content of directory in sorted alphabetically.
    If you have millions of files/directories, ls command hangs because of sorting.
    Ls –U ouput files without sorting and more useful
    Please see more about Ls command examples in Linux/Unix

    ReplyDelete