• English
  • Japanese

vi

Easy manual for Vi

About Vi

What is Vi

  • It is the simplest text editor in UNIX.*1
  • Once you get used to the concept of "mode (command mode and insert mode)", editing file on UNIX will be very easy.

How to run

  • To run Vi, just type "vi filename" where "filename" is file you want to edit with Vi. If the filename does not exist, a new file with the filename will be created.

How to close

  • Enter "q![Enter key]" while Vi is activated.
  • To close at the same time of saving, enter "wq![Enter key]" or "ZZ".

How to save

  • Enter "w[Enter key]" while Vi is activated.

How to move the cursor

k

h ←  → l

j

Mode

  • There are command mode and insert mode for Vi. When you run Vi, the command mode is on.
    • Command Mode: Mode to edit the character strings
    • Insert Mode: Mode to enter the character
    • ex mode: ":" will appear on left bottom of the screen. File manipulation or slimer will be done with this mode.
  • Use [ESC key] to switch mode.
  • If you are not sure which mode you are in, press [ESC key], you will go back to the command mode.

Key assign from command mode

Command Key assign
Switch to the insert mode i
Delete one word x
Delete one line dd
Replace one word cw
Show number of lines :set number
Show number of lines :100 (Move to line 100)

Example

(1) Create a new html file

  1. While you are logged in, move to the public directory.
    % cd public_html
    
    1. public_html: If there is no directory, create one.
      % cd [Enter key]
      % mkdir public_html [Enter key]
      % cd public_html [Enter key]
      
  2. Specify the filename, and run Vi.
    % vi index.html [Enter key]
    
    The screen like below will appear.
    ~
    ~
    ~
    ~
    ~
    "index.html"  [New File]
    
  3. Enter "i" to switch to the insert mode, and edit.
    <html>
    <head><title>Hello!</title></html>
    <body>
    Hello!
    </body>
    </html>
    ~
    ~
    "index.html"  [New File]
    
  4. After you enter </html>, press [Esc key] and enter "ZZ".
  5. Enter "http://web.sfc.keio.ac.jp/~login name/index.html" on a browser, and see if "Hello!" appears.

(2) Edit the administrative file for mailing list

  1. Login to the mail server, and open the administrative file in Vi.
    % vi /var/mail-list/Specified directory/Administrative file name
    
    The screen like below will appear.
    ~
    ~
    ~
    ~
    ~
    ~
    ""Administrative file name"" 0 lines, 0 characters
    
  2. Enter "i" to switch to the insert mode, and write one email address on each line.
    z00000tk@sfc.keio.ac.jp
    z00001hk@sfc.keio.ac.jp
    ~
    ~
    ~
    ~
    ~
    ""Administrative file name"" 0 lines, 0 characters
    
  3. When you finish entering all email addresses, press[Esc key] and enter "ZZ".

*1 It is a single program, that it has more chance to use compared to Emacs (of which the format is very complicated) at the rescue mode in case of trouble on Linux.

Last-Modified: December 20, 2011

The content ends at this position.