Einar's blog

Vim vs Emacs for fast text editing

vimvsemacs.jpg

Introduction

I used vim for 2 years and got it into my muscle memory before my Emacs curiosity led me to try out GNU Emacs in the spring of 2022. Many people say vim is the faster text editor, but Emacs is wonderfully extensible. After having used both, I don't think vim is faster than Emacs, so I am going to show how many consecutive key presses you need to accomplish some common tasks in both and compare. Of course, you can use evil-mode in Emacs and edit text the vim way, but I am comparing the usual non-evil default keybindings of Emacs with the defaults in vim. There is also the common halftruth about crazy key chords in Emacs contra simple one-key presses in vim. This is true for many workflows, but not so much for actual text editing. For text editing, the keyboard shortcuts in Emacs are usually just a modifier key plus an alphanumeric key.

I believe that pressing two keys at once takes the same amount of time as pressing one key. I call both "a keypress". On the other hand, pressing three keys consecutively takes three times longer than pressing three keys at once because you have to depress one key, lift up your finger, then depress the next key, lift your finger and then depress the next key and lift your finger. Therefore the actual speed of editing is determined by the number of consecutive keypresses to get something done, not the total number of keys depressed. As an example, I often press C-c C-, s (Ctrl-C Ctrl-, s) to insert a code block in org mode which is three consecutive key presses and a total of four keys (I hold Ctrl down from the time I start pressing c until I have finished pressing ,). This could have been faster if it involved fewer consecutive keypresses even with the same amount of totalt keys, like for instance the keypress C-M-S-, (Ctrl-Alt-Shift-,) which is just one keypress, but also four total keys. My point is that when thinking about the speed of editing, it is the number of consecutive keypresses that matter, not how many keys you press at once (although fewer is less akward for the hand).

In this blog post, I will use Emacs symbols for keybindings in Emacs where C = Ctrl, M = Meta (Alt or press and release Esc), S = Shift and s = super (Windows/Tux/Purism/Apple Commmand Key/…). Emacs keybindings are generally written with a dash when pressed together and with a space in between when consecutive, for instance M-b C-p means Alt and b pressed at the same time and then Ctrl and p pressed at the same time. When the same modifier key is used for two consecutive key presses, it does not have to be released like the example above with C-c C-, s shows. When writing about Vim, I will use Esc for Escape and use no space between consecutive keypresses which is the usual vim way, for exampel 2gj for the three consecutive keypresses 2, g and j. All other glyphs just represent themselves.

Comparison of keypresses for common editing tasks

Before starting to write anything in vim, you have to press i (or a or o etc). In Emacs, you just start writing. That's one keypress for vim and zero for Emacs. After you have written a sentence, maybe you want to move back and do an edit. Let's say you want to move to the start of the sentence you just wrote and add something in front of the first sentence. In vim you would press Esc to get to normal mode and then 0 to move to the start of the line (not necessarily the line you see on screen, but the start of the chunk of text since the last newline character) and then you would have to press i. In Emacs, you would press C-a (or M-a if the sentence is longer than one visual line) and start to write. In vim, you would need three consecutive key presses and in Emacs one before you started to write your new first sentence. After you have written your new sentence, maybe you want to go to the end of your paragraph and add something. In vim, you would press Esc, then A and then start to write. In Emacs, you would press C-} and then start to write. Again, vim has two consecutive keypresses and Emacs has one before writing.

Maybe we want to move two visual lines up and correct the misspelling of the word "icnonsequently" next. The word is two visual lines up, but starts four columns (four letters) to the left of where we are now. In vim, you would press Esc to get to normal mode, then 2gk (that is not one keypress, but three consecutive key presses) and then 3h (two key presses), then d, l, i and c, which is 1 + 3 + 2 + 1 + 1 + 1 + 1 = 10 consecutive key presses. In Emacs, you would press C-2 C-p (or C-p C-p) (two consecutive key presses to move up two visual lines since Emacs works on those), then C-2 C-b, then C-t (to use the function transpose chars with the cursor at the third letter), ie 5 consecutive key presses.

I could probably think of more examples, but the point is that for most edits, Emacs demands fewer consecutive key presses than vim. You can check out Emacs Rocks' episode 3 and 4 where Emacs is faster than vim for a couple of Vim golf tasks. Protesilaos Stavrou has also made a video where he shows off Emacs Macros for solving Vim Golf tasks. I think most of these are faster than vim as well. There are probably more examples floating around the internet if you care to find them.

How can vim be slower than Emacs? Everyone think it is the opposite…

The reason why we get more consecutive key presses and spend more time to get from one point to another and start writing in vim is because of the modal editing. Since we constantly have to hit Esc to get to normal mode before moving around and then hit a, o, i, r, c, A, O, C or I before actually getting to start typing our text, we generally need two or at least one (when combining movement and getting into insert mode like with A) more consecutive keypresses for moving around and starting to write in vim than in Emacs. There are also more functions in Emacs for editing text, many of which are bound to simple key combos like the function transpose-chars that we used above which further saves keypresses compared to manually deleting a character, moving and then adding the same carachter one letter later in the word. Even if you count the total number of keys depressed and disregard that it is faster to press two keys at once than to press the same two keys after each other, Emacs usually ends up with fewer total number of keys pressed for doing the same edits.

Modal editing, as shown above, is slower since you have to go in and out of modes. In addition, it is very unlike any workflow most of us have ever used before, which means that it takes more time to learn text editing in vim than to learn text editing in Emacs. As someone who has spent time learning both vim and Emacs, I found it a much steeper learning curve to learn vim for text editing. Emacs can also do everything else, so you can continue to learn Emacs after your muscle memory has adjusted to its text editing workflow and that is the reason why many people think Emacs is hard to learn, but for text editing, it is actually much easier to learn than vim since the way you edit text in Emacs is less weird.

Both vim and Emacs have good tutorials that you should spend some time with when trying them out for the first time, but for vim, I felt like I had to go back to the tutorial a lot of times before I really mastered the concepts of modal editing. When I finally made my own vim cheat sheet for my Norwegian keyboard layout and hung it on the wall underneath my screen, I finally started to get its keyboard shortcuts into my muscle memory. (I published that cheat sheet here, but realized I did not actually have the right to base my sheet on the model I used since it was not published under a free culture license, so I had to remove it.) With Emacs, I felt that the text editing keyboard shortcuts were more intuitive and easier to learn and remember, although I have made a cheat sheet that I plan to laminate soon and hang where my vim cheat sheet used to hang before.

Ok, so Emacs is faster for text editing, but what about…

Editing remotely

Emacs has built in functionality called TRAMP which lets you edit files on remote servers through SSH from the comfort of your own, well-configured Emacs. If you use Vim on the remote server through SSH, you get the configuration of vim from the server which is probably vanilla vim which might feel foreign if you have spent some time editing your vim configuration. Or you could spend time transferring your config over to the server. There is nothing stopping you from running terminal Emacs from the remote server through SSH if you absolutely must, but you get the same disadvantage as with a remote vim that you do not get to use your own configuration. TRAMP is the elegant solution to this problem.

Lightweight footprint

Vim is usually used in the terminal (even though gVim exists) and Emacs is usually used as an X program (with Wayland support probably landing in version 29). This makes typical vim more minimal than typical Emacs. This is true. However, Emacs works perfectly well in a terminal as well. Both vim and Emacs started out as terminal line editors (ed and Teco) in the 1970s that over time morphed into the programs we know and love today, so even if they both have GTK X11 variants, the terminal is where they both grew up. You loose the ability to have different fonts in different modes and you loose the ability to show pictures inside buffers, but othwerise, Emacs runs really well in a terminal. The X clipboard needs a package for integration into Emacs if you use it in the terminal (just like vim's registers do not correspond with the X clipboard without some configuration except if you have installed gVim and use terminal vim based on it) or you could use C-S-c and C-S-v which most terminals use today to copy and paste (as in vim) if you don't mind changing your habits. If you use many custom keybindings including C-m or C-i, you probably want to rebind those to something else since they have special meaning in a terminal. Or you could configure your terminal to treat those keys differently.

All that being said, Emacs is heavier than vim because it includes a lot of built in functionality that vim does not have. This might be good or it might be bad depending on your point of view. I recently spent some time in Emacs on the tty of my Raspberry Pi 3 B+ and surfed the web with Emacs' built in eww browser to check something in the Debian Wiki quickly before continuing to configure my minimal Debian system. I checked "free -m" from Emacs' ANSI term (one of the many built in terminal emulators inside Emacs) and I used 128 MB of RAM with three buffers open: eww, ANSI term and a scratch buffer. You could use W3M and vim alternating to achieve the same, but I think the few extra MBs of RAM and SSD space Emacs takes is worth it to get to do it all within one program (even when used from a tty).

Emacs might be fast at editing, but it starts slowly

Emacs can be slow to start if you have a lot of stuff in your config. The recommended way to avoid this (both for terminal use and graphical use) is to use Emacs as a server that starts up with either a systemd unit or other init script or you can start it with your Window Manager or Desktop Environment at startup. Whenever you then want to use Emacs, you should use "emacsclient" to open a window to the already running Emacs server process. This makes startup of your Emacs window ("frame" in Emacs terminology) or terminal window very fast. I have bound "emacsclient -c -n" to Super+F6 in my Sway config to fast get a new window with Emacs whenever I need one.

Ergonomics

This is where vim shines, especially if you rebind Esc to Caps Lock which many people recommend for vim use. Modal editing means that you can do all your edits with keyboard shortcuts that are generally just Esc to get to Normal mode and then one alphanumeric key for each command, unlike in Emacs where you have to use one or more modifiers plus an alphanumeric key to get the same thing done. In text editing, the key chords are simple in Emacs, but for other workflows, there are some crazy keychords. When you learn that you can keep Ctrl depressed if you have two consecutive keypresses which contain it, that helps. Over time, most key chords you use often become muscle memory and if you want to rebind crazy keybindings you use often, that is easy to do. Many recommend rebinding Ctrl to Caps Lock for Emacs use since then you have ctrl in a more natural position for you left pinky. I think ergonomics is the real reason why many prefer vim. It is not faster to press more consecutive keypresses even if there are fewer keys in each, but it is more ergonomic. It is probably also the reason some Emacs users use evil mode and that it exists in the first place. Since Emacs is an Elisp interpreter, you can do things like that, but using Emacs keybindings in vim is not possible since vim isn't as hackable as Emacs.

Unix philosophy

Emacs does one thing well and that is to interpret Emacs Lisp. It does not matter if you edit text, chat on IRC, read and reply to email or look at PDFs while taking notes in Org Mode, it is all done by executing Emacs Lisp. Many Emacs users use it as a program that integrates many tasks they do into one cohesive workflow. Even if Emacs can become all-encompassing, it does not have to. You can just use it as a text editor if you want. Today, vim both interprets VimScript and edits text and NeoVim does the same plus interprets Lua and is embeddable. Emacs just interprets Emacs Lisp. Maybe you need a bit of mental gymnastics to get any of these programs to fit the "a small program that does one thing well" philosophy?

Conclusion

The myth that Emacs is good at anything except text editing is a myth, not a fact. It is a slightly faster text editor than vim and it is much easier to learn to edit text in because you do not have to grasp the concept of modal editing. Even so, how many key presses you need to get things done and how much time it takes might be less important to you than other factors when you choose your text editor, especially since most time is probably spent thinking. How inspired you feel by the software might be more important to you than the speed of editing. Maybe you prefer to separate different functionality into different programs and configure all of them to use vim binding or maybe you like to integrate them all into a hypermoldable, super-hackable Emacs Lisp interpreter. Or maybe it is important for you to use the same software as your colleagues, friends or fellow students to be able to share their workflow tweaks and take part in a shared culture?

I think both (Neo)vim and Emacs are good choices for investing some time into learning since they both have strong communities that will make sure they will be around for fitfy years more (unlike some editors, both FOSS and non-free, that are developed by companies or individuals without communtiy participation and may be abondoned at any time, like Atom exemplified recently). Both editors demand a bit of configuration which is a slightly different approach than many of the newer editors that are more ready out of the box experiences, but might not be as flexible to adjust to your workflow (as opposed to you adjusting to theirs). In the end, you choose the tools that work the best for you. Just don't lie and say you use vim because it is faster when you haven't even given Emacs the chance to convince you otherwise.



All content is shared under the terms of the Creative Commons Attribution-ShareAlike license.