LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
::Linux Guide
::Tips
::Connections
::Alternatives
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: New Users :: Beginners Linux Guide :: Using Emacs

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-fMove forward a character
C-bMove backward a character
 
M-fMove forward a word
M-bMove backward a word
 
M-aMove to beginning of sentence
M-eMove to end of sentence
 
C-nMove to next line
C-pMove to previous line
C-aMove to beginning of line
C-eMove 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-cExit Emacs
C-x C-fOpen a file
C-x C-sSave the file
C-x uUndo
 
C-gCancel command/get rid of prompt
M-dDelete word
C-kDelete line
 
C-fMove forward a character
C-bMove backward a character
M-fMove forward a word
M-bMove backward a word
M-aMove to beginning of sentence
M-eMove to end of sentence
C-nMove to next line
C-pMove to previous line
C-aMove to beginning of line
C-eMove to end of line




About the author, Oliver Pfaff.

USERS COMMENTS


                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell