Thursday, May 24, 2012

Evil - An Emacs Mode for VI/Vim Users

Two years ago, I wrote an article that covered two packages (Vim-Mode and Vimpulse) that brought Vim-style modal editing to Emacs. Evil (extensible VI layer) has been introduced as a newer replacement for both packages.


Getting Started With Evil

Evil setup is a two step process: get it on your system and amend your .emacs file to load and use it.

There are multiple ways to install or download Evil. The official site suggests two: use git to clone Evil's repository, or download & extract a .tar.gz. The Emacs Wiki entry has installation instructions for el-get, quelpa, and Emacs's built-in package manager

Here's how you get Evil using el-get:

1. Install and Setup el-get
el-get's Github repository has up-to-date info on installation and setup.

2. Run El-Get-Install
You need to type the following in Emacs:

M-x el-get-install

When prompted, enter 'evil'.

As of this writing, the el-get recipe for Evil clones its main git repository. This can take a while, particularly on slower Internet connections.

3. Configure Emacs
Put the following code in your .emacs file:

(require 'evil)
(evil-mode 1)


The next time you start Emacs, you'll be using Evil. Much like VI/Vim, it defaults to normal mode at startup.

Possible Error You Might Encounter
I received the following error message while trying to use Evil on my OSX Lion Mac Mini:

An error has occurred while loading `EMACS DIR':

Symbol's function definition is void: declare-function

Mac OSX Lion, Mavericks, and (I assume) Mountain Lion come with GNU Emacs version 22.1.1 by default. Using a newer version of Emacs corrects this error.

Configuration

Evil config should be placed in your .emacs file (or in a file loaded by your .emacs). The documentation available on Evil's official site covers the basics of configuration and provides some examples. Here are some more examples:

1. Getting :n[ew] to work
As of this writing, Evil does not allow you to shorten ':new' to ':n', but you can define a command that does.

(evil-ex-define-cmd "n[ew]" 'evil-window-new)

Evil's version of ':new' opens a new window within Emacs.

2. Changing Cursors
Change the normal mode text cursor to a hollow box:

(setq evil-normal-state-cursor 'hollow)


Change the insert mode cursor to a red underline:

(setq evil-insert-state-cursor '("red" hbar))


3. 'imap jf <ESC>' equivalent
I know of two easy ways to exit insert mode without pressing Escape: define-key and key-chord define. This example of the first method maps to Control-J:
(define-key evil-insert-state-map (kbd "C-j") 'evil-normal-state)

A previous version of this article incorrectly had (kbd "j f") instead of (kbd "C-j"). This may seem to work at first but makes it difficult to enter the letter 'j' while in insert mode.

You can get the "jf" behavior by installing another package named key-chord.el. There does not seem to be a good resource for it, but here's an EmacsWiki entry for Key Chord. Install it like you did Evil and place the following in your .emacs file:

(require 'key-chord)
(key-chord-mode 1)

Now you're ready to use it! Here's an example of using it with Evil:

(define-key evil-insert-state-map (kbd "C-j") 'evil-normal-state)


Conclusion

If you want Vim-style editing in Emacs, Evil provides just that. It's easy to setup, and configuration is easy if you are somewhat familiar with Emacs Lisp.

Updates to this article:
December 20, 2014 - Updated content and fixed some mistakes (such as the broken 'imap jf <Esc>').