FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Terminal

Terminal: UsingVI

Text Editors

nano

The nano editor (Nano's ANOther editor) is installed on all XOs. It is a relatively simple editor, which has the advantage for many users that it is easier to learn than more powerful editors such as vi (below). When nano is started from the command line, the following text window appears:

   GNU nano 2.2.6                New Buffer

 

 

 

 

 

^G Get Help  ^O WriteOut  ^R Read File ^Y Prev Page ^K Cut Text  ^C Cur Pos
^X Exit      ^J Justify   ^W Where Is  ^V Next Page ^U UnCut Text^T To Spell

Some of the most frequently-used nano commands are shown at the bottom of the window, where the caret character ^ means to hold down the control key and the indicated letter key at the same time in order to perform the function shown. Control-g brings up a help file that contains the rest of the nano commands. The nano command has many options, such as naming a file to open, and positioning the cursor at a specified row and column position when opening the file. These options can be viewed with the command

nano -h

vi

The vi (visual editor) program can also run in the Terminal Activity. Typing vi at the command line brings up the following view.


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

There are two modes in vi, command mode and insert mode. You use command mode for file management, searching, editing commands such as cut, copy, and paste, or for exiting vi, and insert mode for entering text. In command mode, the i command switches to insert mode, while in insert mode the escape key switches to command mode.

Simple Commands

Many commands 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.


There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.