vi

Sugar has a built in text editor that you can use with the Terminal Activity. This editor is called vi and is used in many other types of operating system such as Linux.  Lets have a quick look at vi : type vi in the terminal and you will see something like this:


vi 

This is vim running inside the terminal. You can use vim as a text editor so that you don't need to open any other Activities to read or write most documents.

Open a Text File

To open a file with vi it is best if you type the name of the file you wish to open after the vi command, so that vim opens with the file already loaded. For example if we wanted to read the text file called "MyExample.txt" in the same directory we are currently working in then just type:

vi MyExample.txt

Note : If you the type the above and the file 'MyExample.txt' does not exist then vi opens a new (blank) document.

If we assume there is a file called 'MyExample.txt' then the above command will open vi with the file loaded as so:

example

Simple Commands

Now, I am imagining vi is quite a bit different to any text editor you have used before, so perhaps some explanation is needed. First, since vi works onthe command line there are not menus then you click on to make things happen. Instead you must use the keyboard to type commands that vi will understand. There are many commands you can use to work on the file and most of them are executed by just typing a single letter, or they are in the format:

: command

Where 'command' is the name of the command you wish to use. The commands are all designated by shortcuts. An 'i' , for example, is short for 'insert'. The following is a table of vi commands you should know:

command action
i (only used in read-only mode) insert text
:w (only used in read-only mode) write changes to file
u (only used in read-only mode) undo changes
:q (only used in read-only mode) quit vim

vi always opens a file initially in read-only mode. This means that when you first open the file you cannot change the file.  It is in this mode that you type the commands. At anytime you can press the 'esc' (escape) key to return to read only mode.

Lets look at some examples. First we open a file as we did in the above example :

vi MyExample.txt

This will open the "MyExample.txt" file as explained above, or creates a new (blank) file if it did not already exist.

You can scroll up and down the file using the up and down arrows on your keyboard. 

To insert new content or change the existing content of the file in vi you need to type :

i

This will put me in the insertion mode and now anything I type will appear in the document itself. When I have finished making the changes I may wish to save the file. I would then press the "esc" key followed by :

:w

This will write the file with the new changes. I then need to quit from vi so I press the escape key followed by :

:q

Now find a file and experiment. If you haven't used something like vim before then it might take some getting used to, so spend some time working out for yourself how vi works before you really need to use it.