Terminal Activity

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 systems such as Linux.  Let's have a quick look at vi: type vi in the terminal and you 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 you wanted to read the text file called "MyExample.txt" in the same directory you 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 opens vi with the file loaded as so:

example

Simple Commands

Now, vi may be quite a bit different than any text editor you have used before, so perhaps some explanation is needed. First, since vi works on the command line there are no menus to click to make things happen. Instead you must use the keyboard to type commands that vi understands. 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.

Let's look at some examples. First open a file as you did in the above example:

vi MyExample.txt

This opens 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 puts you in the insertion mode and now anything you type appears in the document itself. When you have finished making the changes you may wish to save the file. You would then press the esc key followed by:

:w

This writes the file with the new changes. You then need to quit from vi so you press the esc key followed by:

:q

Now replace 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.

If you want to get out of vi without saving your changes, use:

:q!

If the commands you are typing are going into the document instead of having an effect, remember to press esc first!

If you make a mistake when editing a document, press esc and then u for undo.