Cambridge IT Courses logo
Birmingham

Cambridge IT Courses: Specialists in advanced IT training

Cambridge IT Courses Blog: 5 commands you need to know to use VI

VI: THE command line editor

The VI editor has powerful features to aid programmers and as it runs on the command line it makes it particularly useful if you need to edit files on a server. However, many beginning users avoid using VI because of the steep learning curve. That's why we'll show you 5 commands you need to know to use VI:

You open a file with:

vi FILENAME

If the file doesnt exist, it will be created!

VI Modes

In Vi you have to switch between three basic modes:
  • Command Mode
  • Insert Mode
  • Line Mode

Switching modes:

The Command mode is the normal mode you are in when you enter the VI.
When you are in Insert mode you can enter Command mode by pressing "Esc".

Insert mode is the mode you use when you want top write or change text.
You can enter Insert mode by pressing "Esc" for Command mode and then lowercase "a" followed by "Enter".
Now you can write or change code.

The Line mode is used for Line oriented commands, such as searching for strings in your code.

5 useful VI commands:

Saving files:

Go into Command mode (press "Esc" if in doubt), then type:
:w
Followed by "Enter".

Closing VI:

:q

To save and exit use:
:wq

VI Viewing options:

With
:vsplit and :hsplit 
you can split the VI horizontal or vertical. With that function you can easily compare code or work on you HTML and CSS sheet at the same time

For line numbers you can use
:setnu
to show them, or
:setnonu
to hide them!

Copy and Delete:

When you want to paste or copy code you can use
:y for 1 line or :y3 for 3lines

When you want to delete one or more lines use:
:d or :d3 for 3lines


Once you've mastered the basics get yourself a good 'cheat sheet' such as: http://bullium.com/support/vim.html And when you've used VI for a while writing code with shortcut commands will be much more efficient than a GUI/ mouse based text editor!
Posted by Deniz Gruenberger at 22:15 2011-05-21 | Trackbacks (0) | Comments (0)
Cambridge IT Courses Blog

Articles, tips and tricks discussing technical issues related to our courses

Blog Posts
Categories
Authors
Feeds