X
Tech

Master Emacs text editing in Linux

Cruise along the learning curve for Emacs text editing with these basic commands.
Written by Bruce Stewart, Contributor
Cruise along the learning curve for Emacs text editing with these basic commands.

Emacs is a LISP interpreter that functions as a text editor in Linux, distributed by the Free Software Association. Originally written by Richard Stallman, Emacs has been improved upon by many others in the open software community over the years. Emacs can be used not only to edit files, but also to read mail, read news and run other programs.

Emacs has a strong following among programmers who appreciate its extensibility, since it is highly configurable. In addition, Emacs has a host of built-in tools that allow it to be your interface for many tasks. However, Emacs has a reputation of being cryptic and hard to master, a view which is not entirely unwarranted as there is definitely a bit of a learning curve. Emacs comes with a built-in tutorial that is worth taking the time to complete. To start the tutorial, press Ctrl+h, then type t once you've got Emacs running. And don't worry if you can't find what you need--Emacs also comes with extensive online help.

One advantage Emacs has over vi is its graphical version that will run under X-Windows. On the downside, Emacs is a much larger executable than vi, and requires several times the memory: An old nickname for Emacs is Eight Megabytes And Constantly Swapping.

To start Emacs, simply type emacs at the shell prompt, or type emacs filename to open (or create) a specific file. Once you've opened a file you can just start typing text directly into it. To move around the file use the arrow keys or the following commands:

Command Function
Ctrl+p Moves up one line
Ctrl+n Moves down one line
Ctrl+f Moves forward one character
Ctrl+b Moves back one character
Ctrl+v Moves down one screen
Alt+V Moves up one screen
Ctrl+a Moves to beginning of line
Ctrl+e Moves to end of line
Alt+} Moves ahead one paragraph
Alt+{ Moves back one paragraph

Note: The PageUp and PageDown buttons on most keyboards should work just fine with Emacs as well.

To cut and paste in standard Emacs, you will need to set a mark in the file by pressing Ctrl+Space, then move the cursor to the end of your selection. The portion of your file between the cursor and the mark is called a region; you can easily cut, copy, or move regions around a file. If you're using XEmacs, you can cut and paste just as with other windows applications.

Once you've set a mark and then moved the cursor to another point in the file, you can now use the following commands on the region:

Command Function
Ctrl+k Cut line
Ctrl+w Cut region
Ctrl+y Paste line or region
Emacs allows you to open multiple windows, as well as split your screen up into different windows. If you want to open another file when you're working in Emacs, just type Ctrl+x, followed by typing Ctrl+f. You can then type the name of the file you want when prompted. Here are some commands for working with multiple windows:

Command Function
Ctrl+x, 0 Close window
Ctrl+x, 1 Close all windows except this one
Ctrl+x, 2 Split window horizontally
Ctrl+x, 3 Split window vertically
Ctrl+x, o Move cursor to next window

Undoing your actions
An important feature of any editor is the ability to undo any action if you mistakenly performed on your file. In Emacs, type Ctrl+x, then type u to undo the last thing you did. If you need Emacs to abort its current process, type Ctrl+g.

To close and save a file in Emacs, you need to press Ctrl+x, followed by Ctrl+s. Or, to save the file as a new file, press Ctrl+x, followed by pressing Ctrl+w. When prompted, type a new file name. If you want to quit Emacs, type Ctrl+x, then type Ctrl+c.

Editorial standards