Vi editor: basic commands

Created on 2011-02-11 by Sona Gabrielyan

Switzernet

 

 

Introduction. 1

General Rules. 1

Keyboard keys. 1

Cursor movements. 1

Text input commands. 1

Training session. 2

Validation. 4

References. 4

 

 

 

Introduction

The UNIX text editor is called vi. It is available on every UNIX machine. Knowing some very basic commands of this editor will save your time for example if you will need to make a modification in a file in our PortaSIP or Asterisk servers, and there is no other editor installed (which is often the case).

 

 

General Rules

 

The main rule to keep always in mind: when finish typing a text in vi, one must press Escape key.

 

Keyboard keys

In some cases, when you use arrow, PgDn, PgUp, Home or End keys, it will cause some characters to be inserted into your text.

 

  Do not use arrow keys to move the cursor

  Do not use PgDn or PgUp keys

  Do not use Home or End keys

 

If you accidentally use one of mentioned keys, and some strange characters are inserted into the file, the best to do is to press Escape followed with :q! (quit without saving command). Then open the file again and continue to edit.

 

Cursor movements

Here are characters you need to press to move the cursor while you are editing a text with vi:

 h - left one character

 l - right one character

 j - down one line

 k - up one line

 $ - to the end of line

 0 - to the beginning of the line

 Ctrl-F - forward one page

 Ctrl-B - back one page

 

Text input commands

 

Entering a text is a combination of three actions:

- start input command  (for example, i for insert a text)

- type the text (here you type the text to be inserted)

- termination of input command (press the Escape key)

 

Text input commands are the following (!all require "Esc" to terminate!)

  i - insert text before the character cursor is on

  I - insert text at the beginning of the line

  a - append (insert text after the character cursor is on)

  A - append text to the end of the line

  c - change (replace previous text with new one)

  o - start entering text at the beginning of the new line below the cursor

  O - start entering text at the beginning of the new line above the cursor

 

 

Deleting:

  d   - delete a character

  dd - delete the whole line

  x - delete character cursor is on

 

 

Exiting:

  ZZ - save and exit (hold down shift and press "z" twice)

  :q! - quit without saving if changes were made

 

Other:

  u - undo last editing command

  p - paste below cursor line (deleted or copied text)

  P - paste above cursor line

  /sometext - search for "sometext"

 

 

 

Training session

Open cygwin.

Open a MS Word document.

Type “date” to show the date and time.

Create a folder where you will keep your files during the current training session.

Change current directory to that folder:

 

 

Make a prinscreen of Cygwin (Alt+PrintScreen) and past it into the MS Word document. Save.

 

Type vi and yymmdd-lname-vi.txt to begin with editing session. Here yymmdd is the current date, lname is your lastname.

 

For example:

 

$ vi 110211-gabrielyans-vi.txt

 

 

Make a prinscreen of Cygwin (Alt+PrintScreen) and past it into the MS Word document. Save.

 

To begin inserting text, press i. Type the current date, your name. Press ESC to signal vi that you stopped entering text. To save and exit, type ZZ - save and exit (hold down shift and press "z" twice).

 

Open again the same file.

 

$ vi 110211-gabrielyans-vi.txt

 

 

Make a prinscreen of Cygwin (Alt+PrintScreen) and past it into the MS Word document. Save.

Change the date format: write the date in format yyyy-mm-dd.

Under “My name is…” add a new line “I follow a training session on vi.”.

Surround your name with square brackets. Save and exit.

 

If you make error and you obtain strange results, exit without saving (Esc->:q!), and then open the file again.

 

Open the file again:

 

Make a prinscreen of Cygwin (Alt+PrintScreen) and past it into the MS Word document. Save.

 

Your MS Word document must contain now 4 prinscreens from cygwin.

 

Validation

Name the MS Word document with prinscreens in format yymmdd-lname-vi.doc, where yymmdd is the current date and lname is your lastname. Upload it on the training session web site, according to the guidelines.

 

 

References

This document: [ch1] [ch2]

Vi editor: search, replace commands and exercises. Regex: [ch1] [ch2]

Vi: first steps: http://www.infobound.com/vi.html

Unix editor vi: http://www.temple.edu/cs/unix/E310.pdf

Training sessions: http://www.unappel.ch/2/support/100722-training-employees/i/

 

*   *   *