The "Learning GNU Emacs" Thread

Discussion in 'Mac Help/Tips' started by Rotwang, Apr 18, 2003.

  1. Rotwang macrumors newbie

    Apr 18, 2003
    The purpose of this thread is to not only give some tips and tricks but to give new users or the generally curious a crash course or lite-tutorial on this terrific program. I will break it up into easily digestible pieces/posts.

    Emacs Intro

    GNU Emacs is a text editor. Actually GNU Emacs is more of a Lisp interpreter than
    just a text editor. This Lisp interpretation allows Emacs to be easily extended,
    adding new features upon startup or while the program is running. Emacs can
    interpret .el(Emacs Lisp) files or byte-compile them into .elc files.(like Java or C#)

    GNU Emacs extensions range from adding new key mappings, syntax
    highlighting(new languages), new language specific commands, games, or
    even programs such as email, system shells, address books, and a calendar.

    Alot of people like to make fun of Emacs because it can do so much and often cite it as bloatware but it is only as bloated or powerful as you want it to be. If you want an excellent text editor and nothing more you will have it. Most of the things Emacs can do are simply extensions of the core program(which is basically a Lisp interpreter).

    GNU Emacs comes installed with Jaguar. Open up a terminal and type "emacs" and there you go. Carbon and even Cocoa versions are available but I recommend sticking with tty/terminal Emacs for now(you dont have to go download anything. You cant try it out while reading this).

    Here is a pic of Emacs with its configuration file .emacs


    Here is a pic of of Emacs split with the .emacs file to the right, the emacs shell to the left(written in Lisp), and below is the calendar with the holidays highlighted.


    Here is a pic with my favorite Emacs add-on, "dired" the directory editor. In this
    mode or extension you can edit file names, delete files, traverse the system, change file modes, etc etc. Everything is performed like you are working on a text file. The commands that are available are easy to remember one letter commands(position the cursor
    over a file and type "d" to delete the file, type "~" or "#" and emacs will mark all
    of these files(such as the backup files emacs makes by default) for deletion.


    Ok so that was the intro, if you didnt know anything about Emacs you will have at least an idea of what it is and what it can do.
  2. Rotwang thread starter macrumors newbie

    Apr 18, 2003
    Emacs Basic Editing

    Next up we have a crash course in text editing. Everything you need to know to get started. First off, to access the menu press "f10". The Emacs window will split and the menu options will become available to you. Use the arrow keys or type the key letter to select the appropriate menu item/s.

    At anytime in Emacs your "get out of jail free" command is C-g which means hold down the control key and press the "g" button. This will cancel or escape what you were currently doing.

    Emacs is a modeless editor. All of the commands are issued by pressing a key combination and the text is edited like most normal text editors or word processors (simply start typing). Vi on the other hand is a modal editor, in one mode you enter commands and in the other you edit text(a major source of confusion and frustration for many new users).

    Most commands in Emacs executed with either a Control-X or a Meta-X combination(Meta will be either the escape, option, or command key).

    The menu bar is at the top of the screen, its purpose should be self-explanatory. The modeline is the bottom bar with various info such as the file you are editing along with the modes(extensions) you are in(If I am editing c files I will be in c mode(and special functionality will be available to me), etc). The echo area is the blank space below the modeline and this is the area for entering commands(entering modes, searches, opening files, feedback, etc).

    Ok, type emacs .emacs from a terminal. The modeline will show you that you are in (Emacs Lisp) mode and are editing the file ".emacs".

    Type "The quick brown fox" and then hit enter. Type it once more, ok good.

    C-f moves forward 1 space
    C-b moves backward 1 space
    C-p moves up 1 line
    C-n moves down 1 line

    Try these out and move around the two lines you just created. It will seem awkward at first but when used it easily becomes second nature. An added benefit of using Emacs keystrokes is that the Bash shell responds to these by default(although you can change it to vi strokes if you wanted). Ok so now you can move around, good.

    M-f moves forward 1 word (from quick to brown etc)
    M-b moves backward 1 word

    Handy ways of moving around quicker.

    C-a moves to the beginning of the current line
    C-e moves to the end of the current line

    C-d deletes the letter under the cursor.
    M-d deletes the rest of the word from the position of the cursor
    C-k deletes the rest of the line from the cursor position

    C-x u is the undo

    Hold the control key and then hit x, "C-x" will show in the echo area, now hit u

    To exit the program you "C-x C-c" which means hold down control and then it x and then c.

    To save your file do "C-x C-s" and save as is "C-x C-w" which will write the file with the name and path you specify in the echo area(emacs will prompt you) such as "~/test.txt". The ~/ is shorthand for your home directory. To open a file from within Emacs you "C-x C-f" to find the file, if you specify a file that doesnt exist Emacs will create it for you.

    Every file you work on has its own buffer. You could work on several java files at the same time with the Emacs window split or work on each individually with the non-active buffers out of sight(but still opened by Emacs).

    C-x k kills the active buffer(the one your in)

    C-x b switches you back to the last buffer you worked on(prompts you too)

    C-x C-b splits the emacs window. In the new window is a list of your buffers.

    To switch between split windows hit "C-x o". Remember if you screw up hit C-g to cancel. Now navigate the buffer list like a text file and hit enter on the buffer you want to
    change to.

    If you have a split window hit "C-x 1" to display your active buffer full screen (whatever you visually closed is not closed only taken out of site, it remains in the buffer list). "C-x 2" will split your window horizontally and "C-x 3" will split your window vertically.

    By now you should see that C-x is important. It initiates commands. M-x does too.

    "M-x kill-some-buffers" and then hit enter

    Emacs will prompt you for each buffer and you can selectively clean out the buffers that you are finished with.

    Emacs has tab completion so when typing the long M-x commands hit tab at a certain point to complete the command. If 2 commands start with "kill" and you hit tab on the "i" Emacs will complete to "kill-". Hitting tab again will open a window showing the commands beginning with that word. Typing "so" and then tab will complete this command. Tabbing with just C-x or M-x will give you the full list of commands.

    Remember once again your "C-g" escape in case you just want to cancel whatever your doing.

    Ok, you know what Emacs is, how to start and exit the program, save and save
    as(write) files, work with buffers, use the menu, and how to do basic editing.

    If you are interested in using Emacs practice this stuff or start the Emacs tutorial "C-h t"

    Coming soon "Advanced Editing", "Configuration, and "Extensions".

    (if people are interested I will add a down and dirty "Creating your own Emacs Lisp Extensions")
  3. kylos macrumors 6502a


    Nov 8, 2002
    Did you just bash vi?!?! Oh let's not go there man, let's not go there.
  4. Rotwang thread starter macrumors newbie

    Apr 18, 2003
    Emacs Advanced Editing

    Moving to the beginning or the end of a file

    M-shift(hold shift key)-< moves to the start of a file
    M-shift-> moves to the end of a file

    Cutting, Copying, and Pasting

    To copy and cut text you have to let Emacs know which text you want to manipulate. To do this you must mark your selections.

    C-SPC(spacebar) sets the mark, now move the cursor wherever you want. If you cant remember where the mark is, M-x transient-mark-mode enter(hit enter). Now Emacs will highlight your text

    Ok so you have some text highlighted and you want to copy it

    M-w copies your selection
    C-w cuts your selection

    To paste it simply position the cursor and type

    C-y to yank the copied text to your file


    C-s initiates a forward search

    Type "The quick brown fox" and then C-a to move to the beginning of this line

    C-s o This will highlight both o's and place your cursor at the first one.

    To move to the second either hit C-s again or type "x" and "ox" will be selected along with cursor placement.

    Once you are at f-ox you can move around, edit, or whatever you want.

    C-r initiates a reverse search

    C-r qu will move you obviously to "qu"

    Searching is a great way to move around large chunks of text quickly as well as its
    more obvious application of finding a selection.

    Query and replace

    M-x query-replace Tabbing at "que" will complete this command. You will be prompted in the echo area for the text you want to replace along with the replacement text, hitting "?" will show you some options. "y" or "space" will replace one match, "delete" or "n" will skip to the next occurence if any, "!" to replace all the other occurences in the file, "^" to move to the previous match and "E" to edit your replacement string.


    C-v scrolls down
    M-v scrolls up
    C-l centers the screen on your cursors line

    Multiplying commands

    C-u 8 C-f Will move forward 8 times

    C-u 5 C-b Will move backward "C-b" 5 times


    C-x r space a Saves your cursor(point) position in register "a"

    C-x r j z Jumps to your saved "z" register position

    C-x r s z Copies selection (region) into register z for later use
    C-x r i h Inserts your "h" register text

    Bookmarks are sort of like registers but you can give them long names

    C-x r m RET(hit return) to set the bookmark for the file at cursor(point)

    C-x r m blah RET to set the bookmark names "blah" at the cursor(point)

    C-x r b blah RET jump to the "blah" bookmark

    C-x r l lists all bookmarks

    M-x bookmark-save saves all current bookmarks into the default bookmark file

    Transpose characters teh

    C-t on the "h" in "teh" will switch the "h" with the "e" for "the"

    M-t on the "t" will transpose or switch "teh" with "characters"

    Case Conversion

    M-- M-l Converts last word to lower case (M-- is Meta-minus)
    M-- M-u Converts last word to all upper case
    M-- M-c Converts last word to lower case with capital initial

    Last but not least Macros

    C-x ( <div="blah"> SPC </div> C-a )

    C-x ( starts macro recording, insert the text and execute the commands like normal and then C-x ) to stop the macro. Now hit C-x e to execute the last macro

    M-x name-last-kbd-macro prompts you for the name of the div macro, I called it divblah. Now just M-x divblah RET and that macro executes

    M-x insert-kbd-macro RET blah RET

    Saves your defined macro to the your open file. It writes the appropriate Lisp code for you into that file, then save the file. For now save your macros in ~/.emacs that way Emacs autoloads them without anymore fuss from you(.emacs is the emacs config file read everytime Emacs starts)
  5. Rotwang thread starter macrumors newbie

    Apr 18, 2003

    No. :)
  6. richie macrumors member

    Jul 16, 2002
    Melbourne, Australia
    Good write-up for emacs. That said, I'm no emacs fan. My feeling, using it, is it *is* somewhat bloated, and the keystrokes I just find irksome and overly complicated. Of course, I don't/didn't use any of the features of emacs other than the text editing, which could explain why I didn't take to it.

    The problem for emacs, at least in the mac osx context, is that it does all these things but in a way that can be achieved, imo, so much more elegantly via a GUI program that you could find on versiontracker or macupdate. The only text editing I'll want to be doing via the command line in Mac OSX is simple, which is why I stick to pico/nano.

    But I digress, I don't want this to be an emacs/vi war. :) Just wanted to say, good write-up :)
  7. bousozoku Moderator emeritus

    Jun 25, 2002
    Gone but not forgotten.
    No, he didn't, but I'd be glad to do that. :D

    Dealing with a text only system, there's no other editor I need but emacs. Even the old, non-bloated public domain micro emacs works wonders. If I didn't have emacs, I'd end up using ed.
  8. kylos macrumors 6502a


    Nov 8, 2002
    If you're using a text only system, I would probably agree. Vi is mostly good for a quick edit. Just trying to make things exciting.:D
  9. mc68k macrumors 68000


    Apr 16, 2002
    i wrote my folding scripts entirely in vi
  10. bousozoku Moderator emeritus

    Jun 25, 2002
    Gone but not forgotten.
    If I type perfectly, I can use vi very quickly because it does move more quickly than emacs. After I'm finished, I spend 30 minutes trying to remember how to save the file and get out. :D Don't even ask about how it goes if I make a mistake.

    emacs keystrokes have even been used in several big name (at one time) products: Epsilon programming editor for PC-DOS, PerfectWriter for CP/M, and then, Borland's editor can emulate Epsilon and therefore, emacs keystrokes.
  11. kylos macrumors 6502a


    Nov 8, 2002
    Yeah, vi is unforgiving. And to quit it's just :wq (after hitting esc, of course).
  12. bousozoku Moderator emeritus

    Jun 25, 2002
    Gone but not forgotten.
    Rotwang, thanks for bringing us this thread. I think it should prove invaluable, especially the LISP code. :)
  13. frescies macrumors regular

    Dec 9, 2002
    Los Angeles, CA
    Does it work on other unix variants?

    Does it run on other UNIX variants?

    Also, what is the website for EMACS?
  14. bousozoku Moderator emeritus

    Jun 25, 2002
    Gone but not forgotten.
    emacs runs on pretty much any machine that has a C compiler and more than 128 KB of RAM.

    You should be able to find it at
  15. Rotwang thread starter macrumors newbie

    Apr 18, 2003
    A modern Emacs comes with Jaguar.

    A GUI Carbon Emacs can be found here. This version will make it into the official Emacs when it hits 21.4 AFAIK.

    A GUI Cocoa Emacs can be found here. This one is not as stable as the carbon build.

    Emacs Wiki and DotEmacs are pretty good resource sites in case your anxious to learn more. You cant go wrong with the Emacs Manual(f10 help menu from within emacs) either.

    M-x viper-mode

    This will give you 5 levels of vi emulation making the whole Emacs/Vi argument largely irrelevant.

    viper-mode is just one of many things that show off the sheer power and flexibility of Emacs and its Lisp engine. Emacs is more than just a program, it is a programming environment unto itself.
  16. Rotwang thread starter macrumors newbie

    Apr 18, 2003
    Emacs keystrokes are fairly common place.

    Try using them when you create a Macrumors post :)

    C-f forward
    C-b backward
    C-p up
    C-n down

    C-a beginning of line
    C-e end of line
  17. bousozoku Moderator emeritus

    Jun 25, 2002
    Gone but not forgotten.
    There's also Xemacs which runs in XWindows.

    Sorry, but emacs keystrokes don't work when I'm posting using Camino on MacRumors.
  18. Rotwang thread starter macrumors newbie

    Apr 18, 2003
    GNU Emacs runs in X too.

    Emacs keybindings obviously wont be everywhere on every app ever made but they appear to work in Safari, Mail, Sherlock, TextEdit, Addressbook, tcsh/bash/zsh, Calculator, iCal, etc. Emacs keybindings seem to work everywhere on my apple system.

Share This Page