This is the first screencast covering basic Emacs use on the Mac. The following topics are covered:

  1. Windows, basic file operations, customizing window position
  2. Modifier keys, in particular options for international users
  3. Filesets
  4. Backup files
  5. Outline major mode

The following explanations are very short, but might be helpful, if the explanations in the video are not clear. If you want to know more about the concepts, variables and functions used in Emacs, please read the excellent manual that is accessible via the Help menu or on the Web. You can download the configuration file shown in the screencast here. After the download, move it to your home directory and change its name (.emacs) afterwards.

Windows, basic file operations, customizing window position

What you would typically call a "window" is called a frame in Emacs. You can have multiple frames open; create new frames via the File menu or by hitting either Command-N or Control-x 5 2 (platform independent shortcut). Close a frame via the File menu or by hitting either Command-W or Control-x 5 0. Note that when you close a frame, this action does not close the files that are currently displayed in it, you can still access them via the Buffers menu in another frame. Cycle through frames using Control-x 5 o or Command-` (activated by default, OK for US keyboards) or Command-<, if you enable it in your .emacs:

(define-key global-map [?\s-<] 'other-frame)

You can split a frame in multiple windows via the File menu or by hitting Control-x 2 ("vertical split": two windows, one above the other). For "horizontal split", use Control-x 3. Merge windows via File→Remove Splits or by hitting Control-x 1. If you enable this in your .emacs file

(define-key global-map [?\s-<] 'other-window)

you can use Command-< to cycle through windows in a frame. Is there a another common shortcut for this in Mac OS X, maybe for cycling through tabs?

A window displays the contents of a buffer. A buffer holds the text you are editing. In most cases, this text will be the contents of a file (open files using Command-O or Control-x Control-f). Depending on the mode of the active buffer, the menu entries and toolbar may change (e.g. Elisp menu entrywhen editing an .el file or your .emacs file). Select the buffer to be displayed in the active window using the Buffers menu; cycle through buffers by clicking on the buffer name in the modeline. Select a buffer using string completion in the minibuffer by hitting Control-x b and typing the first characters,then hitting Tab (we will cover more sophisticated completion mechanisms in a later screencast). Close (kill) a buffer via the file menu or by hitting Command-k or Control-x k.

The following settings set the initial window (frame) position, enable the toolbar by default, provide a platform-adapted File menu, and make emacs open a file in a new frame when you drop it on an existing Emacs window, respectively:

(setq initial-frame-alist
       '((top . 50) (left . 90) (width . 90) (height . 40)))
(tool-bar-mode)
(ns-extended-platform-support-mode)
(define-key global-map [ns-drag-file] 'ns-find-file)

Modifier keys, in particular options for international users

Emacs uses two main modifier keys: Control (C-) and Meta (M-). The default Emacs package for Macs (and GnuStep) maps your alternate modifier "Option" to act as Meta and your Command key to another Emacs modifier, Super(s-), enabling you to use platform specific (i.e. customary Mac) keyboard shortcuts for popular commands (open file, copy&paste etc.) without messing with the default Emacs modifiers. This is fine for people who use a US layout on their keyboard. At least for some common continental European keyboard layouts, this hinders users from entering special characters, because these are typically entered using the option key (e.g. square brackets, tilde, @). Three major options are available to them. I do not endorse any one in particular, because I would not consider any one of these to be "naturally" superior. The following snippets present probably not the most elegant way to achieve the intended effect, and possibly do not conform to any standard Emacs convention. However, they should work as expected under normal circumstances.

Option 1: Remove Meta binding from option key

By default, you can use the Escape key to enter a two character sequence acting as Meta. If you are using keyboard shortcuts a lot, this does not seem very ergonomic (except for vi converts, possibly). You could use another key to act as Meta modifier.

(setq ns-alternate-modifier 'none)

Option 1.1: Rebind kp-enter to Meta

enterkey.png

kp-enter is the enter key on the numeric pad. If you mainly use a Laptop keyboard, this might be convenient.

(global-unset-key [kp-enter])
(define-key function-key-map [kp-enter] 'event-apply-meta-modifier)

Option 1.2: Rebind fn to Meta

fn is the Function key. It is available on many Mac keyboards, and it is commonly used to access the F keys in your top row or to trigger the specific actions on those keys (volume control, screen brightness, etc.), depending on your setting in the System Preferences. Additionally, it is used to activate the numpad characters on the right hand side of laptop keyboards. This is the reason, why you would have to remap some Meta key combinations involving digits (see code below).

(setq ns-function-modifier 'meta)
(define-key global-map "\M-1" (lookup-key (current-global-map) "\M-j"))
(define-key global-map "\M-2" (lookup-key (current-global-map) "\M-k"))
(define-key global-map "\M-3" (lookup-key (current-global-map) "\M-l"))
(define-key global-map "\M-4" (lookup-key (current-global-map) "\M-u"))
(define-key global-map "\M-5" (lookup-key (current-global-map) "\M-i"))
(define-key global-map "\M-6" (lookup-key (current-global-map) "\M-o"))
(define-key global-map "\M-*" (lookup-key (current-global-map) "\M-p"))

Option 2: Rebind a couple of Meta combos to produce popular special characters

You could also build a set of frequently used special characters and remap the existing Meta key bindings to produce these. This is how Aquamacs handles this problem. It keeps some lists of language (keyboard) specific, popular special characters and provides Meta bindings for them. The downside of this is that, if you require a lot of special characters, you will mess up all the existing Meta keybindings used for regular Emacs commands. See the file aquamacs/src/site-lisp/macosx/emulate-mac-keyboard-mode.el in the Aquamacs github repository.

Options 1 and 2: Remap stolen Meta keybindings to Super

Meta-digit is a standard Emacs keybinding to supply a numeric argument to the following command. It's up to the following command to interpret it, but it is commonly used to specify the number of times the command should run ("repetition count"), so it is actually quite useful to have a shortcut for this. The simplest way to take care of this is to remap s-digit to provide the numric argument.

(define-key global-map [?\s-2] 'digit-argument)
(define-key global-map [?\s-3] 'digit-argument)
(define-key global-map [?\s-4] 'digit-argument)
(define-key global-map [?\s-5] 'digit-argument)
(define-key global-map [?\s-6] 'digit-argument)
(define-key global-map [?\s-7] 'digit-argument)

Option 3: Don't use Mac "proprietary" key combos

If you do not rely on having Mac specific shortcuts involving the Command key available to you, you could simply use the default Emacs keyboard shortcuts. To do so, simply remap the Command key to act as Meta and remove any binding from the Option key:

(setq ns-alternate-modifier 'none)
(setq ns-command-modifier 'meta)

Filesets

The easiest way to group files is by using Filesets. This feature is provided with the default distribution. Note, that youc ould also open all files in a specific directory by entering the wildcard character * in the minibuffer open file dialog, but filesets can be specified once and made persistent. If you enable filesets,

(filesets-init)
you will get a nice Filesets submenu in the buffer for working with filesets. I advice you to download filesets+.el and dired+.el. These files contain a bugfix for the directory tree type filesets and a filter mode for the directory browser, displaying only files contained in a specific fileset. Depending on your web browser, you might have to change the file extension from txt to el in the Finder after the download. To install these files, open the Emacs.app package (right-click on the application icon, "Show Package Contents", navigate to Contents/Resources/lisp and copy the files into this directory. Then, open these files in Emacs and select Byte-compile This File from the Emacs-Lisp menu for each.

Package Contents.png Contents Resources lisp.png

Finally, add these lines to your .emacs, before (filesets-init):

(require 'filesets+)
(require 'dired+)

Now, define a fileset using the Customization buffer (FileMenu→Filesets→# Filesets→Edit Filesets) or by changing the variable filesets-data. There are different ways to specify a fileset, please see the manual or the hints in the customization dialog. If you use the Customization tool, a new section will be added to your .emacs file that holds all customizations. By default, the filesets package assigns commands for viewing files externally to a set of file types (e.g. browse-url for html files). You can disable this behavior for all files by removing these associations. This will not influence any other bindings for external viewers.

(setq filesets-external-viewers nil)

Enter M-x filesets-open and specifiy the name of the fileset to open all files contained in it.

Backup files

Emacs creates backup files to store the contents of a file at the beginning of an editing session (unless the file is under version control). You can disable this behavior via a central switch:

(setq make-backup-files nil)

You can also define a directory for these backup files, so they will not be scattered around in the directories you are saving your files in. See the section in the manual (Files→Saving→Backup).

Outline major mode

Outline mode is actually used more commonly as a minor (helper) mode, e.g. when working on LaTeX or HTML documents. The major mode is useful for taking hierarchical notes. You can activate the outline major mode for a buffer by hitting M-x outline-mode. Level headings use asterisk (*) characters, and you can access the major functions for folding, hiding, moving via the Outline menu entries ("Headings", "Show", "Hide"). To enable this mode for a certain file extension, add the pattern for the files and the name of the mode to the variable auto-mode-alist:

(add-to-list 'auto-mode-alist '("\\.outl\\'" . outline-mode))

Shortcuts starting with C-c are reserved for the major mode, so we are free to rebind some keys to allow for convenient access to some popular commands (C-> is definitely not convenient on a German keyboard!).

(add-hook 'outline-mode-hook
	  '(lambda ()
	     (define-key outline-mode-map "\C-c."
	       'outline-promote)
	     (define-key outline-mode-map "\C-c,"
	       'outline-demote)))

The end

I hope, you found the screencast and these notes instructive, and you will find some time to play around with the Emacs editor. In the following screencasts I am going to cover an advanced completion mode, a way to store and use snippet templates, Matt's Ten Surprising Uses of BBEdit, modes for HTML/CSS/JavaScript and AUCTeX. As there are many instructions around for using Emacs for Ruby development and since I am not a Ruby programmer, I am not going to cover this topic here. It is my intention to show that Emacs is not only a very good editor for "real programmers" but also a viable alternative for other editing tasks.

Comments

There are no comments on this post.