Beginners Guide to Vi

Published: 1/12/2009
 

Beginners Guide To Vi

What Is Vi

Vi is a Non-GUI text editor that runs within a CLI (shell). It allows the user to create, view and edit basic text files like scripts and log files. It supports a number of functions like

  • Find
  • replace line
  • go to line 23

Why

Why use Vi when there are many good graphic text editors about?

The main reason is that most Linux computers are accessed vie Telnet or SSH. This means that there is no graphical user interface running on the system. It many not even be installed! When connected to a terminal shell vie telnet or SSH it is impossible to open a GUI application without high security risks.

Create A File Using Vi

The job of Vi is to edit text files. It can also create text files if it does not exist. Below (in bold) is the command to create a file called new.sh

 

Think of Vi as Notepad for Linux.

 

Gizard@LRC_GARRY /# vi new.sh

 

Vi will now open and you will be presented with a screen with nothing on it. (This is a good thing). You have now created a file using Vi. To exit Vi press Esc then : then q! then enter.

Note if no data is saved into the file, the file will automatically be removed.

Problem With files

You can open (or create) a file by using the vi command followed by the file name. The file you are trying to open must be in the current location or Vi will create an empty file in the location you are in. I.e.

 

/home/gizard/Documents/new/myfile

 

If you wished to edit the file above you simply login to the Server and use the Vi command.

 

gizzard@LRC_GARRY /home/gizard/ # vi myfile

 

The problem with the command above is that the location you are in is /home/gizard. This will add a new empty file within /home/gizard/

To solve this problem you can try two things.

A)      Go to the location ( cd /home/gizard/new/myfile; vi myfile )

B)      Set the full path name to Vi ( vi /home/gizard/new/myfile )

Editing A File

To edit a file simply open it using the steps above. The file should now be open and displaying its content on the screen. Press the Insert key (next to Home). You will see at the bottom of Vi that the word INSERT is visible. You can now edit your document.

 

 

Saving , Saving & Exiting

To save a file press: ‘Esc’ then ‘:’ then ‘w’ then ‘enter

To save and exit press: ‘Esc’ then ‘:’ then ‘wq’ then ‘enter

To exit without saving, press: ‘Esc’ then ‘:’ then ‘q!’ then ‘enter

To force a save press: ‘Esc’ then ‘:’ then ‘w!’ then ‘enter

 

 

Related Posts

Linux File Structure Part 2

Last Updated: 1/12/2009 When you wish to use the CDROM drive it must first have a cdrom in the drive and data on the cdrom. To view the data you must mount the device to a given place of the Linux filing tree. Everything in Linux is a file. The whole partition of a hard dr
Read more...

 

Linux File Structure

Last Updated: 1/12/2009 Understanding the basic idea behind the Linux file structure can help but only if other people follow this idea!
Read more...

 

Pipe and Redirection

Last Updated: 1/12/2009 The command line is not only a tool for bashing out program names but an elegant and intelligent environment. Some programming languages can be typed directly into the command line. Also tasks that take some time to execute in the background leaving
Read more...

 

Mounting a Drive

Last Updated: 1/12/2009 In Linux, like in UNIX, diskettes and CD-ROMs need to be mounted before they can be used. What mount does is to associate the devices (floppy or CD-ROM drives) to a mount point (a directory) in the root file system. The usual mount point for diskette
Read more...

 

Linux in a nut Shell

Last Updated: 1/12/2009 Why should we bother learning Linux shell command when we have GUI like KDE?
Read more...

 

Bash Jobs

Last Updated: 1/12/2009 Understanding bash jobs and how to create and remove them from the CLI
Read more...

 

Training Camps

Last Updated: 1/12/2009 Linux Training Camps
Read more...

 

Linux Cheat Sheet

Last Updated: 1/12/2009 A small Linux cheat sheet, introducing the very basic and very essential commands for surviving at the Linux CLI.
Read more...