By Oliver Pfaff
Emacs introduction
This is a short tutorial intended to provide a person with the necessary
knowledge to perform basic editing tasks.
Emacs and Vi (Vim) have long been the two most popular editors on the Linux
platform but this has changed in recent times with the advance of the Linux
GUI. As a result many people have never used a command line based editor on
Linux and find themselves unable to come to grips when something goes wrong.
For a new user an attempt to make a simple change in a config file can quickly
end in frustration.
This is because like Vi, the keyboard shortcuts are
nothing at all like the ones people may be used to from Windows or a Linux GUI,
where Ctrl-c copy's, Ctrl-v pastes and Ctrl-s saves.
Emacs itself can seem very complicated and
difficult to use and it takes a while to learn but getting the basics is very
easy and features like infinite undo give you a lot of room to make or correct
mistakes.
Starting Emacs
If you're using a GUI like Gnome or KDE, open a terminal and type
emacs. You may be able to open it from an application menu but remember that this may not be
available when something goes wrong. If you're already on the command line simply
type emacs. If you're currently using a GUI, Emacs may start in a separate window
from the terminal. We don't want this because it again defeats the purpose of
the tutorial. Type emacs -nw to start it in the terminal.
Emacs command format
As mentioned before Emacs commands are a little different from what you may be
used to. The following command can be used to exit Emacs:
C-x C-c
This means hold down the Control(Ctrl) key, press x and then hold the Ctrl key
and press c. You can hold down the Ctrl key through the entire operation or let
go after C-x and treat C-c as a separate command — it makes no difference.
Most other basic Emacs commands run along the same fashion. We will cover them
as we move along.
Files
To open a file in Emacs do C-x C-f. You will be prompted for the name of the
file to open. Note that you will need to specify the exact name and path of the
file for emacs to open it — no fancy Browse buttons here. If you specify a file
name that doesn't exist, Emacs will create it in the current directory.
Probably the easiest way to open a file is to specify it when starting Emacs.
For example $emacs -nw myfile opens the file called myfile in the current folder. Again, if the file doesn't
exist Emacs will create it. If you open a file and find the content has
disappeared since you last looked at it, don't panic, you probably mistyped the
name!
Navigation
As with most other text editors you can navigate around a file using the arrow
and page up / page down keys. However you should know that it is also possible
to do this with Emacs command, which often provide much finer grained control
and can save you a lot of key bashing to get around:
| C-f | Move forward a character |
| C-b | Move backward a character |
| |
| M-f | Move forward a word |
| M-b | Move backward a word |
| |
| M-a | Move to beginning of sentence |
| M-e | Move to end of sentence |
| |
| C-n | Move to next line |
| C-p | Move to previous line |
| C-a | Move to beginning of line |
| C-e | Move to end of line |
Other important commands
To save the file you are currently working on do C-x C-s.
To undo your last move do C-x u
Another important command for beginners is C-g. Sometimes you may accidentally
enter some command or emacs prompts you for something you don't understand or
want to know about emacs will just drop whatever it was asking you about or
doing and let you get on with it.
To delete a word do M-d and to delete a line do C-k. Both of these commands can
save a lot of key bashing. They will however only delete text to the right of
the cursor.
Summary
Knowing how to find your way around Emacs can save you a lot of grief when
trouble strikes and its definitely worth it to use emacs for a while even if
other easier to use editors are available.
When just starting with emacs it can be a good idea to
keep a short list of essential commands handy until you know them off:
| C-x C-c | Exit Emacs |
| C-x C-f | Open a file |
| C-x C-s | Save the file |
| C-x u | Undo |
| |
| C-g | Cancel command/get rid of prompt |
| M-d | Delete word |
| C-k | Delete line |
| |
| C-f | Move forward a character |
| C-b | Move backward a character |
| M-f | Move forward a word |
| M-b | Move backward a word |
| M-a | Move to beginning of sentence |
| M-e | Move to end of sentence |
| C-n | Move to next line |
| C-p | Move to previous line |
| C-a | Move to beginning of line |
| C-e | Move to end of line |
About the author, Oliver Pfaff.
USERS COMMENTS
|