Text Editors
If you don't know how to use a text editor in GNU/Linux then you can't really get too far. Reading "README" files and "INSTALL" files will be a necessity quite early on when learning GNU/Linux on the command line.
Text editors is a topic that many books have been written about. So, how do we cover it here and do it justice... well its tricky. We can at best get a superficial glimpse. We will arbitrarily choose a couple: nano and vim. We will also look at less which is not an editor but is a command that allows you to read files on your system.
less
Lets start with
less. This is a command that opens text files for reading only. If, for example, the directory you are currently working in has a file called "README", then try this command:
less README
and you should see something like this in the terminal:
To scroll use the up and down arrows, and to quit just type q
Obviously less is the easiest way to read files but unfortunately its not always installed on GNU/Linux systems, so try less first and if it fails try on of the others below. Remember that less will only allow you to read files. To edit files you will need a text editor or word processor (Sometimes there isn't much difference between the two). nano and vim are text editors which are commonly used by programmers for working on code.
So, first check if these are installed on your system by typing each name in the terminal. I would say you will probably have vim installed, and don't have nano installed (which is a shame because it is a very simple text editor and works well for many basic tasks). If you don't have any of these installed you can try installing them by downloading the source packages, or you can try installing with the package management system that your version of GNU/Linux uses.
If you don't wish to install anything new then maybe try typing the names of some of the other text editors available, but if these are installed you will have to go online and have to research how to use them yourself. Try these in the terminal:
- vi
- gvim
- emacs
- pico
- soffice
- fte
- vfte
- gedit
- glimmer
From the above list the easiest to use if you are familiar with word processors are soffice also known as "Open Office", and gedit.
vim
So... lets have a quick look at vim, assuming it is installed when you type vim in the terminal you will see something like this:
Incidentally if you have vi installed you will see pretty much the same thing.
To open a file with vim it is best if you type the name of the file you wish to open after the vim command, so that vim opens with the file already loaded. For example if we wanted to read a "README" file in the same directory we are currently working in then just type:
vim README
This will open vim with the README file loaded as so:
Now to scroll up and down the file use the up and down arrows on your keyboard. To quit vim type:
:q
There is really a lot to vi or vim, and I don't want to get into it here, but you should really know how to open a file (as above) and then edit a file. To edit a file in vim you need to first open the file, and then type :
i
Now, I am imagining vim is quite a bit different to any text editor you have used before, so perhaps some explanation is needed. vim opens a file initially as a read only file. This means that when you first open the file with vim you are not allowed to change the file. vim has then a whole world of commands you can use to work on the file and most of the 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 vim 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 |
In addition, by pressing the "esc'"(escape) key, you will tell vim to return to the original mode (read only). You must actually press escape before you execute any of the commands in vim. For example if I wanted to open the file "README" and then alter some text, I would do the following, starting with typing:
vim README
in the terminal. This will open the "README" file as explained above. Then if I wish to edit the file, I use my arrow keys to navigate to where I want to insert or delete some text. I then press:
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 will then press the "esc" key, and finally to save the changes I press:
:w
This will write the file with the new changes. I then need to quit from vim 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 vim works before you really need to use it.
nano
nano is a really lightweight handy editor, and its a shame its not used more often. Try typing
nano in the terminal followed by the filename of the file you want to open, in this case "README", like so:
nano README
Again, the application opens in the terminal.
Good if you need to edit material on a remote server. So to scroll you use the up and down arrows, and to quit you use ctrl and x pressed simultaneously. With nano if you need to edit a file then just start typing directly into the terminal window. If you want to save the changes press ctrl and o simultaneously and the file will be saved. Also if you quit nano and changes haven't been saved then nano politely asks you if you would like to save the changes, if you do press y