Emacs markdown-mode
markdown-mode is a major mode for editing Markdown-formatted text files in GNU Emacs. markdown-mode is free software, licensed under the GNU GPL.
The latest stable version is markdown-mode 1.7, released on October 1, 2009:
markdown-mode is also available in the Debian emacs-goodies-el package (beginning with revision 27.0–1) and the OpenBSD textproc/markdown-mode package.
The latest development version can be downloaded directly (markdown-mode.el) or it can be obtained from the (browsable and clonable) Git repository at http://jblevins.org/git/markdown-mode.git. The entire repository, including the full project history, can be cloned via the Git protocol by running
git clone git://jblevins.org/git/markdown-mode.git
Dependencies
markdown-mode requires easymenu, a standard package since GNU Emacs 19 and XEmacs 19, which provides a uniform interface for creating menus in GNU Emacs and XEmacs.
Installation
Make sure to place markdown-mode.el somewhere in the load-path and add
the following lines to your .emacs file to associate markdown-mode
with .text files:
(autoload 'markdown-mode "markdown-mode.el"
"Major mode for editing Markdown files" t)
(setq auto-mode-alist
(cons '("\\.text" . markdown-mode) auto-mode-alist))
There is no consensus on an official file extension so change .text to
.mdwn, .md, .mdt, or whatever you call your markdown files.
Customization
Although no configuration is necessary there are a few things that can be customized. The M-x customize-mode command provides an interface to all of the possible customizations:
markdown-command- the command used to run Markdown (default:markdown).markdown-hr-length- the length of horizontal rules (default:5).markdown-bold-underscore- set to a non-nil value to use two underscores for bold instead of two asterisks (default:nil).markdown-italic-underscore- set to a non-nil value to use underscores for italic instead of asterisks (default:nil).markdown-indent-function- the function to use for automatic indentation (default:markdown-indent-line).markdown-indent-on-enter- set to a non-nil value to automatically indent new lines when the enter key is pressed (default:t)markdown-uri-types- a list of protocols for URIs that `markdown-mode’ should highlight.markdown-enable-math- syntax highlighting for LaTeX fragments (default:nil).
Additionally, the faces used for syntax highlighting can be modified to your liking by issuing M-x customize-group RET markdown-faces or by using the “Markdown Faces” link at the bottom of the mode customization screen.
Usage
Keybindings are grouped by prefixes based on their function. For example, commands dealing with headers begin with C-c C-t. The primary commands in each group will are described below. You can obtain a list of all keybindings by pressing C-c C-h.
Anchors: C-c C-a
C-c C-a l inserts inline links of the form
[text](url). If there is an active region, text in the region is used for the link text. C-c C-a w acts similarly for wiki links of the form\[[WikiLink]].Commands: C-c C-c
C-c C-c m will run Markdown on the current buffer and preview the output in another buffer while C-c C-c p runs Markdown on the current buffer and previews the output in a browser.
C-c C-c c will check for undefined references. If there are any, a small buffer will open with a list of undefined references and the line numbers on which they appear. In Emacs 22 and greater, selecting a reference from this list and pressing
RETwill insert an empty reference definition at the end of the buffer. Similarly, selecting the line number will jump to the corresponding line.Images: C-c C-i
C-c C-i i inserts an image, using the active region (if any) as the alt text.
Physical styles: C-c C-p
These commands all act on text in the active region, if any, and insert empty markup fragments otherwise. C-c C-p b makes the selected text bold, C-c C-p f formats the region as fixed-width text, and C-c C-p i is used for italic text.
Logical styles: C-c C-s
These commands all act on text in the active region, if any, and insert empty markup fragments otherwise. Logical styles include blockquote (C-c C-s b), preformatted (C-c C-s p), code (C-c C-s c), emphasis (C-c C-s e), and strong (
C-c C-s s).Headers: C-c C-t
All header commands use text in the active region, if any, as the header text. To insert an atx or hash style level-n header, press C-c C-t n where n is between 1 and 6. For a top-level setext or underline style header press C-c C-t t (mnemonic: title) and for a second-level underline-style header press C-c C-t s (mnemonic: section).
Other commands
C-c - inserts a horizontal rule.
Many of the commands described above behave differently depending on whether Transient Mark mode is enabled or not. When it makes sense, if Transient Mark mode is on and a region is active, the command applies to the text in the region (e.g., C-c C-p b makes the region bold). For users who prefer to work outside of Transient Mark mode, in Emacs 22 it can be enabled temporarily by pressing C-SPC C-SPC.
When applicable, commands that specifically act on the region even
outside of Transient Mark mode have the same keybinding as the with
the exception of an additional C- prefix. For example,
markdown-insert-blockquote is bound to C-c C-s b and only acts on
the region in Transient Mark mode while markdown-blockquote-region
is bound to C-c C-s C-b and always applies to the region (when
nonempty).
markdown-mode supports outline-minor-mode as well as org-mode-style
visibility cycling for atx- or hash-style headers. There are two
types of visibility cycling: Pressing S-TAB cycles globally between
the table of contents view (headers only), outline view (top-level
headers only), and the full document view. Pressing TAB while the
point is at a header will cycle through levels of visibility for the
subtree: completely folded, visible children, and fully visible.
Note that mixing hash and underline style headers will give undesired
results.
Extensions
Besides supporting the basic Markdown syntax, markdown-mode also
includes syntax highlighting for \[[Wiki Links]] by default.
SmartyPants support is possible by customizing markdown-command.
If you install SmartyPants.pl at, say, /usr/local/bin/smartypants,
then you can set markdown-command to "markdown | smartypants".
You can do this either by using M-x customize-group markdown
or by placing the following in your .emacs file:
(defun markdown-custom ()
"markdown-mode-hook"
(setq markdown-command "markdown | smartypants"))
(add-hook 'markdown-mode-hook '(lambda() (markdown-custom)))
Experimental syntax highlighting for mathematical expressions written
in LaTeX (only expressions denoted by $..$, $$..$$, or \[..\])
can be enabled by setting markdown-enable-math to a non-nil value,
either via customize or by placing (setq markdown-enable-itex t)
in .emacs, and restarting Emacs.
Acknowledgments
markdown-mode has benefited greatly from the efforts of the following people:
- Cyril Brulebois for Debian packaging.
- Conal Elliott for a font-lock regexp patch.
- Edward O’Connor for a font-lock regexp fix.
- Greg Bognar for menus and a patch.
- Daniel Burrows for filing Debian bug #456592.
- Peter S. Galbraith for maintaining emacs-goodies-el.
- Dmitry Dzhus for reference checking functions.
- Bryan Kyle for indentation code.
- intrigeri for face customizations.
- Ankit Solanki for longlines.el compatibility.
- Hilko Bengen for proper XHTML output.
- Jose A. Ortega Ruiz for Emacs 23 fixes.
- Alec Resnick for bug reports.
- Peter Williams for fill-paragraph enhancements.
Bugs
Although markdown-mode is developed and tested primarily using GNU Emacs 23, compatibility with GNU Emacs 21 and 22 is also a priority.
markdown-mode’s syntax highlighting is accomplished using the search-based fontification features of Emacs through a series of regular expressions. Unfortunately, Emacs has trouble highlighting multi-line constructs using regular expressions and this creates several syntax-highlighting quirks such as mistaking indented lists for preformatted text, etc. Making markdown-mode’s syntax highlighting more robust through the use of matching functions or syntactic font lock is a high-priority item for future work.
If you find any bugs not mentioned here, please construct a test case and/or a patch and email me at jrblevin@sdf.lonestar.org.
History
markdown-mode was written and is maintained by Jason Blevins. The first version was released on May 24, 2007.
- 2007–05–24: Version 1.1
- 2007–05–25: Version 1.2
- 2007–06–05: Version 1.3
- 2007–06–29: Version 1.4
- 2008–05–24: Version 1.5
- 2008–06–04: Version 1.6
- 2008–10–01: Version 1.7