Beginning Emacs in 10 minutes

2009.01.01

Emacs is a great text and code editor but it can be very confusing for someone trying to learn to use it.  After you read this you should feel comfortable launching emacs and opening a file, editing the file and using copy/paste.  Future articles will investigate installing add on lisp files such as a mode just for php or even a twitter client.  Installing emacs is outside of the scope of this article but I’m going to direct you to the following two distributions for windows and OS X users in case you want to try it but don’t have it installed.  Linux users should search out a tutorial for installing it on your particular distribution for Debian/Ubuntu its normally as simple as apt-get install emacs.

Before we begin a quick note on keyboard commands in this article as well as most emacs documentation.  There are special keys designated as M and C which typically are the Esc and Control keys respectively.  On older keyboards the M key was the meta key on most modern keyboards they can be used as the Esc and Control keys.  The M is commonly referred to as the Meta key and if your keyboard doesn’t have it just use the Escape key.  It’s a good habit to get into to use the Escape key anyway that way if you float from keyboard to keyboard you won’t be reliant on the meta key for quick command entry.

The easiest way to get started is to just launch emacs via your operating systems preferred method and then open a file.  To open a file your going to use the key combination involving the control key.  The Open File command is C-x C-f that means type control-x followed by control-f.  After the open file command is entered you will get a prompt asking for the filename.  You can either type the name of an existing file or a new name to create a new file.  As a handy tip you can use the tab key for filename completion.  You can for instance type “te” and hit tab to automatically fill in testfile.txt provided testfile.txt exists in the current directory.

Once you have the filename entered simply hit enter and it will either load or create the file and you’ll be ready for editing.  The next thing you should investigate is how to move around in a file.  Most emacs installations will allow you to use the arrow keys for movement but the basic method for movement involves using the control key and 4 directional keys.  There are also more complicated methods of moving by word or page/screen.

Now you have a good basic understanding of how to open a file and move around in it we can take a quick look at how to save.  Saving your file can be accomplished with the C-x C-s command, that is type control-x followed by control-s.

Copy/Paste is one of the things that I think confuses most people in editors like VI and emacs that don’t rely on a mouse.  Emacs is fairly straightforward for copy/cut and paste.  There is a fast way if you wish to just move an entire line do the following

C-k will cut everything from where the cursor is until the end of the line.  It looks like you just deleted the whole line but what you actually did was cut that text as if you were placing it on the clipboard.  To place the text back at a different location you can use the keyboard command C-y to yank that text back into your document.

To mark an area of text such as several lines or an entire paragraph you can use C-space to indicate the start of the selection area and then move your cursor to the end of your desired text and type M-w to copy that region of text.  If you want to cut the text rather than copy it you would use C-w instead of M-w.  Now that you have the text copied or cut you can use the same C-y to yank it back into the document at the location of your cursor.

To recap the copy a region and paste it elsewhere do the following:

  1. Mark the start of the region with C-space
  2. Mark the end of the region with M-w (that is usually Esc-w)
  3. To paste the text use C-y to yank it back into the document

If you want to cut rather than copy you can use C-w instead of M-w and when you type the C-w at the end of the region your selecting the text will be removed from your current location back to the location where you hit C-space.

If you get into trouble just type C-g a few times to abort whatever you have partially entered into the command area.  Another useful item is undo which is C-x u  that is control-x then ‘u’.

Getting started command summary:

  • Open File: C-x C-f
  • Save File C-x C-s
  • Cut a line from cursor to end C-k
  • Yank the clipboard back into the buffer i.e. paste C-y
  • Mark start of a region C-space
  • Mark the end of a region for copy M-w
  • Cut a region of text C-w at the end of the region
  • Exit emacs C-x C-c

The below image links to a quick one minute screencast of the information covered in this post.

You can find a handy emacs reference card here that I’d recommend printing out and keeping handy as you begin using emacs.  In future posts I’ll further explore using emacs for more complicated things like spell checking and special modes for editing source code and using split windows.

Categories : tips  tools
Tags :

Leave a comment