Previous Section
 < Day Day Up > 
Next Section


GNU Emacs Editor

The other popular editor used by the Linux developer community is the Emacs editor from the open source GNU organization. This editor is a desktop application and provides a window type environment in addition to the traditional command-based control. It is newer than the vi editor discussed in the previous section. For Microsoft Windows-based users, this editor looks somewhat similar to the Windows WordPad application. But it is important to note that the key combinations used in the WordPad should not be attempted with the Emacs editor. Emacs and the WordPad are similar as both are (a) desktop applications providing the menu-based command set, (b) able to interact with the mouse, and (c) able to create document files in addition to the simple text files. However, the Emacs editor is much more advanced and feature-rich than the WordPad in many ways.

The Emacs editor is capable of

The editor is also available on a variety of UNIX operating systems including Linux and a very large variety of hardware platforms. As a desktop application, the editor seems to be easier to begin using compared with the vi editor, but do not underestimate the wealth of commands needed to be acquainted with the editor. It provides a very large set of commands supporting a number of features and is extensible through the ‘elisp’ language. In this chapter, we will attempt to familiarize the Linux beginner with the Emacs editor, using an easy-to-understand approach. Those who intend to master this editor might also want to refer to the detailed documentation from the GNU project. The simple way to start the editor is by typing the emacs command at the shell command prompt (with or without the file name to be edited) as shown here.

    $ emacs            
    $ emacs <file name>

If the file name is not provided, the Emacs editor is invoked with an empty buffer to start with. If a file name is provided, the file is opened in a buffer and is set as the current working buffer. The editor is capable of opening multiple files and keeping them in separate buffers. The buffers can be managed using the menu options or commands. The main window of the editor can be split into multiple windows, each displaying a different buffer (or file). However, we will work with only one buffer at a time, although we can switch across buffers (and split windows) for convenience. The editor can also be invoked from the start menu of the desktop. It may be located in different application groups in the Start menu based on the desktop being used (GNOME or KDE) and also based on the Linux distribution being used, such as Red Hat, SuSE, Mandrake, and others. However, it is easily locatable in the Start menu. The main program window is displayed in Figure 3.2 (as it appears on Red Hat Linux 8.0) and Figure 3.3 (as it appears on SuSE Linux 8.1).

Click To expand
Figure 3.2: The Emacs editor window in Red Hat Linux 8.0 (GNOME desktop).
Click To expand
Figure 3.3: The Emacs editor window in SuSE Linux 8.1 (KDE desktop).

As mentioned in the previous chapter, the window appearance may be different based on the Linux distribution and the desktop being used, but the features will be the same for the same version of the application.

Command Notation

The commands are mainly two types: those that operate on entities at the base level (such as characters, lines, and so on) and those that operate on language-based entities (such as words, sentences, and so on). The first type of commands usually form the combination of the letter alphabets with the control key (CTRL), and the second type of commands usually form combination with the alt key (ALT), though there is no rule to this effect. The ALT key is identified as the Meta key in Emacs terminology. Instead of combining the command character with the ALT key, we may also press the ESC key and release it, followed by the corresponding command character. Therefore in Emacs, there is no concept of command mode and insert mode as in the vi editor. If the characters are typed without combining with the CTRL/ALT key or without using the ESC key, then they are automatically considered as part of the text being edited. The concepts will become clearer as the commands are discussed in the following sections. The characters must be considered case-insensitive unless otherwise mentioned. Also in the command listings, a + sign is used to indicate that the two keys on either side of the + sign must be pressed together. Some commands might have more than one character to be pressed while keeping the CTRL or ALT key pressed. In such cases, the multiple characters are written closely, as in ‘CTRL+xc’. This is actually equivalent to ‘CTRL+x’ followed by ‘CTRL+c’. The book chooses the former notation as it is simpler than the latter. Some commands might require that the CTRL/ALT key should be released after pressing one character (or the first character), and then the user will continue to press other character(s) to complete the command. In such cases, the characters to be pressed after releasing the CTRL/ALT key are written with a separating space, as in the example ‘CTRL+x 2’. Some commands, when executed, need additional input from the user and hence prompt for the input in the (small) message window, which is at the bottom of the editor. After entering the required input, the ENTER/RETURN key should be pressed. If we do not wish to enter input and, rather, wish to cancel the command, we can do so. The command to cancel an initiated command is ‘CTRL+g’ and is discussed in the following sections. The message window is also used by the editor to display any messages as a result of executing the commands.

Closing the Editor (Saving or Discarding Changes)

Table 3.5 describes the commands necessary to close the editor, save/discard the changes to the buffer.

Table 3.5: Emacs Commands to Close, Save, and Discard Buffer

Command Character(s)

Command Description

CTRL+xc

Close the editor. The editor can also be closed by clicking the window close button, which is normally located on one of the top corners of the window (marked as ‘X’), or is equivalent to the File ® Exit Emacs menu option. In all the cases, if the contents of one or more of the buffers have changed since the last time they were saved, a message will be displayed in the message window asking the user whether to save or discard the changes. An appropriate action is taken based on the answer provided by the user.

CTRL+xs

Saves the contents of the currently active buffer. If the buffer was not assigned an appropriate file name, the user will be prompted in the message window to provide a file name to save the buffer contents. This is also equivalent to the File ® Save (Current Buffer) menu option. There is a variation of this command, ‘CTRL+x s’, which is discussed in a later section.

CTRL+xw

Saves the contents of the currently active buffer with a different file name (from the original name). When this command is invoked, the user is prompted in the message window, to enter a file name. The user may provide a new file name if desired so, or may discard the command action by pressing ‘CTRL+g’ (as explained below).

CTRL+g

Often, the editor may be waiting for user input as a result of a command initiated by the user. But if the user decides not to give the input and hence to cancel the currently initiated command, then the ‘CTRL+g’ command cancels the current command (or quits from the initiated command).

CTRL+_

Performs undo on the last action performed. This is also equivalent to the ‘Edit ® Undo’ menu option.

Navigation Commands

The navigation commands are very important for anyone working with a document or text file, as these improve our productivity. Table 3.6 describes the Emacs navigation commands most commonly used.

Table 3.6: Emacs Navigation Commands

Command Character(s)

Command Description

CTRL+v

Moves the cursor position forward by one screen.

ALT+v

Moves the cursor position backward by one screen.

CTRL+f

Moves the cursor position forward by one character.

CTRL+b

Moves the cursor position backward by one character.

ALT+f

Moves the cursor position forward by one word.

ALT+b

Moves the cursor position backward by one word.

CTRL+a

Moves the cursor position to the beginning (first character) of the current line.

CTRL+e

Moves the cursor position to the end (last character) of the current line.

ALT+a

Moves the cursor position to the beginning (first character) of the current sentence (to the line where the current sentence began).

ALT+e

Moves the cursor position to the end (last character) of the current sentence (to the line where the current sentence ends).

CTRL+p

Moves the cursor position up by one line (or the previous line).

CTRL+n

Moves the cursor position down by one line (or the next line).

CTRL+l

Scrolls the text around the cursor up/down so that the line containing the cursor is displayed in the center of the screen. Here the character pressed along with the CTRL key is the letter ‘l’ (and not the numeral 1).

ALT+>

Moves the cursor position to the end of text in the buffer.

ALT+<

Moves the cursor position to the beginning of text in the buffer.

ALT+}

Moves the cursor forward to the beginning of the next paragraph.

ALT+{

Moves the cursor backward to the beginning of the previous paragraph.

Apart from these, the standard up, down, left, and right arrow keys and the page-up and page-down keys also can be used to assist the user in navigating the file. However, as these are almost de-facto standard in most editors, more practice with the Emacs specific commands is recommended to enhance the user’s familiarity with that editor.

Buffer Manipulation Commands

It has been pointed out earlier that the Emacs editor is capable of opening multiple files or handling multiple buffers in the same session. A buffer is a chunk of memory that Emacs maintains to hold the contents you are typing. If you open a file in any one of the several possible ways (such as File ® Open File … menu option, providing the file name while invoking the Emacs editor, and so on), the contents of that file are maintained in a buffer. Similarly, you may be just typing something without even saving it to a file; all that you type is stored in a buffer. Both these types of buffers may be maintained at the same time.

Similar to the way that Emacs maintains multiple buffers, it can also split the current window into multiple sections (or split windows) to handle the multiple buffers. This is usually advantageous when we are writing an application and trying to work with multiple source files at the same time. However, it is not necessary to have a one-to-one relation between the buffers and the split-windows. In fact, frequently there is no such correspondence between the number of active buffers and the number of open windows. Table 3.7 describes the commands that are used to work with multiple buffers, windows, and files.

Table 3.7: Emacs Commands to Work with Buffers, Split Windows, and Files

Command Character(s)

Command Description

CTRL+x 2

Split the current window horizontally into two windows one above the other, enabling it to display more than one buffer at a time. The same result is achieved by selecting the File ® Split Window menu option.

CTRL+x 3

Split the current window vertically into two windows side by side, enabling it to display more than one buffer at a time. There is no corresponding item in the menu for this command.

CTRL+x 1

Display the current window at the complete screen size, closing all the other windows. The same result is achieved by selecting the File ® Unsplit Windows menu option. Here we are using the numeral ‘1’ (one) and not the letter ‘l’.

CTRL+x 0

Close the current window where the cursor is positioned. All other split windows are still open.

CTRL+x ^

Make the current window taller by one line. This command works only for horizontally split windows. It has no effect on windows split vertically.

CTRL+xb

This command lists all buffers in a split window. We can also see the list of all buffers without splitting the window by selecting the menu item ‘Buffers’ in the main menu. When the cursor is placed on this item, the drop-down list shows all the buffers.

CTRL+x b

This command enables you to switch to a different buffer. Notice the minute difference between this command and the previous command. You should type the letter ‘b’ after releasing the CTRL key.

CTRL+x s

This command will attempt to save all those buffers that have changed since the last time they were saved. When this command is executed, you will be asked to confirm saving each of the buffers. Notice the difference between this command and the ‘CTRL+xs’ command discussed earlier.

CTRL+xf

This command is known as ‘find a file’ in Emacs terminology, because it actually attempts to open the file specified as the input parameter in the message window. The result of this command is equivalent to the File ® Open File … menu option.

CTRL+xr

This command performs a ‘find a file’ operation, but opens the file in read-only mode.

CTRL+x i

This command is known as ‘insert file contents’, which means that we are trying to insert the contents of another file into the current file buffer at the current cursor position. When this command is executed, you will be prompted to enter the name of a file whose contents should be copied into the current file buffer. The original file will be intact and is not opened as a separate buffer or updated by the editor.

Edit Commands

Commands that work on (or affect) the text being edited are usually treated as edit commands. Table 3.8 provides a description of the most commonly used edit commands.

Table 3.8: Emacs Edit Commands

Command Character(s)

Command Description

CTRL+d

Deletes the character under the current cursor position.

ALT+d

Deletes the part of a word from the current cursor position to the end of the current word.

CTRL+k

Deletes (kills) the part of a line from current cursor position to the end of the current line. The content deleted using this command is stored in a temporary buffer and can be restored by ‘CTRL+y’ command if executed immediately (or before the stored contents are replaced by another similar delete command).

ALT+k

Deletes (kills) the part of a sentence from current cursor position to the end of the current sentence. End of sentence is identified by the next immediate occurrence of the period ‘.’. The content deleted using this command is stored in a temporary buffer and can be restored by ‘CTRL+y’ command if executed immediately (or before the stored contents are replaced by another similar delete command).

CTRL+y

Restores (or pastes) what has been deleted by ‘CTRL+k’ and ‘ALT+k’ commands. If the restoration is attempted more than once, then contents are pasted repeatedly (and may produce undesired output, if that is not what is expected).

ALT+<n> <command>

This command is used to repeatedly execute a command <n> number of times. For example, if you desire to delete the next 12 characters from the current cursor position, you could execute the command ‘ALT+12 CTRL+d’. What this means is that the ‘CTRL+d’ command is

ALT+<n> <command>

automatically executed 12 times. This command can also be used to insert repeated occurrences of ‘n’ number of ASCII characters (other than numeral digits), as in ‘ALT+6 *’, which would insert the string of 6 stars ‘******’ in the current cursor position.

CTRL+<space> & CTRL+w

This command duo enables us to mark a region of text (with the first command ‘CTRL+<space>’) and then delete the marked region (with the second command ‘CTRL+w’). However, between the first command and the second one, the cursor should be moved to the position where the marked region ends. After executing the first command, when the cursor is moved (forward or backward using the arrow keys or page-up/page-down keys or navigation commands), the region is marked in different colors for easy identification.

CTRL+<space> & CTRL+xu

Marks a region using the first command and then converts all the text in the region to uppercase using the second command ‘CTRL+xu’.

CTRL+<space> & CTRL+xl

Marks a region using the first command, and then converts all the text in the region to lower- case using the second command ‘CTRL+xl’.

ALT+u

From the current cursor position, convert all the remaining characters (to the right of, and including the cursor) to uppercase.

ALT+l

From the current cursor position, convert all the remaining characters (to the right of, and including the cursor) to lowercase.

ALT+c

Convert the character at the current cursor position to uppercase.

CTRL+x h

Mark the whole buffer as selected, and highlight with different color, for easy identifying.

ALT+h

Mark the whole paragraph containing current cursor position.

In addition to the above commands, the DEL key deletes the character under the cursor, and the backspace key ‘® deletes the character previous to the current cursor position.

Miscellaneous Other Features

In addition to being an editor, Emacs performs many other functions. You can find differences between two or more files, search for specific strings in files, execute shell commands, merge files or buffers, and so on. You can also set compiler preferences for popular languages, compile programs, and build applications using the make utility. All these options are available under the Tools item in the main menu of the Emacs main window. The preceding discussion presented only some of the most commonly used commands; there is much more that can be explored from this editor.

It is important to note that most of the desktop-based IDEs (Integrated Development Environments) developed in the modern Linux era provide their own source editors, which are naturally graphical and mouse-driven and provide an environment very similar to the Windows-based IDEs. Examples of such tools are Borland Kylix, Borland JBuilder, Oracle JDeveloper, IBM WebSphere Studio, and so on. While working with these applications, we do not need to use either the vi or the Emacs editor. However, these two editors are native to the UNIX environment and are treasured by the users; one who is a master of these editors always uses them even if the modern IDEs provide state-of-the-art editing features. This is because once you master these editors, you feel very comfortable with using them. Additionally, if you are using programming environments such as GNU C++, you need an editor because the GNU C++ does not provide an IDE for development. It is a package of executables and libraries that work in the background mode and give you the final output. Industry experts agree on the fact that graphical environments always bring in a lot of overhead, no matter how efficient they are, no matter how productive they are, and no matter how powerful your computer is. On the same computer, a non-graphic editor such as vi is faster and more efficient than any graphical editor. However, a graphical editor may provide some other features not offered by vi. Therefore, as an end-user, it is up to you to decide between the two types of editors.



Previous Section
 < Day Day Up > 
Next Section