Robert Kozikowski's blog

Smartparens is super awesome, especially with evil mode

Introduction

If you use Emacs, you must try smartparens. I strongly regret not trying it out earlier. It’s one of those packages that you will be wondering how did you live without it.

It helps navigating and editing “parentheses” groups much better. When I first saw it I thought it will be useful for lisp or just small expressions in other langugaes. The more language relies parentheses of any kind (e.g. () or {}), the more useful it is. It is life changer in lisp, very useful in C++ and java and only sometimes useful in python.

There are two competitive packages in this area – smartparens and paredit. I didn’t try paredit, but from what I heard, smartparens works better for non-lisp code.

Configuring it to work well with evil mode took me a bit.

Two evil-smartparens packages

After installing base smartparens package, there is a bit of work to make it work well with evil mode. There are actually two packages named the same way, evil-smartparens, and both are useful and improve different aspects of integration with evil mode.

evil-smartparens from melpa

Melpa link. Installed via the M-x package-install evil-smartparens. It makes current evil commands work better with evil-smartparens. It does not add any new keybindings for smartparens commands.

evil-smartparens from github

I found an alternative evil-smartparens on github at https://github.com/jschaf/evil-smartparens/. It doesn’t modify behavior of existing evil commands, but just adds an “evil feel” keybindings for smartparens commands. They work much better for me than the default smartparens keybindings and I had much easier time remembering them, since they have evil mode equivalent.

Installation

It’s not on melpa, so I had to manually add the elisp code. Name of the package, evil-smartparens, conflicts with the package from melpa, so I had to rename it to evil-smartparens-keybindings.

My configuration

(require 'smartparens)
(require 'evil-smartparens)
(require 'evil-smartparens-keybindings)

(require 'smartparens-config)

(smartparens-global-mode t)
(evil-smartparens-keybindings-mode t)
(show-smartparens-global-mode t)
(smartparens-strict-mode t)

(add-hook 'prog-mode-hook #'evil-smartparens-mode)
(add-hook 'prog-mode-hook 'turn-on-smartparens-strict-mode)
(add-hook 'org-mode-hook #'evil-smartparens-mode)
(add-hook 'org-mode-hook 'turn-on-smartparens-strict-mode)

(add-hook
 'smartparens-enabled-hook #'evil-smartparens-mode)

(provide 'smartparens-setup)

(setq sp-show-pair-from-inside t)

How I learned how to use it

M-x describe-key-map evil-smartparens-mode-map only makes current evil commands work better with smartparens. It primary purpose is to not let evil unbalance the parentheses when using the (smartparens-strict-mode t). You may take a look at it to see which evil commands will behave differently.

New functions provided by smartparens are the important part. I looked through all keys bound by the https://github.com/jschaf/evil-smartparens/, call describe-function on each and try them on some code (especially lisp).

One thing that you may not be immediately aware of is that many of the “edit” commands work well with . command in evil mode. You can try it by creating parantheses, place cursor inside sexp and type “zfl” and “.”.