I'll show you why I love Vim and why I switched to Emacs and why you might too.
Vim is really really awesome. Inspired by a lot of great hackers who swear by Vim (like Bram Moolenaar and Vim Diesel) and by the greatest ode to a text editor ever ("Vim Creep"), eight months ago I decided to take the plunge.
First I did
vimtutor at home, then I installed VimEmu in Visual Studio (I programmed C# at the time), I printed the Vim cheat sheet, and broke up with my mouse.
Against the advice in the excellent video Learning Vim in a week (see below) I did start at work.
Work is the place where I edit the most text and I needed to do some boring work at the time. I have no regrets learning Vim there. Within a week my speed was back to my average before that. I became slower in certain parts but faster in others.
More importantly, boring, repetitive tasks were no longer boring and repetitive. In Vim, writing boring code is seriously fun because you stretch your mind on creating efficient text manipulations. I stayed interested doing the work, and I learned Vim. In that period both my employer and myself have benefited from me learning Vim at work.
Eight months later I use Vim everywhere. Nowadays even a mundane task as updating my TODO list is exhilarating.
The following example of the awesomeness of Vim is based on a real life situation where I wanted to unroll a T-SQL loop. Suppose you want to create four separate update statements from the four table names (First, Second, Third, Fourth) and columns above the EXECUTE block below:
INSERT INTO @TABLES_COLUMNS (TABLE_NAME, COLUMN_NAME) VALUES ('[dbo].[First]', '[Yadadada]'), ('[dbo].[Second]', '[Blabla]'), ('[dbo].[Third]', '[Abacadabra]'), ('[dbo].[Fourth]', '[Yeehah]'), (...) WHILE 1=1 /* Set the variables of table name and column name (substitute with VALUES above) */ EXECUTE ( 'UPDATE ' + @TABLE_NAME + ' ' + 'SET ' + @COLUMN_NAME + ' = temp.OLD 'FROM ' + @TABLE_NAME + 'AS t ' + 'INNER JOIN #temp AS temp ' + 'ON t.' + @COLUMN_NAME + ' = temp.NEW' )
(Note that this code is a bit simplified: the setting of the loop variables and extra values are skipped.)
In the real life example this was based on there were about 15 of those substitutions instead of four. This is what I would consider a boring task. Since the task is short enough that writing a script to automate it is slower than actually doing it, in normal (non-Vim) life I would to do this by hand.
What needs to be done above is:
I would have to do this about 15 times. I think this would take me about 10 minutes.
With Vim it is different.
I recorded the operations on the text once, and then I executed the operation I just did on the next two variable values. Then on the next, then on the next. This is programming your text. And it is so easy. You should probably see the recording and execution of the macro to understand:
Note: after recording I see that showing these keypresses doesn't really help since a lot of them are missed and you cannot easily see the difference between lower and upper case. :) Apologies.
We see lots of missed efficiencies and some wrong keypresses. It is a dirty recording. But life in Vim is messy. Editing code with a mouse doesn't go right the first time either.
What we see in the gif above is that first I remove the no longer necessary EXECUTE statement after easily having jumped to the end of the file (G). (Note that navigating in Vim goes with h, j, k and l – you never have to leave your home row.) I visually select the statement with something like V5k (visually select five lines [V] upwards [k]).
In Vim it is as if you speak to your computer: you say a verb, you say how many times and you say what.
Then I substitute the "'" and "+" from the selected region with a command (:) with nothing globally:
Next I delete the selection and I yank the UPDATE statement plus one blank line in register u ("ud [delete into register u]). Now I navigate to the top of the page (gg) and start a recording in register a (qa) (I pick register a for no other reason than that it is close to q). I navigate down two lines (2j), find the first quote (f') of the first variable and yank (copy) everything inside the quotes (') into register f ("fyi'). Next I go to the end of the line ($) and copy the second variable into register s. (I use register f for first and s for second.) So now we have the table name we want to substitute stored in register f and the column name in register s.
Then I go to the end of the file (G), paste everything inside register u ("up). This way the UPDATE-statement comes at the bottom of the page. i Next I visually select the word of the table name variable and paste the f register over it ("fp) (so: vw"fp). I also substitute the column name for the new value inside register s.
After having substituted all the column names and table names variables for the new values, I jump to the top of the file and go two lines down (gg2k). I remove the line (dd), jump back to the top (gg) and stop recording (q).
Next I execute that what I have just recorded three times (3@a)! Since the recording was on a meta-level of navigation, selection and substitution this works perfectly fine for the other variable names. Each time copying the statement, removing and substituting the values of the table and column name and ending on the top of the file. The whole file is done. This would have worked just as fast for 100 rows by the way.
I find these possibilities so cool.
/edit As Veedrac showed in a comment on reddit in Sublime Text this refactoring is also possible and arguably more intuitive using multiple cursors. Vedrac made a video where multiple cursors are used to do the same refactoring. This video can be watched here.
Note that the jumping around in Vim and selecting text is usually the biggest time saver. As JangoSteve writes in the comments on Hacker News in response to the article Vim Creep:
vim doesn't save you five minutes, twenty times a week; it saves you 0.75 seconds, eight thousand times a week. Same time savings, but much harder notice the pain before you've solved it.
And indeed the most time savings are just all the basic movements where you would have normally grabbed your mouse repeatedly. But Vim is also absolutely wonderful for these large refactorings as above.
To be honest, when I recorded the macro for the first time (in the real life thing where the example was based on) it only really worked the tenth time I tried. (In my defense: it was during a late night cowboy deploy that went horribly wrong because of a query that performed a tad bit worse on production than acceptance and there were people watching my actions via the remote which gave me performance anxiety.) But finally it did succeed and I learned from it and this time while recreating it I got it in one go. In the excellent book Learning the vi and Vim Editors something like this is said about forcing yourself to learn efficiency gains: first some action or learning a regex will take you some time, but this pays back many times over the next time when hyper-efficient text-editing has become embedded in your muscle memory.
Note that one of the lessons of the Learning Vim in a Week video above is that "There is always a better way." Typing this I see I could have better substituted the variable names in a similar manner as how I substituted the quotes and plus from the full statement. Also I used B or W instead of b and w to jump Backwards or forwards a big Word (including the @). So there was indeed a better way with less keypresses. There surely is even a (much) better way.
Furthermore, the fact that I am now recreating this situation shows how much fun it is. I really don't mind.
I got a bit carried away already, but Vim is awesome. Some of the things we have seen is easily navigating with the keyboard, executing commands over lines of text, storing stuff in multiple registers, editing selecting or deleting specific blocks of text, and recording a macro to re-execute a sequence of keypresses.
This is made possible by different modes that are available in Vim: normal mode for navigation, insert mode for inserting text, command mode for executing a command and visual mode for selection.
Some more awesomeness that is not shown in the example above are for example:
If you want to jump into Vim I can advice you to watch the video on Learning Vim in a Week above and do the
Stick with it. You will not regret it.
I love Lisp and it turns out that for Lisp editing it is somewhat more logical to use Emacs (although for example the expert Lisper Doug Hoyte swears by vi – the non-improved version of Vim). The reason for that is it has tight integration with the language and you can use a version of Lisp called ELisp to configure and modify the editor. (Note that some people get really carried away with editing their editor instead of editing arguably more useful code).
But even before I used Emacs to learn the Lisp-dialect Clojure I had already switched to it. The reason for that was the video "Evil Mode: Or, How I Learned to Stop Worrying and Love Emacs":
(Warning: it is a bit long.)
Evil Mode is an Extensible vi layer for Emacs. This means you can have Vim keybindings while having the Emacs environment.
Why would you want that? Well, Emacs is a much better environment than Vim. Some people call it an operating system instead of a text-editor. And it is true. Where Vim will sometimes hang when performing large operations, Emacs just runs on easily.
In the video above examples are shown how things that would block normal Vim can sometimes go on in Emacs on a background thread. And for me Emacs with Evil mode just feels lighter than Vim. There are some other attempts to improve the performance of Vim (notably NeoVim), but this would still not have the Lisp integration Emacs has.
Furthermore, things like searching and replacing are better. You visually see in the text what will change while a substitution command is typed. And search matches light up before you finished typing your search. Examples of search and replace are shown in the video at the end. And when you start typing in the command area it automatically starts showing suggestions. There are a lot of these minor things that make Emacs with Evil mode very pleasurable.
Because of the Lisp integration (firing up a REPL and never having to leave Emacs), nice quirks as command completion, and the better performance I use Emacs.
To configure Emacs you can edit a .emacs file or the ~/.emacs.d/init.el file. Evil mode provides a lot of ways to hook into the execution flow and plugins. For editing Clojure I just use the Emacs setup as described in Clojure for the Brave and True extended with Evil mode.
Some of the non-Evil features I use for editing Clojure are mentioned there. Things like CTRL-c CTRL-k to compile code and CTRL-c CTRL-d CTRL-d to view the library definition (if I am not mistaken, fact of the matter is the keybindings for Emacs to me are much more unlogical than those of Vim).
Now I will visually show some of the other awesomeness, but related to Evil mode. Just to give an impression.
Redoing things with the .-command work just as in Vim:
Above we see the . command used to change text inside the parentheses " (ci").
Next I have bound CTRL-b to display all available buffers and easily switch between them. This is done in such a way you can use the familiar hjkl to navigate.
And we can display multiple windows vertically (CTRL-W v) or split them horizontally (CTRl-W s):
(Since I did not want to not use these buffers at all, note that we made a selection upper case with U and it changed in all views of the buffer simultaneously.)
Another really awesome thing which I believe comes out of the box is undolist, with it you can visually navigate the undo history. Yes, you can view the branches you decided to delete. Undo history can easily be viewed and navigated by typing the command:
How cool is that?
And we can add a very rich set of plugins to Emacs. Of course we have all the plugins added for Clojure by the author of Clojure for the Brave and True. Which provide syntax highlighting and tight integration with documentation, the REPL and executing code.
I'll now show two other plugins I find very interesting.
We can display the file system on the left if we want with F8 by using NeoTree:
Above we see me pressing F8 and navigating to an uninteresting folder on my file system.
(Note that the five lines are combine with 5J [J is for joining two lines] – another feature I wanted to force into this).
Slurping on one end or the other or barfing things out on one side of the other is a feature of ParEdit. ParEdit is helpful for manipulating S-expressions (the parentheses in Lisp) but can also be used for other structures like the quotes above.
Is everything rose shine and moon light? No. I have identified the following shortcomings so far:
That's all I can come up with and I have thought really hard.
I think Emacs Evil mode is a very nice environment to edit text. I hope I was able to show some of that by the examples. If you want some more of Evil mode I can recommend this great demonstration:
You can download the Clojure for the Brave and True emac setup which I extended with Evil mode as descibed above by clicking here. If you install Emacs and place the repository in the ~/.emacs.d directory everything will install automatically. Note that there are probably better dotfiles out there. The way to install plain Vim can be found here.
One final note: if you start using Vim do not forget to remap your Capslock to Escape. You will be pressing Escape a lot and on todays keyboards it is too faraway from the homerow.
Thank you for reading. Comments are welcome.