Cheat productively in Emacs 0

Posted by sjs
on Tuesday, August 21

By now you may have heard about cheat, the command line cheat sheet collection that’s completely open to editing, wiki style. A couple of weeks ago I posted cheat.el which allows one to cheat from within Emacs. There’s an update. However, before I get to cheat.el there’s a small detour.

Cheat is not just about Ruby! A few examples of cheats available are:

  • bash and zsh
  • $EDITOR (if you happen to like e, TextMate, vi, emacs, RadRails, ...)
  • GNU screen
  • Version control (darcs, svn, git)
  • Firebug
  • Markdown and Textile
  • Oracle and MySQL
  • Regular expressions
  • and of course Ruby, Rails, Capistrano, etc.

As of today, Aug-21 2007, the count is at 166 cheat sheets so there’s probably something there that you’ll want to look up from the command line or Emacs sometime. That’s enough stroking cheat’s ego, but there seems to be a notion that cheat is only for Ruby stuff and that’s really not the case.

So what’s new in this version of cheat.el? Completion! The only thing that bothered me about cheating in Emacs was the lack of completion. It now has completion, thus it is now perfect. :) In all likeliness this won’t be the last release, but I can’t really foresee adding anything else to it in the near future. Enjoy!

Download it now: cheat.el

For any newcomers, just drop this into ~/.emacs.d, ~/.elisp, or any directory in your load-path and then (require 'cheat). For more info check the original article for a rundown on the cheat commands.

Cheat from Emacs 5

Posted by sjs
on Friday, August 10

Update: I had inadvertently used string-join, a function provided by something in my ~/.emacs.d. The script has been updated to work with a vanilla Emacs (23, but should work with 22 as well).

Update #2 [2007.08.10]: Editing cheats and diffs have been implemented.

Update #3 [2007.08.21]: I added completion to cheat.el. The file linked on this page is still the latest version.

We all know and love cheat. Now you can cheat without leaving Emacs (and without using a shell in Emacs).

Just save cheat.el in ~/.emacs.d and then (require 'cheat) in your ~/.emacs. I also bind C-z C-c to cheat, you may want to do something similar.

You can’t do everything you can do with cheat on the command line yet, and for most of the commands the cheat command itself is used. Now you can do everything the command line client does from within Emacs, though you may need to revert to using cheat-command (described below).

Here’s the rundown:

Any time you enter a cheat name there are both completion and a cheat-specific history available. Unless you are adding a new cheat. In that case you should use a new, unique name (duh).

  • cheat – Lookup a cheat sheet interactively (cheat <name>)
  • cheat-sheets – List all cheat sheets (cheat sheets)
  • cheat-recent – List recently added cheat sheets (cheat recent)
  • cheat-versions – List versions of a cheat sheet interactively (cheat <name> --versions)
  • cheat-clear-cache – Clear all cached sheets.
  • cheat-add-current-buffer – Add a new cheat using the specified name and the contents of the current buffer as the body. (cheat <name> --add)
  • cheat-edit – Retrieve a fresh copy of the named cheat and display the body in a buffer for editing.
  • cheat-save-current-buffer – Save the current cheat buffer, which should be named *cheat-<name>*.
  • cheat-diff – Show the diff between the current version and the given version of the named cheat. If the version given is of the form m:n then show the diff between versions m and n. (cheat <name> --diff <version>)
  • cheat-command – Pass any arguments you want to cheat interactively.

(Added) I may add support for --diff and --edit in the future.

Please do send me your patches so everyone can benefit from them.

Emacs tips 0

Posted by sjs
on Thursday, August 09

This is my collection of Emacs tips or commands that I keep around for quick reference. Perhaps someone else will find something they like here.

Download the text (2007.08.18): ↓ emacs-tips.txt

Emacs tips
==========

Time-stamp: <2007-08-18 17:40:08 sjs>

Inside an incremental search:
  * C-w pulls in the word under point
  * C-y pulls in the rest of the line from point
  * M-y yanks the last text from the kill ring

Keybinding   Function                                Description
----------   --------                                -----------

==Help
C-h c        describe-key-briefly                    Display the name of the function bound to the given key sequence.
C-h k        describe-key                            Display documentation for the function bound to the given key sequence.
C-h w        where-is                                Display key sequences bound to the given command name.
C-h l        view-lossage                            Display the last 100 keystrokes.
C-h m        describe-mode                           Display documentation on the current major mode.

** C-h after any prefix key will display all available shortcuts under that prefix. **

==General
             word-search-{forward,backward}          Search for sequences of words across newlines & puncuation.
C-x $        set-selective-display                   Hide lines indented greater than ARG levels (folding).
             follow-mode                             Scroll windows of the same buffer in harmony.
C-x =        what-cursor-position                    Display details about what's under the cursor.
C-x z        repeat                                  Repeat the most recently executed command.
C-x ESC ESC  repeat-complex-command                  Edit and re-evaluate the last complex command (or ARGth command).
             list-history-command                    List commands available for repitition (via repeat-complex-command).

==Mark and point
M-r          move-to-window-line                     nil ARG: Move to the middle of the window.
                                                     ARG > 0: Move point to the ARGth line.
                                                     ARG < 0: Move point to the |ARG|th line from the bottom.
C-x h        mark-whole-buffer                       Mark the entire buffer.
C-x C-x      exchange-mark-and-point                 Exchange mark and point, reactivating mark if required.
C-SPC        set-mark-command                        Set and activate the mark, C-u C-SPC pops the mark ring.
C-x C-SPC    pop-global-mark

A prefix of 0 (C-u 0) has a special meaning for the transpose family of functions. Since it would otherwise be meaningless
it transposes the chars/words/sexps/lines around the region specified by point and mark. It's best to try this one out to
see exactly how it works!

C-t          transpose-chars
M-t          transpose-words
C-M-t        transpose-sexps
C-x C-t      transpose-lines

** Mark and point can be treated as a rectangle, and are by certain functions. (see next section) **

==Rectangles
C-x r c      clear-rectangle                         Replace text in the rectangle with spaces, blanking out the region destructively.
C-x r d      delete-rectangle                        Delete text in the rectangle described by mark and point.
C-x r o      open-rectangle                          Shift text in the rectangle right, blanking out the region non-destructively.
C-x r t      string-rectangle                        Replace rectangle contents with the given string.
C-x r y      yank-rectangle                          Yank back the last killed rectangle contents, top-left at point.

==Whitespace / cleaning up
C-x C-o      delete-blank-lines                      Delete all but one of many consecutive blank lines.
             fixup-whitespace                        Clean up space between objects around point.
M-\          delete-horizontal-space                 Delete all spaces & tabs around point.
M-SPC        just-one-space                          Delete all spaces & tabs around point except for one (or N).
             canonically-space-region                Remove extra spaces between words in region.
             delete-trailing-whitespace              Delete all trailing whitespace in the current buffer.

==Shell mode
C-c C-n      comint-next-prompt                      Move point to the next prompt.
C-c C-p      comint-previous-prompt                  Move point to the previous prompt.
C-c RET      comint-copy-old-input                   Copy the command input at point to the end of the buffer.

Variable           Description
--------           -----------

track-eol          If set next-line and previous-line will move to EOL if moving from EOL.

Slime mode
==========

Keybinding   Function                                Description
----------   --------                                -----------

==Compilation
C-c C-k      slime-compile-and-load-file             Compile and load the current buffer.
C-c M-k      slime-compile-file                      Compile (but don't load) the current buffer.
C-c C-c      slime-compile-defun                     Compile the top-level form at or around point.
M-n          slime-next-note                         Move to the next compiler note.
M-p          slime-previous-note                     Move to the previous compiler note.
C-c M-c      slime-remove-notes                      Remove all annotations from the buffer.

==REPL
C-Return     slime-repl-closing-return               Close all open parentheses and evaluate the current line.

==Navigation
M-.          slime-edit-definition                   Go to the definition of the symbol at point.
M-,          slime-pop-find-definition-stack         Go back from the definition found with slime-edit-definition.

==Evaluation
C-M-x        slime-eval-defun                        Evaluate the top-level form.
                                                         (ARG inserts result into buffer)
C-x C-e      slime-eval-last-expression              Evaluate the last expression before point.
                                                         (ARG inserts result info buffer)
C-c C-p      slime-pprint-eval-last-expression       Evaluate the expression before point and pretty-print the result.
C-c C-r      slime-eval-region                       Evaluate the region.
C-c :        slime-interactive-eval                  Evaluate an expression read from the minibuffer.

             slime-scratch                           Create a buffer like Emacs' *scratch*, eval expressions with C-j.

==Programming
C-c M-i      slime-fuzzy-complete-symbol             Presents a list of likely completions to choose from for an abbreviation
                                                     at point.
C-c C-s      slime-insert-arglist                    Look up and insert the arg list for the function at point.
C-c C-m      slime-macroexpand-1                     Macroexpand the expression at point once. Use a prefix to call macroexpand
                                                     instead of macroexpand-1.
C-c C-t      slime-toggle-trace-fdefinition          Toggle tracing of the function at point.
C-c M-d      slime-disassemble-symbol                Dissassemble the function definition of the symbol at point.

==Debugging

[TODO clean this up, this is copied from the Emacs wiki]

The following commands invoke restarts:

    * 0 .. 9 - Invoke a restart by number.
    * q - Throw to the top-level.
    * a - Invoke the ABORT restart.
    * c - Invoke the CONTINUE restart.

There are also commands for poking around in the frame at point:

    * v - Show the current frame's source expression in a buffer.
    * t - Toggle verbose display of the frame at point. The verbose version includes all local variables and CATCH tags.
    * l - Show local variables.
    * e - Evaluate an expression from inside a frame.
    * d - Evaluate an expression and display the result in an extra buffer.
    * i - Evaluate an expression and inspect the result.
    * : - Evaluate an expression (in the global environment).
    * D - Disassemble the code of the current frame.
    * r - Restart execution of the frame with the same arguments as it was called originally. (Not supported by all backends.)
    * R - Return a value from the current frame. (Not supported by all backends.)

Motion between frames:

    * n - Move to the next frame.
    * p - Move to the previous frame.
    * M-n - Detail-move to the next frame. This switches the current frame to a one-line summary, then moves to the next frame, displays it verbosely in the backtrace (as with t), and shows the corresponding source expression (as with v).
    * M-p - Detail-move to the previous frame.

==Reference
C-c <        slime-who-calls                         Reveal callers of the function at or around point.
C-c >        slime-who-references                    Reveal references to the global variable at or around point.

C-c C-d is the prefix for the following help commands:
    d        slime-describe-symbol                    Describe the symbol at point.
    a        slime-apropos
    z        slime-apropos-all                        Includes internal symbols.
    p        slime-apropos-package
    h        slime-hyperspec-lookup                   Customize with common-lisp-hyperspec-root browse-url-browser-function.
    ~        common-lisp-hyperspec-format