Evil Emacs, best of both worlds?

First, let’s get this out of the way. This is not a rant against Vim, or Emacs, for that matter. I am not interested in joining the epic religious battle between the two. In fact, if you are not using either one, I highly recommend learning one of them. Both have steep learning learning curves but the payoff down the road is tremendous. Like Baron Schwartz articulated so well in his post on Sublime: if you are not using a powerful text editor, get one. I’d suggest Vim, simply because Vim is a great *editor* and your hands/fingers will thank you by the time you retire ;)

With that said, let’s get to the subject of this post.

I’ve been a long time Vim user. I put in some real effort learning Vim around 2004, and it took some time before it really started to pay off. Even after I was comfortable with entering text and doing basic movements, it took another good few years to learn, appreciate, and then form a habit of using many of its powerful command combinations. In fact, the learning never ends. Just a few months ago, I learned a neat trick to comment out selection of code and the Pathogen plugin management.

To encourage and hopefully help a Vim beginner, here are the commands (the ones that come to mind now) that I use most often. I didn’t include the motion commands (w b e 0 $ g gg G and such) because those are the basics. But they are incredibly important because they can and should be combined with other commands. For a beginner, I suggest you print our a cheat sheet and put it next to your monitor so you can consult by rolling your eyeballs. If I miss certain super useful commands here, please comment away.

  • The dot (.) command, which repeats your last action. This sounds simple but is incredibly powerful after you get used to it. For instance, if the last action is dw (delete a word), simply pressing . deletes another one!
  • The f motion command. While in Normal mode, typing fa moves the cursor to the next occurrence of the letter a. Replace a with other keys you desire.
  • The c (change text) and d (delete text) command. They both start in Normal mode, but the former lands you in insert mode so you can start entering text, whereas the later keeps you inside Normal mode, ready for the next motion or command.
  • The power of combination. The whole is greater than the sum of its parts! I often combine c (or d) together with f (or t) commands. I also combine c (or d) with w very often. I often do something like c3w, which deletes (change) the next 3 words and change into Insert mode, ready for me to enter text at the current cursor location. I use cG or dG quite often as well.
  • Powerful search with the forward slash while in Normal mode. Just press forward slash and then start typing the term you are looking for. It can do regex search as well. After entering the search term, press the enter key and it will land at the first occurrence of the term after the present location. Press n to search forward, N to search backward for the same term. Another trick I use often is the * command. While in Normal mode, press * will search for the term that the cursor happens to be in, then press n/N to find the next/previous occurrence;
  • I use the sp (or vs) often, to see multiple source files (or even different section(s) of the same file). sp splits the current window horizontally, vs (vertical split) splits vertically.
  • I often use the o (or O) command to create a new line.
  • I am aware of the mark (mark the current spot in the file so you can jump back to that location quickly), but I haven’t used it often. I need to make a conscious effort to form a habit of doing that. This is how it is done: say you are at the bottom of your code file but you need to make a fix to a function at the beginning of the file before you can continue, you can do ma (can be mb, mc etc up to mz). Now go to the top of the file and make the change. When done, press ‘a and you will be back at where you were earlier.
  • Macros. I use this quite often and really enjoy this powerful function. Here is an example. Say I am composing an IN clause of a list of countries for a SQL statement. Say I copied a list of 26 countries from an email a friend sent me:
    Albania
    Belgium
    China
    Denmark

    Zambia

    Now I need to surround each country with single quote, put a comma after the closing quote (except the last one). I normally record a Vim macro for this. Macro is a recording of the sequence of my actions that I can apply to other text later. Here is how I do it:
    1. Make sure the cursor is in the first line, anywhere is fine, Normal mode;
    2. Start recording by pressing letter q. Then I will pick a letter, say a, which is my macro’s name that I can call later;
    3. Press I, which means (I)nsert at the beginning of the line, another thing I use very often;
    4. Enter ‘, the single quote, then press Esc so I am back to Normal and ready for next motion;
    5. Press A, which means (A)ppend at the end of the line, another thing I use very often;
    6. Enter ‘, the single quote, then press Esc so I am back to Normal;
    7. Press j, now I am at the line that says Belgium. I will press q again to indicate I am done recording the macro;
    8. We are still in Normal mode, now it is time to play back the actions I took for Albania and apply them to all other countries;
    9. Type 25@a, now the rest of the 25 countries will be quoted properly with a comma at the end. Just remember to remove the last comma for the last country, otherwise the SQL cannot parse. If you need to bring all the countries in one line, just press J (yes, upper case j).
    Now this looks like a lot of steps, but in reality it is pretty fast and very efficient once you get the hang of it.

Ok, now where are we? That’s a long detour. Let’s get back to the subject of this post, if it has one!

Right, although I have been and remain happy with Vim, I am aware of the editor war between Vim and Emacs and am curious about Emacs’s capabilities. When attending various conferences/user group meetings, I tried to find an Emacs user who could show me the capabilities of Emacs. Yeah there are articles and screencasts on the web, but talking to a real person is nice. I really didn’t come across many Emacs users in those meetings/conferences I attended. Therefore my understanding of Emacs remained shallow. My impression, which could be wrong, is that there are way more Vim users out there than Emacs users. Even if we combine Vim and Emacs users, I bet that it is still a tiny minority in the entire population who have easy access to a computer.

My long lasting curiosity toward Emacs was finally satisfied when I started taking Coursera courses, because two of them require the students to give Emacs a try. The first course is Dan Grossman’s Programming Languages, highly, highly recommended. The second one is Startup Engineering, still ongoing at this writing. Since I mentioned Coursera, why not take another detour. Let me bring navel-gazing to a whole new level while I am at it. Brownie points to whomever reached this far, entirely non-redeemable!

If you are interested in learning and improving yourself but haven’t heard of MOOCs, I highly recommend you checking them out. The big ones are Coursera, Udacity, and edX. Coursera is the one that gets the most buzz and has the most comprehensive offering of courses at the moment, but I heard people say there are good ones from Udacity and edX as well. So far I’ve registered 6 or 7 courses and finished 3. I am about to finish the fourth one, Introduction to Systematic Program Design – Part 1, another fantastic course. I hope the MOOCs are continuing to thrive. No doubt they will attract a lot of VC interests but I hope they can keep true to their original spirit: to bring high quality education to people all over the world free of charge (or very cheaply to local people).

The first part of Professor Dan Grossman’s Programming Languages introduces the students to ML. As a requirement, I loaded Emacs on my Windows machine and installed ML. One thing that I can really appreciate fairly quickly was the tight integration of the editor window with the SML REPL. That is really super convenient, effective, and efficient.

Then Google announced that it would cease the Google Reader service. Seeing the tight integration of Emacs with many external tools such as newsreader, IRC chat, email, and such, I kept Emacs on my workstation, and have played with various tools, but haven’t settled on anything yet. It is my intention to come back to it and get rid of my Feedly account after Google Reader was gone.

Then Coursera Startup Engineering course started, I’ve watched the video on the demonstration of coding JavaScript inside Emacs. Once again, the tight integration of the editor with the REPL impressed me. I will go back and re-watch that video to learn all those tips. I’ve tried using tmux in combination with Vim for similar effect, but in my limited experience, I think Emacs integration is superior. In fact, if you work in *nix environment, I would hazard a guess that the combination of Terminator and Vim works better in more cases than tmux and Vim.

After using Emacs here and there, it is clear that I really miss the editor power of Vim. Thankfully, Evil package does a fantastic job of emulating Vim editing capabilities. Even macro works! The only drawback I noticed so far is while in Normal mode, the ( and ) keys in Vim moves the cursor to the next/previous sentence. I use that often, but under Evil, the command moves a paragraph, not a sentence.

Another interesting note is gVim on Windows 7 (I haven’t tested it on Windows 8) cannot display Chinese input on an English version of Windows 7. I haven’t bothered to figure out why, but it works fine on Emacs.

So now I am using Evil on Emacs, and have been happy so far. My next goal is to check out Emacs integration with PowerShell, since that is the language I am using for a project I am working on. In writing this, I need to remind myself that there is a balance between spending time to get your tools right versus spending time producing things.

Leave a Comment