1. Text Editors in Linux
1.1. Vim
Vim (Linux Vi and Vim Editor 2017) is the standard built-in editor of Linux. It is an improved version of the original default Vi editor of Unix. Unlike most other editors, vim has 3 different operating modes, which are
. Command mode: for entering commands
. Insert mode: for entering and editing text
. Last-line mode: for saving files and exit
When vim starts, it is in the default Command mode, in which most keys denote special commands. Examples of command keys for moving the cursor are
When using vim inside X-window, cursor movements can also be done by arrow keys. To enter text for editing, the user must switch vim into Insert mode by entering either the i (insert) or a (append) command:
i: switch to Insert mode to insert text;
a: switch to Insert mode to append text
To exit Insert mode, press the ESC key one or more times. While in Command mode, enter the : key to enter the Last-line mode, which is for saving texts as files or exit vim:
:w write (save) file
:q exit vim
:wq save and exit
:q! force exit without saving changes
Although many Unix users are used to the different operating modes of vim, others may find it somewhat unnatural and inconvenient to use as compared with other Graphic User Interface (GUI) based editors. The following editors belong to what is commonly known as What You See Is What You Get (WYSIWYG) type of editors. In a WYSIWYG editor, a user may enter text, move the cursor by arrow keys, just like in regular typing. Commands are usually formed by entering a special meta key, together with, or followed by, a letter key sequence. For example,
Control-C: abort or exit,
Control-K: delete line into a buffer,
Control-Y: yank or paste from buffer contents
Control-S: save edited text, etc.
Since there is no mode switching necessary, most users, especially beginners, prefer WYSUWYG type editors over vim.
1.2. Gedit
Gedit is the default text editor of the GNOME desktop environment. It is the default editor of Ubuntu, as well as other Linux that uses the GNOME GUI user interface. It includes tools for editing both source code and structured text such as markup languages.
1.3. Emacs
Emacs (GNU Emacs 2015) is a powerful text editor, which runs on many different platforms. The most popular version of Emacs is GNU Emacs, which is available in most Linux distributions.
All the above editors support direct inputs and editing of text in full screen mode. They also support search by keywords and replace strings by new text. To use these editors, the user only needs to learn a few basics, such as how to start up the editor, input text for editing, save edited texts as files and then exit the editor.
Depending on the Unix/Linux distribution, some of the editors may not be installed by default. For example, Ubuntu Linux usually comes with gedit, nano and vim, but not emacs. One nice feature of Ubuntu is that, when a user tries to run a command that is not installed, it will remind the user to install it. As an example, if a user enters
emacs filename
Ubuntu will display a message saying “The program emacs is currently not installed. You can install it by typing apt-get install emacs”. Similarly, the user may install other missing software packages by the apt-get command.
2. Use Text Editors
All text editors are designed to perform the same task, which is to allow users to input texts, edit them and save the edited texts as files. As noted above, there are many different text editors. Which text editor to use is a matter of personal preference. Most Linux users seem to prefer either gedit or emacs due to their GUI interface and ease to use. Between the two, we strongly recommend emacs. The following shows some simple examples of using emacs to create text files.
2.1. Use Emacs
First, from a pseudo terminal of X-windows, enter the command line
emacs [FILENAME] # [ ] means optonal
to invoke the emacs editor with an optional file name, e.g. t.c. This will start up emacs in a separate window as show in Fig. 2.1. On top of the emacs window is a menu bar, each of which can be opened to show additional commands, which the user can invoke by clicking on the menu icons. To begin with, we shall not use the menu bar and focus on the simple task of creating a C program source file. When emacs starts, if the file t.c already exists, it will open the file and load its contents into a buffer for editing. Otherwise, it will show an empty buffer, ready for user inputs. Fig. 2.1 shows the user input lines. Emacs recognizes any .c file as source file for C programs. It will indent the lines in accordance with the C code line conventions, e.g. it will match each left { with a right }with proper indentations automatically. In fact, it can even detect incomplete C statements and show improper indentations to alert the user of possible syntax errors in the C source lines.
After creating a source file, enter the meta key sequence Control-x-c to save the file and exit. If the buffer contains modified and unsaved text, it will prompt the user to save file, as shown on the bottom line of Fig. 2.2. Entering y will save the file and exit emacs. Alternatively, the user may also click the Save icon on the menu bar to save and exit.
2.2. Emacs Menus
At the top of the emacs window is a menu bar, which includes the icons
File Edit Options Buffers Tools C Help
The File menu supports the operations of open file, insert file and save files. It also supports printing the editing buffer, open new windows and new frames.
The Edit menu supports search and replace operations.
The Options menu supports functions to configure emacs operations.
The Buffers menu supports selection and display of buffers.
The Tools menu supports compilation of source code, execution of binary executables and debugging.
The C menu supports customized editing for C source code.
The Help menu provides support for emacs usage, such as a simple emacs tutorial.
As usual, clicking on a menu item will display a table of submenus, allowing the user to choose individual operations. Instead of commands, the user may also use the emacs menu bar to do text editing, such as undo last operation, cut and past, save file and exit, etc.
2.3. IDE of Emacs
Emacs is more than a text editor. It provides an Integrated Development Environment (IDE) for software development, which include compile C programs, run executable images and debugging program executions by GDB. We shall illustrate the IDE capability of emacs in the next section on program development.
Source: Wang K.C. (2018), Systems Programming in Unix/Linux, Springer; 1st ed. 2018 edition.