Sunday, March 28, 2010

Learning emacs - Getting Help

Emacs is a highly extensible editor - so extensible in fact that it is whatever you want to make it. There are some compelling reasons to get to know emacs, whether you're a die hard unix vi/vim maven or just windows coder. These include: learning lisp, learning clojure (a very young and promising java-based lisp variant with an emphasis on functionaal programming and concurrency) as well as editing xml and xhtml using the nxml and nxhtml modes.

The more comfortable you are with the lisp way of programming and seeing the world, the more likely you'll make emacs your editor of choice or at least more easily appreciate how emacs works and be comfortable with it.

I'm learning both emacs and lisp (in one form or another) so take what I say in this context. Here's a short article on how to get help in emacs.

Getting help in Emacs

  • you should do the tutorial that emacs provides you when you start; this will teach you basic key combinations and basic concepts; you can access this tutorial by typing C-h t .
  • Take the guided tour at http://www.gnu.org/software/emacs/tour/
  • terminology:
    • a "point" is your cursor; there are commands in emacs such as "find file at oint" or "browser url at point" for instance
    • a "region" is an area of the buffer you have highlighted with the cursor; you can do it by holding the mouse-1 button down and dragging; you can also do it from the keyboard using C-SPACE and cursor movement keys
    • a "frame" is what most people refer to as a window; its the thing that has a title bar and quite often a maximize/minimize button
    • a "buffer" is the thing your cursor is inside of; a file you open to edit is represented by a buffer; there are also special buffers that don't represent files on your file system; for instance: emacs logs its messages to a special buffer called *messages*; there is a buffer for listing all buffers open in emacs which you can get using C-x C-b
    • the "mini buffer" is like a small permanent section at the bottom of the emacs frame that shows you what key combinations you are pressing and returns status and help messages; it's the main feedback area for when you run commands
    • a "window" is the thing a buffer sits inside of; you can view more than one window (and its corresponding buffer) in the current frame, sometimes referred to as "splitting windows"
    • You can practise some of the keys that modify windows and buffers (frames are things you can worry about later):
      C-x 2 horizontally splits your current window into 2 windows;
      C-x 3 vertically splits current window into 2 windows
      C-x 1 gets rid of any other split windows and keeps the one your cursor is in
      C-x 0 if you have at least 2 split windows, this hides (unspits) the window you are in and puts your cursor into another one
      Often with message or error or output buffers - buffers that are created or shown as a result of some action you have just performed - you can close or hide them with the 'q' key.
      C-x 4 0 kills the buffer and hides (unsplits) the window (you might find it easier to type 'q' in some situations although this might not kill the buffer)
      C-x o to move between split windows
  • M-x is the basic prefix to access what emacs refers to as "interactive" commands; everything emacs does can be found this way; not convinced, then type: C-h k C-f to look up what C-f ("move cursor forward" command) uses; there are other "commands" or "functions" which are not interactive and which you cannot directly access via M-x; basically emacs can either expose a command directly to a user via M-x (interactive) or or not; this makes sense, if you're building some new nifty functionality into emacs you will likely end up writing some lower level routines which support higher level interactive ones and which you wouldn't want the user to run by themselves via M-x.
    More to the point, the M-x command leads us into the heart of emacs extensibility; emacs is a bunch of commands and functions written in a variant of lisp called elisp*. If you're not familiar with lisp you might wonder why all the function names are hyphenated and what's really going on and how on earth you might script them (something I might blog about later and which you can read about easily enough on other blogs and using the Gnu manuals (below)). As mentioned above, I think it's worth understanding lisp and elisp a little to grok what's going on (I don't mean you have to straight away, but over time) which leads to the next point...
    [* some of the functionality is written in C but exposed to elisp]
  • There are 3 big official manuals you should know about:
  • getting help; emacs has all sorts of ways to provide you to search and explore what it can do; it just requires you to know how to access it!
    • C-h C-h loads a general help and other C-h * combinations you can use
    • C-h m documents the mode of the current buffer you are in; don't forget to use C-M-v to scroll the help buffer whilst keeping your point in the original buffer; if you want to search in this help buffer, C-x o will move your point into it; type q to hide the help buffer and its window
    • C-h k describes what a given key combination does - what function it calls
    • C-h f describes a function
    • C-h b lists current key bindings
    • C-h w tells you if a function has a key combination
    • M-x apropos does a search on commands in emacs; try "M-x apropos buffer" to look up all commands that have the word buffer in them; C-x o into this buffer and do a search on "kill" and "rename" (C-s kill etc ...); apropos shows more than just interactive functions so some of what you see may not be accessible via M-x