This is a tutorial for the absolute basics of how to use emacs.
Emacs is a programmer’s editor, not a word processor. Editors came first, programmers invented them to make their lives easier. Word processors came later, when non-programmers started using computers. Editors are somewhat analogous to IDEs (Integrated Development Environment).
Emacs is a very powerful and useful editor. This tutorial’s intent is to get you started. To really learn about Emacs you should dig up a GNU Emacs manual or file or try the ‘learn Emacs’ feature that’s built into Emacs, or the Emacs help function (Esc x help-for-help).
GNU Emacs is the standard version, but there are other versions of Emacs out there. I’m going to assume GNU Emacs for the rest of this tutorial.
I originally wrote this literally 25 years ago, and for a different context. I also wrote it mostly about the text user interface version of emacs, which is surprisingly close to the GUI version. I gave it an editing pass and did some major rearrangement, but there may still be some rough spots.
As I rewrote this, I found myself digressing a lot to explain various important emacs concepts, like frames, windows, buffers, mark, regions, etc. These are useful and important and part of how you get the insane effectiveness of editing with emacs, so I decided to move that all to later, and start with the basic editing stuff first. If I slipped up and mention one of these advanced concept terms before we get to that in a later section, sorry about that.
I’ll use [enter] to mean press the enter or return key.
I’ll use bold text for words you’re supposed to actually type.
Ctrl-Anything
Esc Anything
Meta
key.
Esc x blah-blah
or Ctrl-X blah-blah
Esc x
or Ctrl-x
and wait a moment, and either M-x
or C-x
will appear on the bottom line of your screen. Start to type blah-blah. Emacs command names aren’t allowed to have spaces, they use a dash (-) character between words. Note that a few, fairly rare commands in emacs use Esc c
instead of Esc x
.
Tab and ?
Tab
key for auto-complete, sometimes called command completion (though not when actually typing text). In particular, while typing Esc x blah-blah
or Ctrl-X blah-blah
, if you press Tab
, Emacs will try to complete the command for you. If you press Tab
twice, or press ?
, Emacs will list the possible completions of the command. You can continue typing and using Tab until you get the entire command, then press [enter].
If you’re coming to emacs from vi or vim, emacs is more like a normal editor or word processor, you don’t have input mode vs editing mode, you just type characters to add them. All editing commands are done with Ctrl, Esc, Alt, and so forth.
Note: Speaking of “mode”, that’s an important word in emacs concepts. It has the same very general meaning, but emacs has lots of modes, and in fact they’re a major building block of emacs. See the section titled “Modes”, under “More Powerful Emacs Concepts”, below.
The arrow keys do all the usual things for moving the cursor around.
For other versions of emacs, the key binding for quitting and exiting varies. Ctrl-c c, Ctrl-x c, Ctrl-c Ctrl-c, etc, often do the job.
When I can’t do it any other way, this almost always works:
Esc x kill-emacs [enter]
Ctrl-h is the standard help character. Usually you press Ctrl-h and the immediately press another character for what kind of help you want.
If you press Ctrl-h once and pause for a second and you’ll see “C-h (Type ? for further options)-” in the message window. If you then press “?” emacs will divide the GUI window into two windows, one of which will contain an explanation of how to use the help.
I’ll try to add a more thorough explanation of help below, but I wanted to make sure I at least got these basics explained.
Emacs also has a bunch of handy commands for moving around more quickly, i.e. not moving by character but by line, by word, by paragraph, and by buffer (roughly the same as “file” but see the section on buffers below). You don’t need to worry about them right away, but after you get to know them, they’re really handy.
The two most basic and immediate are:
But these are really handy too:
There are also some useful commands for moving around a buffer by large steps:
This is a super detailed discussion of this stuff:
https://www.masteringemacs.org/article/effective-editing-movement
This topic belongs with the above topics, but in the earlier draft of this tutorial I originally had it after the explanations below about “More Powerful Emacs Concepts”. So it’s written with the assumption that you understand buffers, at least, and probably a few other concepts.
Someday, maybe, I’ll try to rewrite this without forward references to those concepts, or with brief explanations of those concepts, but for now, you may want to skip ahead and read that section, then come back here.
A couple of basic concepts; point, mark, region, kill, kill ring.
As I said above, point is what emacs docs call the cursor, or more precisely, whatever point – er, location – the cursor is at.
The mark is just that, a mark. The mark is sort of like an invisible cursor, it allows you to arbitrarily designate a second point in the text.
You usually can’t see the mark, though I’m sure you can customize emacs to make it visible. Sometimes the default settings of a given emacs will represent the mark as a highlighted space.
The key binding to set the mark is usually Ctrl-spacebar. When you set the mark, you’ll see the message “Mark set” in the bottom line of the emacs GUI window (aka frame).
You can also do Esc-x set-mark-command, which I got into the habit of using back when I had to deal with a lot of different, non-graphical versions of emacs that often had different key bindings for setting the mark.
The region is everything between your cursor and wherever the mark was last set. Everything. This is very useful for deleting or applying other commands to large areas of text, but can be dangerous if you forget where the mark last was.
The cursor point and mark define a region, which enables you to do things that we usually use mouse pointer to do today, i.e. select a section of text, what you might call a region (see what I did there?). The region is the section that is between the mark and the cursor’s current location (called, again, the point), and then run some command that affects that selection.
“Newline” isn’t an emacs term, it’s a general programming and software term, but you have to know about newline for the following to make sense, so:
“Line” in the context of text is a human concept. Programs have to have some way of telling when the end of a line is, and the way we do it is pretty much by using a special value or character. The “newline” character is one such value, but different operating systems use different values, like the “carriage return” value.
Sometime OSes use both, in other words a carriage return character followed a by newline character. This can be a bit of a pain in the ass when you move files between operating systems.
Two more terms you may see in programming contexts, and possibly in Emacs documentation is “EOL”, End Of Line, or “line separator”. These both refer to the same general concept as newline and carriage return, the special value (character) that programs use to indicate the end of one line and the beginning of another.
“Kill” is the emacs term for deleting a chunk of text.
Ctrl-k, the kill command, by default deletes all text from the cursor to the end of the line, but it doesn’t delete the newline character, i.e. it removes the text from the line but leaves the line there, taking up space on your screen and in your document.
To really delete the whole line you have to do Ctrl-k twice - once to delete the text, a second time to delete the newline character.
You can also kill an entire region with Ctrl-w. This is something you have to be careful about, or you could end up killing large regions you wanted to keep. Fortunately you have the kill ring.
Killed text all goes into the kill-ring, which is very much like the standard “undo” feature, except more explicit, and only about text that was deleted. There’s an actual emacs undo feature which is more fine grained, Ctrl-Shift-underscore (_)).
You can “yank” back all of the text that you last killed in one “contiguous” batch with Ctrl-y. This means that if you kill five lines in a row by pressing Ctrl-k ten times, Ctrl-y will yank all five lines back. But if you press Ctrl-k ten times, then use Ctrl-n to move somewhere else and do Ctrl-k four more limes, you’re only going to be able to yank back the last batch (the four lines) you killed.
You can do Ctrl-y repeatedly to copy the same chunk of text back into your buffer multiple times.
But the kill ring is bigger than just the last chunk of text, it contains up to the last 120 chunks of text that you killed. To get earlier chunks of text, you first Ctrl-y to yank, then Esc-y to swap that yanked chunk of text with the chunk text that was killed before it.
You can keep doing Esc-y to move further and further back in the kill ring, but as soon as you do anything but Esc-y, it breaks the chain, so to speak. If you try to do Esc-y after anything except Ctrl-y, eamcs runs the browse-kill-ring command, which opens a window onto the kill ring buffer.
You can navigate the kill ring buffer just like any other buffer, and you can set-mark and copy text jsut like any other buffer, but you can’t edit it. When you press a key, emacs will either tell you the buffer is read-only, in the message window at the bottom line, or if the key happens to match one of about a dozen command characters that are specific to the kill ring mode, it’ll do that command.
There’s a limit to how big the kill ring can get, the default is 120 chunks of text.
There’s only one kill ring, all the buffers share it, so that’s the normal way to copy or move text from one buffer to another – kill it in buffer 1, switch to buffer 2, yank it back.
If you don’t want to actually delete the text but you want to add it to the kill ring buffer without deleting it, you can just set the mark, move to the end of the area you want, and do Esc x copy-region to copy those lines into the kill ring, just as if you’d killed them.
There are commands for moving around the kill ring; check Emacs help or your Emacs manual.
There’s a lot to emacs, it’s basically a specialized lisp interpreter and GUI library designed for programming text editor. As a result, the rabbit hole can go as deep as you like. You’re welcome to go down that rabbit hole, but here are some of the major building block concepts of emacs, to give you the lay of the land.
In particular, I think that the emacs concepts of buffers and emacs windows (subdivisions of the main GUI window that emacs presents in) provide very powerful ways of interacting with the information you want to edit. I hope that by presenting these first, I can boost you into being much more productive with emacs.
Other concepts, besides the following, that you probably want to explore are regular expressions, emacs macros, emacs lisp (elisp) for defining more complex commands of your own (and, if you really drink the elisp koolaid, creating your own modes), and, of course, the hundreds (thousands?) of different emacs packages out there to provide additional capabilities.
Modes are a bit “out there”, but the concept underlies a lot of how emacs works, so it’s good to get a rough idea of them early, so:
You can think of a mode in Emacs in two senses, which are really two sides of the same coin.
The first sense is “a way of doing things”, and that’s what emacs modes were originally invented for. You want one set of features and user interface tweaks for editing regular text, you want another set for editing code. Those are two different modes.
The second sense is that hardcore Emacs users are usually programmers, and programmers usually like to solve problems by writing programs, and programs written to be run inside emacs to customize the emacs user experience are called “modes”.
Almost everything I described above is part of the emacs Fundamental mode, or of the Text mode.
Modes are specific to the buffer. You can open one buffer in Text editing mode, and another buffer in one of the code editing modes (the code editing modes are specific to different programming languages). Usually emacs automatically uses the mode that’s most likely according to the file name extension, i.e. if you open a file named “blah.txt” emacs will use Text mode, if you open a file named Blah.java emacs will use Java mode.
Modes can be major or minor.
Emacs can only use a single major mode per buffer, but can also have one or more minor modes per buffer.
The major mode is the main mode, the minor modes are modifications to the major mode.
Modes can and do have mode-specific key bindings.
Note: I originally wrote this document for a text-only display, I’ll have to get some screen shots and add them into this tutorial, someday.
Emacs is incredibly flexible and that includes the GUI layout, you can, and once you get used to emacs, you will, majorly rearrange how the emacs GUI looks, on the fly, just in the course of editing. I find that this is one of the most powerful aspects of emacs for day to day use, and most of it involves two emacs concepts, “Windows” and “Buffers”.
So you need to know some of the terms that Emacs uses for that.
Note, Emacs was also developed a lonnnnnnnnng time ago, so some of the terminology is a little different from modern common usage, but not that much. The main thing is how Emacs uses the terms “frame” and “window”, and “buffer” a major emacs term/concept that most modern GUI editors don’t have, or rather they just sort of conflate “window” and “buffer”.
“Key binding” is the term for how you define various control keys and such to invoke various commands.
It’s also useful to know about the emacs concepts “point”, “mark”, and “region” for regular editing purposes.
Frame in emacs is what modern GUIs call a window“. You usually don’t need to know about”frame“, what you need to know is that a”window" in emacs is what modern GUIs might call a “pane”, i.e. a subsection of the (modern language) window. In this tutorial I’ll probably never say “frame”, I’ll say “GUI window” to mean the window defined by your GUI, to distinguish between that and the subsections of your emacs GUI window, which is what emacs calls windows.
Windows are a subsection of the GUI window, what a modern GUI might call a “pane”. Window is actually a pretty good term for it, if you think of it in the original sense and now how software uses the term these days; an emacs window is a portal through which you view and interact with an emacs buffer. You can have more than one window open onto the same buffer.
A buffer is just what a programmer would think it means, a chunk of memory with content, usually editable, although not always. Windows are always shownig you a buffer, but buffers don’t always have windows, i.e. just because you don’t see the buffer, doesn’t mean it’s not there any more. More on buffers below.
A Window Bar: at the bottom of any given window is a solid colored bar with text in it, that’s called the Window Bar.
Command Buffer: beneath the bottom most window is one more line of space, called the Command Buffer. If you have to type commands with Esc x blah-blah
or similar, that happens in the Command Buffer.
Cursor, cinally, you should already know this one, because we still use it today; the “cursor” is the point at which, when you press a key, a letter appears. Hey, look at that word I just used, “point”. It turns out “point” is what emacs docs calls the cursor, or rather what emacs docs call the location the cursor is at. I’ll probably use “cursor” all the time in this tutorial. Sorry, force of habit. Also see “mark” and “region”, in the section titled “Cutting & Pasting”, below.
Key Binding: Emacs is insanely customizable. That includes defining which key sequences do what. This is generally referred as “key bindings”, which means roughly the same thing as “ht key” or “keyboard shortcut” or “control key sequence”. There’s a default set of key bindings for emacs in general, and many specialized emacs modes have their own sets of key bindings. I’m going to use the term “key binding” throughout this document so you learn to remember it, because that’s the term you need to use if you want to look into either what the key bindings are or how to change them.
Note: About that Meta
key: The Meta
key was an actual, physical key on the early computer keyboards that were in use when Emacs was first created. Most modern keyboards don’t have an actual Meta
key, so Emacs defaults to using “oress and release Esc
” as the Meta
key.
“Buffers” are the Emacs term for having multiple documents open at once. These days this feature is often available in modern word processing programs and the like, typically they’ll have a menu bar item named “Windows” to select which document you’re working with.
Strictly speaking, an emacs buffer doesn’t have to be a document or a file, it can contain anything, including content that isn’t associated with a file, a file that hasn’t been saved, an interactive directory listing, a terminal session (shell), etc.
Buffers are one of the generally nifty things that you can learn more about with the Emacs manual, and make it incredibly easy to work on many things at once. One thing to keep in mind is that the buffer sticks around until you kill it - out of sight does NOT mean out of mind.
You switch between buffers using:
Ctrl-x b
Switch to buffer: (default: blahblah) [enter]
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 Ctrl-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 command completion or ?
to see the other buffer names.
You can also open new buffers to edit text or whatever, simply by switching to a buffer that isn’t there.
Ctrl-x b
Switch to buffer: **newbuffername** [enter]
Generally I don’t close buffers, I just @quit, or save the file I was editing, or write the buffer as a file if I didn’t start it as a file, and then ignore the buffer until I’m ready to exit Emacs. But if you want to or need to make a buffer 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, by typing the name and using auto-complete. CAUTION: If the buffer isn’t associated with a saved file, Emacs will happily kill it without warning you.
If the buffer is associated with a saved file and there are unsaved changes, emacs will warn you and ask you to confirm, before killing it. You can check if there are unsaved changed by looking at the bottom, left corner of the Emacs window, which will show either -:**-
if there are unsaved changes or -:---
if there have been no changes since the last time the file was saved.
Multiple windows are like multiple buffers (in fact, they usually ARE multiple buffers), only they’re in the same frame. Right now, as I type this, I have about 30 lines for this document on the top half of my screen, and the bottom half contains two side-by-side windows containing sessions where I’m connecting to a type of chat system called IRC.
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:
After updating the above and converting it to HTML and putting it online, I found the following, which I wrote a while back. I include it here, but bear in mind it it’s from 10+ years ago and may be out of date. Also, it may duplicate some of the above.
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”, 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:
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.
emacswiki.org is a really good source for all kinds of info.
The #emacs channel on irc.libera.chat also has some very knowledgable (and often even helpful :-) people.
Multiple buffers and 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.
In a nutshell, you can open multiple files, each file is its own buffer. Or you can have buffers that aren’t associated with files yet, just change to a new buffer and specify a buffer name that isn’t in use.
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.
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.
You switch between buffers using “Ctrl-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 “Ctrl-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 (tab or ?) 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 “Ctrl-x b”
Switch to buffer: newbuffername [enter]
#### Killing Buffers
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, I don’t need, using the buffer-editor (Esc-x buffer-editor[enter]). 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 editor 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. The only way to open multiple shell buffers is to use “Esc-x rename-buffer[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.
While I’m here, if you’re a programmer and you’re using git, which is likely if you’re using emacs, then the git pager is kind of annoying when you use the shell buffer. To have git skip using the pager and just dump all the text to screen, so you can just use emacs commands to navigate around it, set the environment variable GIT_PAGER="".
$ export GIT_PAGER=""
I find desktop-save-mode really, really handy. In a nutshell, it lets you shut down emacs and start it up later - and it restores all of your buffers!
Go learn how to set it up and use it.
Emasc predates GUIs, so while in your GUI (windows, mac) the window is usually synonymous with the entire app, 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 “Ctrl-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 ove 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 '(<active> [<inactive>]))
(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):
Ctrl-x 5 2
In whichever frame you want to be the background frame, turn off translucency:
Ctrl-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. The last mode you loaded will be the key binding you get. I think there are conventions that keep most minor mode keybindings from conflicting with major mode keybindings.
There are modes that are entire little (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 “Ctrl-h m”.
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 the right indent level, because you’ve probably made a mistake.
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.html
These 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.
Similar to the more robust undo/redo commands, more multilevel cut/paste commands, both of which used to be rare and are now a lot more common.
In emacs, when you cut (Ctrl-k for “kill”) some text, it goes into the “kill ring”.
You can “yank” back out (Ctrl-y) the text of your last kill.
You can also yank back out earlier “kills”. Immediately after a “yank”, without typing any other key, doing “Esc-y”. Emacs will swap out the text you just yanked back in, and give you 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 rotates 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, "Ctrl-_", which to write it a bit more clearly, is control-underscore, or 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 Ctrl-g) and then you undo again; essentially you “undo the undo”.
This is definitely in the category of minutea, 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. 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 ot a sentence or paragraph (usually a 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: Ctrl-g, aka keyboard quit, to interrupt things if you’re in the middle of a command.
Control/alt keys versus the arrow keys and pageup/pagedown keys is debatable. I’ve been using emacs for over 25 years (starting on keyboards that didn’t have all these arrows and etc :-), so it’s hard for me to objective evaluate it, but I certainly 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 Ctrl-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
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 a quick summary:
Press ctrl-s or ctrl-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 document as you type.
If, for example, your cursor is on the period at the end of the previous sentence and you type ctrl-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. If you wanted the previous occcurrence, press ctrl-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.
Ctrl-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
Note: Interactive search is a bit odd and I’ve never really gotten super used to it. Basically, it’s search that stays active and you use some control keys to determine what happens. For a simple example, let’s say I use Ctrl-r right here and then start to type the word “search”. When I type “s”, the cursor will jump backwards to the first match. If we start from just after “Ctrl-r” in that sentence, that’s the “s” in “use”. If I then press Ctrl-r again, the cursor will jump further backward to the next “s”. Or I can type additional letters to clarify the search, like “se”. That expands the match to include the “se” in use. If I press Ctrl-r and the cursor jumps backward to the match before that one. If I decide I messed up and I actually wanted that first search, I can press Ctrl-s to make the cursor jump forward to the first match after the cursor.
As I said, interactive search is powerful but I’ve never really gotten used to it. I should, one of these days.
Really it’s control-shift-dash (the dashkey, 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 Ctrl-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 Ctrl-k again to kill the newline.
(* kill-buffer is also available as “Ctrl-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.
dired-mode: By opening a directory instead of a file, you can use dired-mode, essentially it is a file browsing mode. Each sub directory 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, then did some regular expression search and replaces and some macros to convert it to markdown mode. It’d be nice if there ws a markdown-outline mode, there might even be one.
I sometimes find outline mode really useful for editing large documents.
In outline mode, instead of using one or more hash characters (#) at the beginning of the line to indicate a header, you use asterisks (*).
You can then collapse the text and just view the headers, navigate around to the header you want, then expand the text to edit.
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.
This just loads it, which makes the functions available at the “Esc-x” prompt, but you probably still have to invoke the function or mode directly.