Pros and Cons
It’s the year 2020 and there are now countless ways in which you can write software. Gone are the days of monochromatic text editors and manual indenting of your code, so why is it still worth learning vim (vi improved)?
First, not having to use the mouse is way more convenient than I ever imagined. The ability to quickly reach any key from having your fingers set on the home row [ASDFGHJKL] is (once you get used to it) much less error-prone. I think in my case this comes from the fact that, if I am to move my hand from the keyboard to the mouse, I lose track of what keys each finger is assigned to when returning to the keyboard. This then requires a mental reset before I begin typing anything again. Having all fingers situated on the home row also encourages the use of all ten fingers when typing, which is something I’ve never really done before trying out vim. Often in the past, I’ve found myself using six or seven of my fingers as that is just the way I learned to type. When I first started it was rather comical how weak my pinky fingers felt.
Second, technology moves rapidly these days with new IDEs, new languages, new coding conventions being released on a monthly basis. There always seems to be a new trend in the industry demanding your attention, which to me is comparable to the fashion industry. You can spend a fortune on a designer t-shirt only for it to be in the sale a month later because that designer is no longer in fashion, much like the way you can invest hours of your time learning the trendy new piece of technology or the hottest new IDE, only for the new kid on the block to steal it’s thunder three months down the line. The one thing that remains constant among all of this, however, is vim. It is on almost every machine no matter how barebones it is, which can be very handy when using SSH, it is adopted as one of the best text editors to stand the test of time (along with emacs) and the fact that I am even writing this blog post, questioning whether people should still learn a 28-year-old piece of technology, maybe answers the question itself.
Now I’m not advocating for the sole use of vim, I do think there are many situations where it is better to use a fully-fledged heavyweight IDE. The motivation for me currently learning how to use vim is not to replace the use of any IDE, it is simply to aid the user. Like with everything, there are use cases that fit one piece of technology better than another, and there are downsides of taking the leap into the vim world.
It is a completely different style of text editor from what I was used to—figuring out the movement alone was a big learning curve. If you do not have any issues with the way you currently work then I realise it’s a big step back in order to start again with a brand new way of working. Due to me being new to the industry, and with free time on my hands outside of work, I felt that this was the ideal time for me to sacrifice productivity temporarily in order to learn this. There is a fear that out of stubbornness I may end up using vim in a situation where it isn’t the best tool for the job, which wasn’t the aim for learning it in the first place, so that could be a potential downside further down the line, and also something to keep in mind.
Another downside is the fact that in the last three weeks, I have had so many occasions that I wanted to physically break my fingers from pressing the wrong key and/or being in the wrong mode. To say learning vim is frustrating is a significant understatement and some may say it’s unnecessary emotional pain and misery. To me this is part of the fun though, it makes the outcome (whenever I become proficient at using it) far more satisfying. If something is easy to do then it often isn’t worth doing at all.
My journey with vim so far…
To fit the stereotype, I started by learning how to actually exit the thing! This may be the most notorious part of vim. Someone once asked me how to get an unbreakable password and then they answered: “put a new user in vim, ask them to exit, and record the keys they try”. I finally understood this joke when I tried it myself. There’s a few methods to exiting such as :q, :wq, :q! and ZZ. You can see what all of these mean if you do a quick google for a vim cheat sheet.
The first step it made sense to get my head around was the movement. This is something I’m still finding tricky at times. Because Vim works off the home row [ASDFGHJKL] for movement, to navigate you use; h (left), l (right), j (down) and k (up). You can also use the arrow keys for this same movement, but that’s NOT the vim way. In order to get to grips with navigating in vim, I spent a few hours playing a game online in which you move the character around using hjkl initially. At first this was hilarious. I had absolutely no control of what I was doing. The situation improved quickly though and after a few minutes I was somewhat competent. The only thing I’m still struggling with movement-wise is the fact that ‘j’ is down and ‘k’ is up. This is still confusing for my fingers.
Once I got to a reasonable point with the four movement keys, I move onto jumping entire words at a time. The keys which do this are; w (start of next word), b (beginning of the previous word) and e (end of the next word). These took a bit more getting used to because. It’s obvious whether you want to move up down left or right, and doesn’t require a lot of conscious decision making on top of knowing which key to press. However, when jumping to the beginning or end of a word ahead or before where the current location is takes a bit more thought. This means, for me anyway, it’s a bit more of a mental two-step process to move around this way, something I’m sure will get better with time. You can also jump to the top of a file using gg and the end line of a file using G.
On getting the basic movement down, I thought I’d move on to the fundamentals of vim. In general, the order of operation is the count (number of times you want the action repeating) followed by the operator command (‘d’ for example) and then the motion command if needed (‘h’, ‘j’, ‘k’ or ‘l’ for example). This quickly becomes very powerful where you can delete, copy, reformat multiple lines, words, characters.
The next thing I stumbled upon by chance was the ability to do actions in line so repeating a command (d for delete) would act on the current line you were on. This means dd deletes the current line and yy yanks (copies) the current line you’re on. When I found this I soon realised (for reasons you can imagine) that it was important to know how to undo and redo actions. This is very simple and can be done using u (undo) and ctrl+r (redo). All these shortcuts are pretty powerful and this is when I started considering that learning vim may not actually be a waste of time, especially when writing code.
Along the way of getting my head around all of these actions you can do, I accidentally stumbled across a lot of things that are quite neat and useful but also very confusing at first, like ‘$’ going to the end of a line and ‘gwgw‘ formatting a line so it doesn’t ever exceed 72 characters. Some of these things I still don’t really understand but it just makes me appreciate how easy it is to undo things. I have also gained a lot of productivity by using the ‘/‘ command followed by a word or set of characters that you would like to search for. This then takes you straight to the next instance of whatever you searched for. I’ve found this to be a quick way to jump around the file you’re in.
I understand that I am only a rookie at the moment with only a few weeks’ experience and I’m sure that some people reading this will be far more proficient at using vim than I am. The things you can do with the tool extends far beyond what I have listed above and I barely have my foot in the door to what is truly possible.
In vim you also have six main modes:
- Normal: used for the editor commands such as navigation and deletion etc. Also the default mode
- Visual like normal mode but used to highlight areas of text so you can run commands on the highlighted areas
- Select: similar to visual mode but if a character or newline is entered then it adds this and enters insert mode
- Insert: just like a standard editor, much like notepad without the mouse
- Command-line: allows for a single line command input at the bottom of the screen, usually starting with ‘:‘
- Ex: like command-line mode but doesn’t exit the mode when a command is executed
The modes I have used so far are mainly normal, insert and visual.
Finally, a story about getting this blog post uploaded.
So I thought that, to be true to the spirit of this article, I’d write the whole thing using vim. No add ons or anything like that, just barebones vim. This was all fun and games until, upon completing the blog post, it came to putting it into the blog website. I assumed I’d be able to copy the whole thing and paste it into a Google doc or straight to the blog post website—how wrong I was. The clipboard registers that vim uses are not the same as the system clipboard. I entered visual mode, hit ‘gg’ to select all the text in the document, then ‘y’ to yank (copy) the whole thing. Then I left vim, full of hope, opened up a Google doc and hit paste and what appeared was the link to an article titled ‘best breed of dog to buy in 2020’ (don’t ask). Upon consulting my colleague (and avid vim user), I found out that vim needs to be configured in order to use the external clipboard, something I thought would be fairly straightforward, although he did warn me. I then ended up in a complete rabbit hole of stack overflow pages and vim forums about the issue. Different systems have different numbers of clipboards and different ways of accessing these clipboards so this was an added complication. It seems ludicrous that this isn’t the default, but alas there I was. There is a setting in the vim config named ‘clipboard’ which is disabled by default. In the vim config file I added a line that enabled this. That doesn’t sound very difficult I know, but on my way to this solution I found a lot of ‘fixes’ that didn’t work for me and I essentially just kept digging a deeper hole. The point of this anecdote is to demonstrate that, maybe, once vim is configured to how you want and it’s all running smoothly, things get much more productive, however even the simplest of tasks (by today’s standards) have added complications. This is something that you don’t usually have to contend with when opening an IDE straight out the box, and it’s definitely something to keep in mind when considering adding vim into your workstation.
To conclude, despite barely even scratching the surface of using vim, I’m starting to enjoy it a. I can see how things become much more convenient and it seems to make sense to me now. Despite this, I don’t ever think I’ll throw out the new age IDEs available because there are things they do which are far too useful—like refactoring for one example. If this blog post has somehow, in any way, captured your imagination then I’d encourage you to try it out. Even if you don’t ever use it again, it is definitely worth trying out. Learning vim, to me, was more about the mental exercise and having an open mind which is something I’m actively trying to work on and I think is an important skill in software development. You could say I was venturing outside of my technological comfort zone. Vim is more than likely already installed on your machine as you read this, however you may never have even thought about opening it. You should open it up now and type something, or even quit it straight away (if you remember how to).