A Geeks Guide to VI

 

If you classify yourself a geek, you will probably have played around with the vi text editor at some point.  A lot of *nix user’s avoid using Vi, because their initial experience with it is not good.  It has a steep learning curve, and takes a lifetime to master.  I hope that you will learn some of the basic Vi commands, and begin appreciating it for its power.

What is Vi?

Vi is a screen based editor that is found on any *nix platform.  Vi can seem daunting at times as it has 150 basic commands that can be used at any given time.  Now I won’t be discussing all 150 commands in this post, but I will be talking about the most basic command that are vital to using Vi.

Starting Vi
  • Open up your shell prompt and type in vi
  • To open up one or multiple files type in vi file1.txt file2.txt
  • If the files do not exist they will be created
Modes

There are two modes in Vi:

  1. Insert
  2. Command

When you begin Vi you will start in command mode.  As you would expect; insert mode allows you to enter actual text in the document, and command mode allows you to issue commands.

Insert Mode
  • allows you to enter text
  • keyboard characters all function as normal
  • hitting enter creates a new line
Command Mode
  • You always enter command mode by hitting the ESC key
  • If you make an error in your command, hit ESC
  • Commands are case sensitive
  • Command mode does not let you insert any text to the document
Basic Commands
  • i – inserts text before the cursor
  • a – starts appending text after the cursor
  • h j k l – move left, down, up, and right
  • w e b – move forward one word, forward to the next end of a word, and back one word
  • x – delete one character
  • dw – delete one word
  • dd – delete one whole line
  • u – undo last change
  • wq – saves changes and quits
  • :q! – doesn’t save changes and quits
  • yy – copies current line
  • p – pastes under the current one
  • P – pastes above the current one
Useful Tricks
  • G by itself – goes to the last line in the file
  • Number followed by G – goes to that line number (24G)
  • ~ – toggles the case of the character under the cursor
  • using a number before a command; repeats that command (3dw – delete 3 words)
  • . – repeats the last action
  • / followed by keyword – searches the document for the keyword

Feel free to practise using the above commands in Vi, and it will come in handy the next time you need to configure a web server!

Note: Found an online Vi editor for those of you that don’t have access to any shell.

Related posts:

  1. AJAX / JSON Guide
  2. Choosing an Open Source CMS: Beginner's Guide
  3. MVC: A Beginner’s Guide
  4. Django Python Framework


Written by Brenley Dueck

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
connect with me!