by Steven J. Owens (unless otherwise attributed)
There are lots of good tutorials out there about emacs in all its many facets. This isn't one of them. This is just an attempt to enumerate, and maybe explain, some of the things I really miss when I have to use other editors. I'm not going to try to be exhaustive, just to give you a little tour, and maybe jumpstart you into using the more powerful features of emacs.
I'll start by explaining some of my favorite "big" items, because that's more fun. I'll put the detailed stuff at the end, but I still think they're very useful.
Also, I recommend you skip ahead to down to the section "Control/Alt/Escape Key Nuances", and read that first, before reading the rest, because I'm going to try to include sufficient details on key combos that you can go try this stuff out as you're reading it.
Note: I'll put this here because it's really important to know, or rather, frustrating not to know:
Keyboard quit is how you interrupt things if you're in the middle of a command.
If you get into the middle of some menu/command sequence and you're not sure what's going on, hitting control-g repeatedly will get you out of it.
Also, sometimes things can apparently freeze up momentarily when you're using control-g to quit out of a command. If you hit control-g a few extra times emacs will offer to "abort and dump core?"
If you've completely given up and just want to get out, go ahead and do that, it's more or less equivalent to killing the emacs process from the outside. However, 99% of the time if you enter "no" and wait for 5 or 10 seconds, it'll resume everything just fine.
emacswiki.org is a really good source for all kinds of info.
The #emacs channel on freenode.org also has some very knowledgable (and often even helpful :-) people.
Emacs predates GUI desktops so a "window" in emacs refers to a subsection inside the emacs window. In some GUIs this is referred to as a "pane" (as in a window divided into multiple panes).
The GUI-level window is referred to in emacs docs and emacs code as a "frame". For the most part this doesn't come up often.
Having multiple buffers and emacs windows and being able to rapidly and fluidly navigate among them are probably what I miss most when I have to use some environment other than emacs.
Emacs distinguishes between the buffer you have open and the window you use to view/edit the buffer.
You can change to a different buffer in one of the windows with: "ctl-x b buffername<enter>".
You can divide your the desktop GUI window for your emacs session into two emacs windows "ctl-x 2".
You can move the cursor in those separate emacs windows to different locations inside the same buffer, or change to a different buffer in one of those windows.
You can use "ctl-x 2" repeatedly, each time dividing the window you're in into two equal-sized windows.
You can go back to having a single emacs window with "ctl-x 1"
You can rename buffers with:
esc-x rename-buffer<enter>newbuffername<enter>
You can open multiple files, each file is in its own buffer. Or you can have buffers that aren't associated with files yet, just change to a new buffer (ctl-x b buffername<enter>) by specifying a buffer name that isn't in use.
One thing to keep in mind is that the buffer sticks around until you explicitly kill (close) it - out of sight does NOT mean out of mind.
If you open the same file more than once, the duplicate buffers will be named "filename<2>", "filename<3>", etc.
Note: in emacs-land, when you open a file and emacs creates a buffer for you to edit that file in, it's said that the emacs buffer is "visiting" the file. I'm just pointing this out so you know what the heck they're talking about when you run across that in the emacs docs.
You switch between buffers using "ctl-x b". Emacs prompts you:
Switch to buffer (default blahblah):
If you just hit enter you'll go to the default buffer. The default buffer is the last buffer you were in, in this window (more about windows next).
For just two buffers, this is fine, you can do "ctl-x b" forever and keep going back and forth. If you have more than one buffer, you can type part of the buffer name and use autocompletion (press the tab or ? key) to see the matching names of available buffers.
You can also open new buffers to edit text or whatever, simply by switching to a buffer that isn't there, using "ctl-x b"
Switch to buffer: newbuffername <enter>
Generally I don't close buffers, I just save the file I was editing, switch to whatever buffer I need to work on now, and ignore the old buffer. But if you want to or need to make it go away, you can use:
esc-x kill-buffer <enter> Kill buffer: (default blahblah)
You can specify which buffer to kill the same way you specify which buffer to switch to, i.e. with autocompletion.
Occasionally I do some "house keeping" and close a bunch of buffers that I no longer need need, using the buffer-menu:
esc-x buffer-menu<enter>
This gives you a list of buffers, one buffer per line. You can use the regular emacs line navigation commands to move around, or search. When your cursor is on a buffer line, you can type "o" to open that buffer, or "d" to mark the buffer for deleting. When you've marked all the buffers you want to delete, type "x" to execute the delete commands.
For example, I was messing around in my "recipes" directory, the other day, and I usually end up opening eight or ten different recipe files. So I bring up the buffer menu and quickly close all of those buffers.
While we're at it, the shell buffer (esc-x shell<enter>) is awesomely handy, since it basically gives you a shell prompt that you can page back and forth in, like an emacs buffer. It's not perfect - programs that use cursor manipulations will probably not be happy in the shell buffer. But 9 times out of 10 I find it incredibly handy.
Note that if you already have a shell open, "esc-x shell" will take you to that shell buffer. To open multiple shell buffers use "esc-x rename-buffer<enter>newbuffername<enter>" to rename the shell buffer.
Note that the shell buffer's default name is "shell", but those asterisks are just convention, you can name it whatever you want, and you can name any buffer with asterisks if you like.
Maybe there's code somewhere in emacs that cares about those asterisks and treats that buffer differently because of them, but I've never seen it.
I generally keep the asterisks, since that makes it easy for me to find shell buffers by using autocompletion.
I find desktop-save-mode really, really handy. In a nutshell, it lets you shut down emacs and start it up later - and it reopens all of the files you had open and restores all of your buffers! (Well, except for shell buffers.)
Go to the emacswiki to learn how to set up and use desktop-save-mode.
Emacs predates GUIs. In your GUI (windows or mac or whatever) the window is usually synonymous with the entire app, but in emacs you can subdivide your visible editing space into multiple windows.
Right now, as I type this, the top half of my emacs GUI window is an emacs window into a buffer running a network client that I'm using to connect to a chat server. The bottom half of my emacs GUI window is further split in half, one half for the buffer I'm typing this in, and another half for a buffer on another file.
Note: What the desktop GUI world typically calls a window, and which I usually call a "GUI window", the emacs world calls a "frame". See "Translucency and Windows and Frames", below.
You can keep opening more windows until all you have left on your screen is window bars (solid light bars that separate the windows). For the most part, two windows is optimal to work with, three if two of them aren't too busy, and four is pushing it. If I have to keep track of more buffers than this, I generally switch buffers in and out.
The windows commands are generally:
Note that all of these windows are split vertically, that is, into upper/lower windows. Emacs can also split horizontally, which used to have a default keybinding of "ctl-x 5", but these days you have to add a keybinding or just invoke the function "esc-x split-window-horizontally").
I find split-window-horizontally really useful for doing side-by-side comparisons and/or diffs. Also, in recent years the 16x9 ratio has taken over the monitor market, so monitor screens have gotten a lot wider, and now horizontally split windows are more useful.
Emacs 23 and 22 support trasparency or translucency, i.e. being able to see through the emacs window to the underlying GUI window.
I find this particularly useful if I'm using emacs as a chat client or irc client; I can have the chat going on in the background window while I work in a foreground window.
However, the only way to make this work is to use distinct emacs "frames" or GUI windows.
To quote from: http://www.emacswiki.org/emacs/TransparentEmacs
Insert the following into your init file (in most flavors of emacs, the .emacs file):
; Set up Translucency ;;(set-frame-parameter (selected-frame) 'alpha '([ ])) (set-frame-parameter (selected-frame) 'alpha '(85 50)) (add-to-list 'default-frame-alist '(alpha 85 50)) ; ; set up control key to turn translucency off/on. control-c followed by t (eval-when-compile (require 'cl)) (defun toggle-transparency () (interactive) (if (/= (cadr (frame-parameter nil 'alpha)) 100) (set-frame-parameter nil 'alpha '(100 100)) (set-frame-parameter nil 'alpha '(85 50)))) (global-set-key (kbd "C-c t") 'toggle-transparency) ; ; set up a function to set specific translucency values. ;; Set transparency of emacs (defun transparency (value) "Sets the transparency of the frame window. 0=transparent/100=opaque" (interactive "nTransparency Value 0 - 100 opaque:") (set-frame-parameter (selected-frame) 'alpha value))
Add the above to the end of your .emacs file and do:
esc-x load-file<enter> Load file: ~/.emacs<enter>
If this causes any errors, either get help troubleshooting it, or edit those changes back out of your .emacs and get used to disappointment.
To be able to see another emacs buffer under your current emacs buffer, you have to open a separate frame (aka GUI window) to your emacs session. It's also easier if you turn off translucency in whichever frame is going to be in the background most of the time (for me, the frame that shows the irc window):
Now pop open a new emacs frame (GUI window):
ctl-x 5 2
In whichever frame you want to be the background frame, turn off translucency:
ctl-c t
Emacs has a wondrous variety of "modes", some of them general, others very specific. A "minor mode" can be used together with another minor or major mode, or even multiple modes.
Of course, two different modes may both use the same keybinding for something. If this happens, the last mode you loaded will win, as far as that key-binding is concerned. I think there are conventions that keep most minor mode keybindings from conflicting with major mode keybindings.
There are modes that are entire, little apps (or not so little apps) unto themselves.
A bunch of modes come standard with the emacs install, and a bunch more are available for download. This includes several programming language editing modes. You can configure modes to be loaded automatically, and a bunch of them are in the default install.
For example, if you open a C source file, emacs will load and run C editing major mode.
For help on your current major and minor modes, do "ctl-h m".
Generally speaking, you install modes by downloading the mode elisp file, saving it somewhere (~/.el is the customary place in unix) and then adding a line to your emacs init file (usually ~/.emacs in unix) to tell emacs to about that mode file. Then either restart emacs or reload your init file.
This is analogous to adding a binary to your path; it makes the mode available, but you still have to actually invoke the mode (by opening a file of the appropriate type, or by issuing a mode command).
These days a lot of systems have package managers, and a lot of those package managers have emacs modes available for fast and easy system-wide installations.
Autoindentation in emacs programming modes is splendiferous. In most emacs programming modes, if you hit tab, it autoindents to the right level. This is not only really handy from a keeping-the-editing-flow point of view, but if it indents to the wrong level, look again - it's probably right and you're wrong, which means you've made a mistake somewhere.
For diving into elisp, I recommend Steve Yegge's "Emergency Elisp" tutorial. While I certainly want to truly learn Lisp, one of these days, Yegge's tutorial was the first that actually helped me get things done.
"Most Lisp introductions try to give you the "Tao of Lisp", complete with incense-burning, chanting, yoga and all that stuff. What I really wanted in the beginning was a simple cookbook for doing my "normal" stuff in Lisp. So that's what this is. It's an introduction to how to write C, Java or JavaScript code in Emacs Lisp, more or less."
http://steve-yegge.blogspot.com/2008/01/emergency-elisp.htmlThese are smaller, more tactical features, but are part of what makes the emacs experience what it is.
Emacs has a very powerful regular expression engine, and it's available in search and replace, and in macros, etc. These days, regex search is way more common, but remember, all of those editors with regex search are following in emacs's footsteps. And most of them aren't yet as powerful as emacs regex.
Emacs kill is similar to the more robust undo/redo commands, or the more multilevel cut/paste commands, both of which used to be rare and are now a lot more common.
In emacs, when you cut (ctl-k for "kill") some text, it goes into the "kill ring".
You can "yank" back out (ctl-y) the text of your last kill.
You can also yank back out earlier "kills". Immediately after a normal "yank" (ctl-y), before typing any other key, do "esc-y". Emacs will swap out the text you just yanked back in with the text from the kill before that.
If you do "esc-y" enough times, you get to the end of your kill ring and it loops back around to the most recent text you killed.
You can also do "esc-x browse-kill-ring" and emacs will open a window containing the contents of your kill ring, so you can scroll through it and find the text you were looking for.
While we're on it, we should talk about the undo command, "ctl-_", which to write it a bit more clearly, is control-underscore, or (on most keyboards) control-shift-dash.
Undo in emacs is a bit tricky. You can keep hitting the undo command and undoing more and more. But there's no "redo", instead, you interrupt your stream of undo commands (with anything, it can be as simple as typing space, or maybe ctl-g) and then you undo again; essentially you "undo the undo".
This is definitely in the category of minutiae, so I'm putting it at the end.
Emacs has a slew of text-navigation key bindings. Some people hold this against emacs, and yeah, it does make learning emacs a bit of a pain. But when you get used to them, it's like you think it, and it happens.
I especially like the higher-level text navigation keys, because they give me a feeling of navigating around the structure of of the text.Besides the usual forward/backward character, next/previous line, beginning of line/end of line, discussed in the section "Cheat Sheet", below, emacs has commands for:
There are a few code-oriented versions of this, perhaps not enough (or I simply haven't dug in and found them).
In a programming mode forward/backward sentence and paragraph change based on the mode and on what the mode developer decided was equivalent to a sentence or paragraph (usually a code block for paragraph).
Emacs programming modes usually have features for making sure your open/close parentheses match up, and for jumping forward/backward to the next or previous enclosing parens.
Reminder: ctl-g - keyboard quit, to interrupt things if you're in the middle of a command.
Using control/alt keys for line navigtion versus using the arrow keys and pageup/pagedown keys is debatable. I've been using emacs for over 25 yearsd, starting on keyboards that didn't have all these arrows and etc :-). So it's hard for me to objective evaluate it. I do feel like not having to move my hands off the home row to jump around the text helps me keep my rhythm and speed.
When you see ctl-x or alt-x, press and keep the control key or alt key held down while you tap the second key.
When you seek esc-x, tap the escape key, let it up, tap the second key.
In the emacs docs, escape is sometimes called the "Meta" key instead, because Way Back In The Day, not all keyboards had an escape key, but might have another key that could be designated for the role.
Alt-whatever seems to be the default modern alternative to escape-whatever. For some combos I find it helpful (alt-v for page up instead of esc-v seems to break my typing flow less), for others I still use the escape key combo, for example the esc-x prompt.
The esc-x prompt can be really helpful, but it has its own little rules for how it works. When I type an esc-x prompt command like this:
esc-x search-forward<enter>
What that means is, tap "esc", then tap "x", then start typing the command. In the example above, you don't need to type the space between "x" and "search". In fact, if you do, you'll be surprised, because in the esc-x prompt, the spacebar key acts like the tab key, it asks emacs to autocomplete - and emacs is fond of autocompleting.
Speaking of autocompletion, it's really handy, usually you invoke it by tapping the tab key. It may seem like more trouble than it's worth, but the nice thing is that it can help you find the command you're looking for. Type esc-x sea<tab> and emacs shows you:
M-x search-
With your cursor at the end, after the "-". Hit tab again and emacs will open a window showing you the different ways you can complete the command.
Note: a common gotcha in the old days was that control-s was interpreted by your modem program as stopping the stream of text. So emacs never got to see the control-s character and I had to invoke emacs search by using esc-x isearch-forward. I'm still not really in the habit of using control-s because of this.
The emacs search prompt can take a little getting used to, specifically the "i-search" prompt (i for interactive), which is the default prompt. You can get a non-interactive search prompt with:
esc-x search-forward<enter>
But you really should take a little time to learn and get comfortable with the i-search behavior, it's handy.
You're probably better off finding a full tutorial on i-search somewhere, but here's a quick summary:
Press ctl-s or ctl-r.
Emacs displays the prompt "I-search:" or "I-search backward:" Start typing the word you want to search for. Emacs will start moving through the document as you type.
If, for example, your cursor is on the period at the end of the previous sentence and you type ctl-r followed by "i", emacs will move the cursor up to the "i" in the word "moving" on the previous line.
If you then type "n", emacs will keep the cursor where it is on "moving", but you will notice the highlighting will expand to include the "n" in "moving."
If you then type "t", emacs will jump the cursor up several paragraphs to the word "interactive." Note that the word "interactive" occurs twice in that paragraph. Ifyou wanted the previous occcurrence, press ctl-R again, without exiting i-search mode. Emacs will jump the cursor to the next match (in this case, since we're using "I-search backwards", "next" means preceding).
If you make a typo in your search term, you can press backspace and emacs will take the character off your search and jump your cursor back to the previous location.
ctl-h makes the emacs mode line (at the bottom of the window) display the help prompt:
C-h (Type ? for further options)
You then type one letter (b, a, c, k, etc, see below) which dictates what kind of help prompt emacs gives you.
If you tap the ? key you'll get a summary list of the different things you can type at the help prompt.
You can also see this by typing "esc-x help-for-help<enter>".
You should definitely read that stuff and explore on your own, but here are my four (no five (no six!)) favorite help usages (out of the 20-odd alternatives):
A quick cheat sheet of text navigation commands
Really it's control-shift-dash (the dash key, which is usually also the underscore key).
There is no redo command. If you undo multiple times it goes further back; if you interrupt the stream of undo commands (perhaps with a ctl-g) then undo again, it "undoes the undo".
If mark isn't set, then kills from cursor to end of line - does not kill the newline character, you have to do ctl-k again to kill the newline.
(* kill-buffer is also available as "ctl-x k" but I recommend using the esc-x version, it's too easy to accidentally kill the wrong buffer, which is really, really annoying.)
The buffer-menu buffer is really helpful for searching, sorting or pruning buffers. Note that in fully GUI emacs, you can click on the column headings in the buffer menu buffer and it will sort on that column, which is really helpful at times. You can also use i-search inside the buffer window, to search for a buffer with a particular name.
dired-mode: By opening a directory instead of a file, you can use dired-mode, essentially it is a file browsing mode. Each subdirectory you open will get its own buffer, so you sometimes have to go into buffer-menu to prune them away after you're done with dired.
I originally wrote this file in emacs outline-mode. In outline mode, asterisks (*) denote section headers. I sometimes find it really useful for editing large documents.
There are other commands that do things like let you hide/show lower level sections, etc, but that gets complicated. For now, just use hide-body/show-all.
Note that when you do show-all, wherever your cursor is, that's where you'll be in the expanded document. So hide-body, move your cursor to the section you want to read, then show-all.
(* Just loads it, which makes the functions available at the "esc-x" prompt, but you probably still have to invoke the function directly.)