Emacs is a text editor that can also read you mail, browse the web, compile your code...

Further Resources

Don't overlook the Help menu. There's a Tutorial, a FAQ, a extensive manual, and comprehensive help for every command.

This is my favorite source of Emacs documentation

Way back in the day, grad student Neal Turner had a semi-regular emacs tricks mailing list. This was before my time, but before his webpage was taken down I grabbed all the mailings. Feel free to make them prettier or to break them up into more logical categories.

grep-find

Type M-x grep-find, then type some regular expression, and watch the magic unfold before your eyes.

Super cool history completion

I often interact with programs running under Emacs, such as IPython, Lisp shells, debuggers, or Unix shells. For interactive programming environments, Emacs has some killer features like paren highlighting that make this worthwhile. You can also get it to do killer history completion. Say you type:

foo = ...long expression...
...lots of commands...

and then you want to re-execute the command starting with foo. You can either hit up arrow a hundred times, or else put the following in your .emacs file and type:

foo = <up arrow>

and emacs will find the command starting with foo and complete it for you. It's sort of like tab-completion for commands, except cooler, b/c if you type:

foo = ...one long expression...
...lots of commands...
foo = ...another long expression...
...lots of commands...

Then, typing foo = <up arrow> once will get you the most recent command starting with foo and hitting <up arrow> a second time will get you the next most recent matching command, skipping over all the commands in between.

Ok, here's the code:

;;; If the cursor is after the command prompt, make <up> and <down> do
;;; command history matching rather than just sequentially pulling
;;; commands from the history.  If the cursor is before the command
;;; prompt, make <up> and <down> do the normal cursor-movement stuff.
;;; Note that this means that you'll have to hit <left arrow> to get
;;; the cursor off of the command line so that <up> and <down> start
;;; moving the cursor instead of fooling around with the command history.
(defun my-comint-history-up () 
  (interactive)
  (if (not (comint-after-pmark-p))
      ;; if we're before the prompt, move around
      (previous-line 1)      
    ;; if we're after the prompt, do history matching
    (comint-previous-matching-input-from-input 1)
    ;; fool comint into thinking that the last command was 
    ;; comint-... b/c of the way it remembers the search string
    (setq this-command 'comint-previous-matching-input-from-input)))

(defun my-comint-history-down () 
  (interactive)
  (if (not (comint-after-pmark-p))
      (next-line 1)      
    (comint-next-matching-input-from-input 1)   
    (setq this-command 'comint-next-matching-input-from-input)))

(defun my-comint-history-keymaps () 
  (local-set-key [up] 'my-comint-history-up)
  (local-set-key "\C-p" 'my-comint-history-up)
  (local-set-key [down] 'my-comint-history-down)
  (local-set-key "\C-n" 'my-comint-history-down))

(add-hook 'comint-mode-hook 'my-comint-history-keymaps t)

Tramp

Tramp (Transparent Remote File Access) lets you edit remote files as if they were local. It's included in new versions of Emacs, but for old versions you might have to install it.

For info about Tramp on Windows, see Windows.

Swapping Caps Lock and Control

If you make heavy use of Emacs, it's imperetive that you swap the caps lock and control keys to prevent your pinky from turning into a useless, shriveled, RSI-ridden hunk of flesh. Who put the caps-lock key right next to the "a" key, anyway? What's it doing in such a prominent spot? Do you ever use it? I don't. But I use control all the time. Clearly, time for a change.

Under Linux, this is accomplished via xmodmap. Look at the man pages. Putting the following in .Xmodmap works for me.

remove lock = Caps_Lock
remove control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add lock = Caps_Lock
add control = Control_L

Under OSX 10.4, this can be changed via: System Preferences -- Keyboard -- Keyboard -- Modifier Keys.

Old (kludgy) solutions for previous versions of OS X included: DoubleCommand, uControl, this, or this

Compilation Mode

For some reason, compilation-mode stopped scrolling with the output of the compilation command. It took me a while to notice this since it's not a big deal, but I had a vague memory of better times, when I didn't have to hit C-x o end to see whether or not there were any errors. Fix this with: setq compilation-scroll-output t).

Forms Mode

Use Forms mode to maintain simple flat-text databases. I use this to maintain the captions for my Photo Album, from which the html pages are generated with a Perl script.

Rectangle Mode

Want to select a column of characters in a text file, then paste the column somewhere else? Easy, use rectangle mode. Type M-x apropos-command (ret) rectangle (ret) to see the options, and pay particular attention to kill-rectangle and yank-rectangle.

Html Helper Mode

This may be getting out of hand since Emacs can do anything, but I also like Html Helper Mode. Available here and here, but the first seems to be more recently maintained.

Advice

The ability to give Emacs "advice" can be a simple way to modify the behavior of some command. For example, this advice prevents the "switch to buffer" command from creating a new buffer if you mistype the buffer name:

(defadvice switch-to-buffer (before existing-buffer activate compile)
  "When interactive, switch to existing buffers only, unless giving a
prefix argument."
  (interactive
   (list (read-buffer "Switch to buffer: " (other-buffer)
                      (null current-prefix-arg)))))

Also, you give prefix arguments by typing C-u before the command. So, to create a new buffer would be (I believe) C-u C-x b <buffer name>

Eshell

Eshell can be useful. It knows about standard unix commands and parses their output in useful ways. Start Eshell with M-x eshell <ret>. Marvel at its cleverness by typing grep foo *. Then you can switch to the new buffer and hit return on each of the results to pull the file into a buffer. Or you can hit C-x ` to look at each of the results in turn. Note that if you're writing code, this last bit can also show you each of the compilation errors in turn.

Dabbrevs

Mike showed me this thing today. Try to start typing an identifier already present in the buffer you're working on, hit M-/ and see what happens. Looks pretty useful.

Answers/Software/Emacs (last edited 2007-09-12 22:56:04 by GregNovak)