Yasnippet and Auto Completion

If I could only install two extensions to Emacs I won’t need a second to think about which ones to add:

  • Yasnippet
  • Auto Complete Mode

The two extensions were already integrated into other configs in this blog, but they are so amazing that I would like to discuss them in a separate article so you cannot overlook them.


Yasnippet is a template engine for Emacs that’s very simple to use and can help you to speed up from bicycle to racing car in every Emacs task.

You are a programmer and have common code snippets you type over and over again e.g. loops, unit tests etc? You are blogging or writing articles, which have all the same structure? You would like to use email templates? Often used SQL queries or shell commands? All common tasks for Yasnippet.

To activate Yasnippet in your Emacs configuration evaluate the following code

(package-install 'yasnippets)
(require 'yasnippets)
(yas/load-directory "~/.emacs-snippets"))
(setq yas/indent-line nil)
(yas-global-mode 1)


This will install and activate it everywhere. Your snippets are stored in ~/.emacs-snippets.
Now let’s create a new snippet! Type M-x yas/new-snippet and paste the following

# name: html for
# key: for
# --
{% for ${1:item} in ${2:object} %}
{% endfor %}


This is the template for a simple for loop. The name is optional, key is the keyword that will get expanded. ${1:item} tells yasnippet that this is a variable it should name item and ask you to fill it out, same with ${2:object}. S0 tells Yasnippet that it should place the cursor here after expansion.

Type C-c C-c to commit your changes and it will ask you for the mode this template should be used in let’s say html-mode.

If you are now editing a Django or Jinja template just type for, press tab and the template gets activated.

Here you can find more information on the template syntax.

Auto Completion

Are you lazy? Lazyness can be a feature for you as a programmer or sysadmin. Non-lazy people dont think as much about automation as lazy ones. Auto-complete Mode is another gimmick that can spare you some typing time. If you type a word that’s already in the buffer or in all buffers and external completion methods like yasnippet keywords, latex commands etc.


To install auto-complete evaluate the following code

(package-install 'auto-complete)
(require 'auto-complete)
(setq ac-dwim t)
(setq ac-sources '(ac-source-yasnippet

The first lines just install auto-complete and load it globally with default settings. The most important variable is ac-sources. It tells auto-complete from which sources it should load it’s possible completions. A List of builtin sources can be found here. The configuration above looksup words found in your yasnippet keywords, .abbrev file and words in the same buffer.

If you find it annoying that auto-complete works automatically you can tell it to only expand on key press.

(setq ac-auto-start nil)
(ac-set-trigger-key "TAB")

If you think it’s completing to fast and blowing up too many CPU cycles, tell it to make a bit slower.

(setq ac-delay 0.5)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s