Emacs tips, keystrokes, usage cases, etc

I use an Aquamacs installation described here. This post is dedicated to accumulating the ways in which I use emacs.

Translating from traditional emacs parlance for key bindings:

C    is    Control
A    is    ⌘
M    is    Option (Alt) or Esc (but don't hold down escape, just tap it)

Aquamacs basic navigation and editing

Note that there are so many commands just in my muscle memory that this list is highly incomplete.

  • ⌘-1 goes to left most buffer of tabs, ⌘-2 to second to left, etc
  • ctrl-K delete from cursor to end of line
  • ⌘-L go to line number
  • ⌘-F search forward
  • ⌘-G search again (including wrapping to start of doc)
  • ⌘-; spell check (jump to next problem point)
  • ⌘-, open Customization menu
  • ⌘-/ / ' uncomment or comment selected region (yes, that's 3 keystrokes on top of ⌘
  • ⌘-Delete deletes entire current line
  • M-q reformat current paragraph (makes no sense in coding environments, useful in latex and text environments)

Python related commands

  • ctrl-C ! launched python interpreter
  • ctrl-C ctrl-C run current buffer in python buffer or launches python and runs current buffer in it
  • ctrl-C | execute just selected region (though indents can wreck havoc)
  • M-/ brings up possible completions
  • C-c g goto definition
  • C-c d show documentation
  • M-; comment/uncomment region

My Emacs setup

I use an Aquamacs installation, primarily because it plays nice with TextExpander and LaunchBar's Clipboard History. (Other emacs installations don't play so nicely with those two tools, which are both well engrained in my muscle memory.) Currently I'm at Aquamacs 2.4.

Note that aquamacs should read from a .emacs file, but with lowest priority. My preferred place for preferences is /Users/User Name/Library/Preferences/Aquamacs Emacs/Preferences.el and that is also where I put configuration files and emacs packages.

Running Python from Emacs

Upon installation Aquamacs already knows where python is and plays nice. It even finds my Enthought Python Distribution installation. As a quick example open a file with some simple code and type Ctrl-C Ctrl-C to launch python and run the code.

To enable up/down arrows to move through the command history in the python interpreter window add the following to /Users/User Name/Library/Preferences/Aquamacs Emacs/Preferences.el:

(require 'comint)\n
(define-key comint-mode-map (kbd "M-") 'comint-next-input)
(define-key comint-mode-map (kbd "M-") 'comint-previous-input)
(define-key comint-mode-map [down] 'comint-next-matching-input-from-input)
(define-key comint-mode-map [up] 'comint-previous-matching-input-from-input)`

IPython: Ideally I'd like to be able to run ipython rather than python, but for now (20120630) it appears to be impossible (or just too much of a pain for me to pursue). It sounds like eventually it will be included within python-mode.el.

Autopairing of Parentheses, Brackets, Quotation Marks, etc

Download autopair.el and place into /Users/User Name/Library/Preferences/Aquamacs Emacs/. Add the following to /Users/User Name/Library/Preferences/Aquamacs Emacs/Preferences.el (Note that the long add-hook enables autopairing of python triple-quotes for comment blocks.):

(autoload 'autopair-global-mode "autopair" nil t)
(add-hook 'python-mode-hook
          #'(lambda ()
              (push '(?' . ?')
                    (getf autopair-extra-pairs :code))
              (setq autopair-handle-action-fns
                    (list #'autopair-default-handle-action

Code Completion (amongst other features)

pymacs: Download pymacs, pymacs manual, and install:

tar -xzvf the-Pymacs-download (my download was pinard-Pymacs-v0.25-0-g5989046.tar)
cd the-Pymacs-directory (mine was pinard-Pymacs-5989046)
make check
sudo make install
cp pymacs.el ~/Library/Preferences/Aquamacs\ Emacs/.
emacs -batch -eval '(byte-compile-file "~/Library/Preferences/Aquamacs Emacs/pymacs.el")'

Check that the pymacs install went OK by running from Pymacs import lisp in a python session.

Check that the pymacs library loads OK in aquamacs by doing M-x load-library RET pymacs RET

Add to /Users/User Name/Library/Preferences/Aquamacs Emacs/Preferences.el:

(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
(autoload 'pymacs-autoload "pymacs")
;;(eval-after-load "pymacs"
;;  '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY"))

After restarting aquamacs, test that pymacs is working with M-x pymacs-eval RET repr(2L*111) RET.

rope library: Download and install:

tar -xzvf rope-0.9.4.tar.gz
cd rope-0.9.4
sudo python install

ropemode: Download and install:

tar -xzvf ropemode-0.2.tar.gz
cd ropemode-0.2
sudo python install

ropemacs: Download and install:

tar -xzvf ropemacs-0.7.tar.gz
cd ropemacs-0.7
sudo python install

auto-complete: Download auto-complete, manual for auto-complete, and install:

tar -xzvf auto-complete-1.3.1.tar.bz2
mkdir ~/Library/Preferences/Aquamacs\ Emacs/auto-complete

In a newly launched aquamacs:

M-x load-file RET
choose to install from: ~/Downloads/auto-complete-1.3.1/etc/install.el
choose to install to:  ~/Library/Preferences/Aquamacs\ Emacs/auto-complete/

Add to /Users/User Name/Library/Preferences/Aquamacs Emacs/Preferences.el:

(add-to-list 'load-path "~/Library/Preferences/Aquamacs Emacs/auto-complete/")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/Library/Preferences/Aquamacs Emacs/auto-complete//ac-dict")

CheatSheet - an app for showing available key commands

I heard about the free application CheatSheet on the recent MacPowerUsers episode with Merlin Mann.  It looks very nice toward helping with the 'continuing education' of using more and more keyboard shortcuts.  Download it from the Mac App Store and launch it.  Then any time you hold the ⌘ key down for 2 seconds a window will pop up showing all of the currently available key commands in whatever application you're in.  Thus far I haven't noticed any interference problems, i.e. application switching with ⌘-tab still works fine so long as you hit tab within two seconds of ⌘.

(Unfortunately I've found at least one instance where it doesn't work well, within Eclipse, which might have something to do with that Eclipse runs within Java runtime.)


Creating a countdown calendar in iCal (or BusyCal etc)

I needed to create a custom calendar for someone with a single event per day telling the number of days remaining until a particular date.  AppleScript made this easy:

tell application "iCal"
	tell calendar "Studio Countdown"
		set theDate to date "Thursday, October 4, 2012 12:00:00 AM"
		set daysUntilShow to 1
		repeat while theDate is greater than or equal to ((current date) - (1 * days))
			make new event at end with properties {description:"", summary:"T-" & (daysUntilShow as string), location:"", start date:theDate, allday event:true}
			set theDate to theDate - (1 * days)
			set daysUntilShow to daysUntilShow + 1
		end repeat
	end tell
end tell

Enabling user web sites on Mac OS X Server

To enable user web sites, (e.g. on Mac OS X Server: Go to Server Admin -> Your Server Name -> Web -> Settings and enable userdir_module and click Save.

Page 1 ... 6 7 8 9 10 ... 32 Next 5 Entries »