Tag Archives: emacs

Trying Out Vim Using Emacs Evil Mode

After using the text editor Emacs for over 20 years, and after listening to debates on the merits of Emacs vs Vi/Vim  (henceforth in this post referred to as simply “Vim”) for at least as many years, I decided that I wanted to give Vim a try. To be fair, I had used Vim before, but, also to be fair, I had never tried to master it or given it a real chance. I knew enough Vim keybindings (the “hjkl” keys and “ZZ” to save and quit) to get by when editing a file via a remote terminal. But I had never taken the time to really learn Vim to the point that it would be an efficient text editor for me. And I certainly didn’t want to abandon Emacs, mostly because of Org mode  the best organizational tool there is, and Magit , the best Git interface there is. Nevertheless the constant key-chording of Emacs, which uses control key combinations for most editing tasks, continued to be awkward despite many years of practice. The question kept coming up: was using Vim a better way to edit text?

My initial resistance to Vim was not just because I liked Emacs. Vim is a modal text editor, so-called because entering text and editing text require changing modes. Moreover, the “Normal” mode in Vim is the text editing mode. To actually enter text, you use a keyboard command to switch to “Insert” mode. To return to Normal mode, you use the Escape key. So you use the Escape key a lot. On my Mac keyboard, the Escape key is located at the top corner of the keyboard, a tiny sliver of a key that is several inches away from my left pinky. New MacBooks don’t even have a dedicated Escape key anymore.

The modal concept caused problems in my prior limited use of Vim. I would constantly forget what mode I was in and start typing in the wrong mode, causing havoc to my text. But still, lots of people used Vim and liked it a lot.

So I started reading more about it. I bought Drew Neil’s book,  Practical Vim, and skimmed through it. Something he said in chapter 2 of the book I found attractive. To paraphrase him, text is to the writer as a painting is to a painter. A painter spends time studying his subject, mixing paints, selecting brushes, and so forth. Only a fraction of time is used to actually apply paint. Likewise a writer, or programmer, spends a lot of time thinking and editing rather than just putting text down on the screen.

While I suspect the analogy appeals more to my vanity, comparing writing to art, than is true (because I think both writers and painters probably spend most of their time applying words or paint to canvas), I think the theory is at least worth trying to put into practice. Editing is what turns mediocre writing into good writing, and what bit of writing wouldn’t benefit from more editing?

Beyond the theoretical, Drew’s book is chock full of examples in which Vim shines as a way to edit text rapidly with a minimum of keystrokes. I had used Emacs’ macros on occasion to do repetitive tasks, but it looked like Vim had the potential to really rev up my editing speed.

Enter Evil mode  for Emacs. Evil mode is an Emacs major mode that transforms Emacs into a Vim clone. You can edit text using Vim keybindings, and still have all other Emacs functionality available. In other words, the best of both worlds. I have been using it for about a week now, and I think it’s great.

It works fine out of the box, but some tweaking always helps. First off, I remapped my Caps Lock key to be the Escape key in my System Preferences. It’s right next door to the “A” key and makes changing modes (referred to as “States” in the Evil manual, since the word “mode” has its own meaning in Emacs) a snap.

Then I added some fixes so that cursor movement with the “hjkl” keys would respect visual lines instead of physical lines, since a lot of my writing uses Emacs word wrap mode. Here is what I inserted into my .emacs file:

;; play with evil mode
(use-package evil
:ensure t
;; make it default, gulp!
(evil-mode 1)
;; Make movement keys work respect visual lines
(define-key evil-normal-state-map (kbd "<remap> <evil-next-line>") 'evil-next-visual-line)
(define-key evil-normal-state-map (kbd "<remap> <evil-previous-line>") 'evil-previous-visual-line)
(define-key evil-motion-state-map (kbd "<remap> <evil-next-line>") 'evil-next-visual-line)
(define-key evil-motion-state-map (kbd "<remap> <evil-previous-line>") 'evil-previous-visual-line)
;; Make horizontal movement cross lines
(setq-default evil-cross-lines t))

Finally, there are some unexpected niceties of Evil mode that makes it perfect for someone wanting to transition to Vim. First of all, it is pretty easy to tell what mode/state you are in because the cursor changes shape and the mode line has a little indicator like so: <N> that indicates the state.

Second, you can easily go back to Emacs keybindings at any time by pressing C-z. The state indicator indicates for Emacs mode. Press C-z again to return to Vim keybindings.

Third, while in Vim Insert mode, a lot of Emacs keybindings work! You can move around with C-f, C-b, M-f, M-b, etc.! So no need to constantly change modes if you don’t want to. I expect I will use this less as I get more used to “The Vim Way,” but it sure is helpful for learning.

Finally, many other Emacs keybindings work too. C-l centers the cursor in the page. I can use C-x C-s to save the file, as opposed to :w in Vim. Of course M-x commands all still work too. And C-g, the Emacs get of jail key, works as well.

So if you want to have the best of both worlds, and bring the editor wars to a peaceful settlement, Evil mode is the answer.

Here is a good talk on YouTube that also contributed to my decision to try Evil mode.