Emacs/How to Use Emacs

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The learning debt model[edit | edit source]

Many emacs tutorials start with a long list of useful commands and keybindings for your to remember. Useful as this is, it can be slightly overwhelming, and distracting. Moreover, other tutorials probably already do a reasonable job.

An alternative way of learning things can be going into a form of "learning debt", learning the minimal amount required to easily find out the things you need to know when you want to do them. And then *playing* with the tools you want to learn. At the very least, this way of doing this can be very compelling, and a complete tutorial is often rendered far more interesting once you have suffered a little.

A mental model of emacs[edit | edit source]

We shall start of with a mental model of how emacs works, befitting its extensible nature. This model together with some emacs's commands for documentation can help you start using without having to learn that much.

  • Most of the work you do in emacs is via a *buffer*, an interactively editable representation of a file.
  • Everything you do in emacs is function call, even things like pressing individual keys.
  • There is a keymap that maps (or binds) keys, or sequences of keys you press, to function calls.
  • Some of these function calls might interactively ask for input. This is done by popping up a prompt (the mini-prompt) at the bottom of the screen.

Finding out how to do things in emacs[edit | edit source]

Emacs is designed to be self-documenting. Emacs's fairly consistent model, combined with functions for finding out about the internal state of emacs makes it quite easy to find out what emacs can do and how it does it.

Looking up functions[edit | edit source]

Everything you do in emacs is via a function call. These functions tend to have descriptive names, and good documentation. So by searching for functions by name you can find out how to do something.

You can search functions with the `describe-function` command. By default this function is bound to *C-h f*. That is you press down the control key, and before releasing it press h, then you release the control key and press *f*.

This pops up a prompt at the bottom of the screen asking you for a function name. At that time you can press *<TAB>* to get a list of functions starting with what you've typed so far.

Exercise[edit | edit source]

Example[edit | edit source]

Let's suppose you want to work out how to move the cursor left. If you press "C-h f l e f t <TAB> <TAB>" you see that there two functions that start with *left*, *left-char* and *left-word*. If you finish typing "left-char" and press "<ENTER>" Emacs will show you documentation for the *left-char* command.

This will tell you that

  • The left-char command moves the cursor to the left.
  • Unsurprisingly, it is bound to the left key.

Look up some functions[edit | edit source]

Look up some function definitions, and call some functions. Use the "C-h f" key-binding to look up as many different functions as you think of until you get bored. See if you can recognise patterns in function names, or the keybindings they have. In particular you are probably interested in how to open files, open buffers, change buffers, close buffers and exit emacs.

Looking up keys[edit | edit source]

Looking up functions can be a good way of finding out how to do this, but it can be problematic if you can't seem to find the name of the function you are looking for. Often what you are after are useful functions together with their keybindings. One suspects that many readers have already found their own list from among the various tutorials, cheat sheets and manuals for emacs that exist on the internet.

Emacs itself can be made to give you such a list. One such useful list is all the functions that are currently bound to some key. You can get at this using the describe-bindings function bound to "C-h f".

Another plausible approach to learning how to use a programming tool is to press keys at random until something interesting happens, and then look up what exactly the key press did. You can use the describe-key function bound to "C-h k" to do this.

Alternative sources of documentation[edit | edit source]

Once you have started playing emacs, and built up a sufficient store of frustration. You might want to interact with some reference or tutorial documentation, the function help-with-tutorial "C-h t" is one place to start.