Emacs is a very powerful and flexible text editor.
This is a very quick walk-through of essential emacs commands, based on my own experience of the commands I use most frequently.
I’m giving the key shortcuts, because those are the ones I use. But running in a GUI, you can use mouse navigation for such things as switching windows, and there are menu items for many of the commands.
I thought that this list would be short, but once I started writing I thinking about new stuff that I should include. Eventually I had to cut it down to what I really think are the basics.
I won’t spend any time on the flexibility of emacs, but will mention it in the section titled “Commands and key bindings”
Keypress sequences will be represented like these examples `C-x C-f’ (pronounced “control ex control eff”), `M-w’ (pronounced “meta double you”).
- The sequence is delimited by a single back quote ` at the start, and a single quote ‘ at the end
- Spaces in the command sequence are just separators. If pressing the space bar is part of the command sequence, it will be represented by SPC, e.g. `C-SPC’
- `C-‘ means: hold the Ctrl key pressed while pressing and releasing the key connected to C with the hyphen
- `M-‘ means: hold the “meta” key pressed while pressing and releasing the key connected to C with the hyphen. On PC keyboards the “meta” key is most commonly the “Alt” key
- `RET’ in a command sequence means pressing the Return (or Enter) key
- `TAB’ means press the tabulator key
Commands and key bindings
The actual commands in emacs are all lisp functions. The keyboard key combinations you press to run the command, are known as the “key binding” of the command.
If a command’s regular key binding has been masked for some reason, you can run the command with `M-x command RET’ where “command” is the command’s lisp function name.
Emacs allows replacing the key bindings for all commands with your own, and for a new emacs user it may be tempting to replace the default emacs key bindings with something more familiar. However, I recommend against replacing the default key bindings, because:
- different emacs modes often have a command set patterned on the base command set, and they are easier to remember for that reason
- if you come to a emacs installation that does not have your customizations you may not be able to use it
To quit emacs, do `C-x C-c’.
If running emacs with a GUI, you can also go to the File menu, and select quit.
Note that `C-x C-c’ is quite easy to hit by accident. It’s possible to make gnus query for whether it should quit or not, by customizing the lisp variable confirm-kill-emacs.
To modify the variable, do `C-h v confirm-kill-emacs RET’. The editing screen will split. Do `C-x o’ to move to the lower half of the edit area, move the underlined word “customize” and press RET. In the form that appears select the “Value Menu” button, and then choose “Ask with yes-or-no-p”. Then select “Set for current session”, then select “Save for future sessions”, then select “Exit”.
The Help menu have lots of stuff. There’s an online tutorial that is worth following. All help commands are available through `C-h’ followed by a key.
I use the following help commands on a regular basis:
- `C-h i’ to get the online manual
- `C-h b’ to find the current key bindings
- `C-h f’ to find the documentation for a lisp function (eg. a command),
- `C-h v’ to find the documentation for a lisp variable
Note that for both `C-h f’ and `C-h v’ TAB and SPC can be used to autp expand the function or variable name.
Also note that many variable documentations have the text “This variable can be customized” in it. Move the cursor to the word “customized” and press RET. This will take you to a form where you can change the variable and persist the changes.
Loading and saving files
When starting emacs in a GUI environment, you will see:
- A menu bar at the top
- A toolbar below the menu bar
- A big window with a blurb-looking text
- A line with a different background color and some text and numbers
- A white area at the bottom (known as “the mini buffer”). This is where you will be prompted for file names, and give search strings
To load a file into emacs, press the keys `C-x C-f’.
The minibuffer will present a prompt for the file name. It will suggest a path based on the current working directory. You can edit that path whereever you want to go, and the possibilities for the paths can be expanded by pressing SPC or TAB. SPC means “expand to the first separator character” (e.g. “/” or “.”). TAB means “expand as far as possible”.
To save the file type `C-x C-s’.
To save to a new file name, type `C-x C-w’.
All of these commands are available under the File menu, when running emacs in a GUI.
You can edit multiple files into emacs. To move between files, either use `C-x C-b’ to get a list of buffers, or `C-x b’ (no ctrl key for the b), and use TAB or SPC to expand the name of the loaded file you want to move to.
The `C-x C-b’ command will split the emacs window in two, vertically. Use `C-x o’ to jump to the lower half, and then arrow keys to navigate to the file you want to go to, and then `C-x 1′ to let it take the entire editing area.
You can also cycle through the loaded files with the commands `C-x C-right’ and `C-x C-left’ (“right” and “left” meaning the right and left arrow keys).
All of these commands are available in the Buffers menu
Editing multiple files
You can load as many files as you like (well… there’s a limit, but I’ve never reached it) into emacs at the same time.
To switch between files, use `C-x b’ (Note: no ctrl key for the `b’) and write the name of the file you want to go to (not the full path, just the local file name). You can use TAB and SPC to auto-expand the file name.
To see all files loaded in emacs give the command `C-x C-b’ (this time use ctrl for the b as well). The text editing window will split in two with the loaded files listed in the lower half of the screen.
To get to a loaded file, use `C-x o’ to switch the cursor over to the lower half, then cursor to the file you want to look at, and press RET.
Now the lower half will show the file you selected, but the top half still show again.txt. To make the new file use all of the editing area, press `C-x 1′ (“control ex one”).
There are many similar commands to split and close part of the editing area. I won’t get into them here. The manual has more.
Navigating in emacs
The normal arrow keys work as expected. So does page up and page down. The Home key will take you to the start of the line, and the End key will take you to the end of the line.
In addition you can navigate with Ctrl keys: `C-p’ is the same as arrow-up (p for “previous”), `C-n’ is the same as arrow-down (d for “down”), `C-b’ is the same as arrow-left (b for “back”), and `C-f’ is the same as arrow-right (f for “forward”).
Two of these commands have “meta” siblings: `M-f’ which forwards the cursor over the next word, and `M-b’ which takes the cursor over the previous word.
In addition there are `C-M-f’ (“control meta eff”) which takes the cursor over the next clause. Whatever the clause is depends on the type of file in the current buffer. In an XML buffer that would be an element. There’s also `C-M-b’ which moves a clause backwards.
The page-up and page-down keys also have ctrl/meta keys. `C-v’ is page down, and `M-v’ is page up.
Ctrl key equivalents for Home and End are `C-a’ and ‘C-e’.
Searching and replacing
To start searching in the current file, give the command `C-s’, then start typing the word you are searching for. The search is incremental so the more you type the more it will match. The cursor will move to the match.
When you have found a match, you can:
- Press `C-s’ to go to the next match
- Press `RET’ to stop the search at the current match
- Press `ESC’ to abort the search and return the cursor to where you were before starting the search
If you do `C-M-s’ instead of `C-s’ (ie. hold down both the ctrl and Alt keys, when pressing and releasing s), the incremental search will be a regular expression search, instead of just a plain search.
Another powerful search method is occur. Type `M-s o’ and the minibuffer will prompt you for a regular expression. After giving a regular expression and typing RET, the editing area will split in half, the lower half showing all matches. To go to a match do `C-x o’ to move to the lower half, use the up and down arrow keys to move to a match, and then press RET.
To search and replace, use the command `M-%’. You will be prompted in the minibuffer for search and replace texts. When being prompted, you can use the up and down arrow keys to select earlier search and replace texts.
The command `C-M-%’ is a regular expression search and replace, and adds complexity and power to the search and replace, but otherwise behaves the same as the plain text search and replace.
Cutting and pasting
To mark the area that is to be copied or pasted, press `C-SPC’ at the start of the area, and then move the cursor the other end. The area is now selected (in a GUI, it will show up as selected).
If you want to copy the marked region, use `M-w’, if you want to cut the marked region (“kill the region” in emacs terminology) use `C-w’.
Then move the cursor to where you want to paste it (“yank it”, in emacs terminology), and press `C-y’.
If you immediately (ie. without moving the cursor) press `M-y’ the yanked (ie. “pasted”) region will be replaced with the previously copied or cut region. By pressing `M-y’ again you will get the copy/paste preceeding it. You can continue pressing `M-y’ until you’re back to the last copied/cut region, and then continue past that again for a new cycle.
This is what emacs calls “cycling the kill ring”.
A very useful way of deleting lines is using `C-k’. This command will kill the rest of the line from the cursor point. If you continue to press `C-k’ the killed lines will be added to the most recent entry in the kill ring, rather than appear as new entries.
If you want to delete the next 50 lines you can do it with `C-u 5 0 C-k’.
`C-u’ is known as “the universal argument prefix”. It can be applied to many emacs commands, typically to give them a numerical argument.
Useful variables to set
There are some variables that are useful to set to values other than the default.
For all of them, do `C-h v variable-name RET’ then select the underlined word “customize” in the variable’s documentation, select the “Value Menu” in the form that appears, and set the appropriate value. Then select “Set for current session”, then select “Save for future sessions”, then select “Exit”.
The variables are:
- `confirm-kill-emacs’ set to `yes-or-no-p’ (emacs will query yes/no on exit)
- `split-width-threshold’ set to `nil’ (emacs will split windows horizontally)
- `x-select-enable-clipboard’ set to `t’ (copy/paste will work from emacs to other programs)