Editor’s note: This is the first in a series of introductory posts on Vim.
What is Vim?
Vim is a text editor. If you’re a computer user who dabbles in code, but is not primarily a programmer – like a data scientist, or a maybe even a blogger – then you may have heard of Vim, but never figured it would be useful enough, or worth the effort to learn.
What is a text editor?
It’s a program that edits text. Which sounds really self-explanatory, but I heard the term a lot at first before the obviousness of it sank in.
Text is literally just what your keyboard types into a computer. Microsoft word is a text editor of sorts (technically it’s a word processor). It’s a highly functional text editor that automatically formats your text as you type, and does all kinds of other crazy things automatically. Ordinary text editors just input the basic text with no formatting (fonts, colours, size, etc).
Notepad is one of the most basic text editors. It is a program where you can open a text file and input text. It will not even automatically jump to the next line if you don’t tell it to. It will not show different fonts or anything… just text.
There are lots of other text editors that also will only show you the text you’ve written with no formatting, however these editors have other functionalities that make them especially useful for doing things like writing computer programs and scripts. They all do different things which make them more or less popular amongst programmers, software engineers, web developers and other people who write computer code.
Some are purpose-built for specific applications. For example R is a programming language and environment which harbours a mind-boggling amount of statistical applications from visualizing data, to performing calculations. R has an inbuilt text editor which is useful because with the push of a button you can send your line of code from the text file you’re writing in, straight into the R command line and it will run that code.
Other more general purpose text editors are Notepad++ (a far superior and functional editor compared to the basic Notepad), Emacs (which does all sorts of crazy things beyond simply editing text, it even has games!), Text Wrangler… the list goes on.
Pretty much all of these text editors work in the way you would expect. At least their basic functionality is familiar.
There is one text editor however, unlike any other. It is among the fastest and most efficient text editor there is, for the purposes of editing text at least.
It’s called Vi.
Vi and Vim
Vim is an acronym for Vi Improved.
Which is exactly what it is.
While it is true that there are a couple of specific occasions where Vi might be more suitable. In general Vim is everything Vi is and so much more.
I think it’s important to understand that Vim was created as an extension to Vi, which was the original mover and shaker, and because what makes Vim so different, and so much more powerful for editing text than other editors, can be traced back to Vi.
Vim is fast, super fast. It is highly extensible and customizable, and it’s not that hard to learn really.
If you have never done any programming, and you opened up a text file in vim, I guarantee within seconds you’ll start panicking. Any number of things might happen depending on what keys you typed on your keyboard.
It definitely will not do what you expect. When buttons don’t work, you will move the mouse and click where you want the cursor to go, and it won’t go there. Your heart rate will start to rise. You’ll notice there’s no menu bar at the top, and your pupils will constrict slightly. About this time you’ll start to think this was a bad idea and try to get out of it, but when you realize that ESC doesn’t work, now you really start to panic. All of a sudden you can’t think properly, you start breathing heavy and you tense up.
Fear grips you.
You will start wondering why you thought this would be a good idea. Now all you want to do is get out of there. In your haste you try to close the terminal window and it prompts you with a warning that there is a program still running, the final obstacle to freedom, you hastily mash the ‘I don’t give a shit’ option, and then just sit there for a minute while you calm down.
Welcome to Vim.
After this post hopefully at a minimum, you’ll be able to get in and out of Vim without raising your blood pressure.
Hopefully, after a series of posts on this wonderful editor, what you’ll also find is that what was the source of your initial angst, becomes your favourite thing about it.
You see, Vim is not like other text editors. In fact Vim does not work like anything else that you’re used to using on a computer. You open a browser, you click where you want the cursor to sit, then you type, and it does what you want. In notepad, you see the cursor, you hit a letter, and it displays that letter on-screen. Vim doesn’t do that. Initially it’s one of the most unintuitive programs you’ll ever use.
But if you put in the time and effort at the beginning to get used to it, like you have to for anything worth doing, you’ll find that it becomes intuitive… even quite nice. You might even find after a while that it starts to feel like, not only is it faster and more efficient, but that this is the way it was supposed to be, and that other editors and the whole desktop experience has been getting it wrong all this time. It will start to feel like home, and other programs will start to feel like somewhere you visit.
After getting comfortable with vim, you might find yourself wanting to learn how to code, just so you have an excuse to use it.
So how do you do that? Get comfortable with Vim, I mean.
This post will help get you started.
Vim, the properly basic basics.
Vi is an IT veteran. It was born in a world where the mouse and the Desktop environment, as you know it today didn’t exist. It is a product of necessity.
In the days of green text on a black screen, computer geeks needed a way to quickly navigate verbose text files without a mouse.
Vi and other lightweight text editors developed during these years. Today Vi, Vim and other similar clones are ubiquitous on UNIX like systems.
What this means is that today systems such as most Linux distros and the Mac OS, come standard with vim, and basically all come standard with Vi.
If you’re a Linux user then, well for starters you probably don’t need some wannabe data scientist teaching you how to use vim, but in any case, you’re probably fairly familiar with the command line.
If you’re a Mac user, you may or may not be familiar with the Terminal. But you should at least know how to find it. If you’ve never used the terminal before then this is the tutorial I used to get me started.
If you have Linux or a Mac OS, then chances are you have Vim built right into the terminal, and it’s simply a matter of typing:
into your command line and voila… Now I need you to sit quietly for a minute while I go and help the Windows users (which is really only because I’m one of them… not by choice).
If you’re a windows user, then you’ve got a little more work to do (not much, but some).
There are multiple options for Windows users, but for now I’m only going to go through the simplest and quickest. Which is to install the gVim direct from the Vim website.
I’ll leave it up to you to work this out. It’s not that hard, and I had a working gVim installed on my Windows 10 laptop in about ten minutes (the ‘g’ just means it’s a ‘graphical interface’ version of Vim, which makes it more user-friendly, which I’ll explain more in a minute).
So gVim on windows will work a little differently to Vim inside a terminal window. Firstly, you have to go to the vim.exe program, like you do every other program in windows and double-click to open it (you can also pin it to the start menu and task bar like any other program in windows). This will open up a Vim session.
The first thing you’ll notice if you’re using Windows is that I lied to you before when I said there was no menu, because in gVim there is. You’ll also find that you can use your mouse to put the cursor where you want it to go. However, that’s about as far as the help goes… for now at least.
So now whether you’re a Linux, Mac or Windows user, you should all be up to speed. You should be looking at a window, of some description, with the Vim welcome screen in it.
Believe it or not, the welcome screen has some very helpful information on it, which in your panic and frustration, may not have noticed the first time.
But we’re going to ignore it all for now, and just focus on the basics.
So the first thing we’re going to do is get back out again by typing:
If you’re in a terminal, it will just return you to your command prompt.
If you’re a windows user it will close the gVim program, just like every other program in windows.
Congratulations, you can now get into Vim, and get out of Vim. Which is the source of a significant portion of the terror that most of us experience the first time we try to use Vim. So give yourself a pat on the back. The next most significant source of anxiety for first time Vim users is when they realise their keyboard doesn’t work properly. Don’t worry, it works perfectly fine.
The essence of Vim
The first thing you need to know about Vim (once you know how to open and close it that is), is that it is like an ogre, which is like an onion, which means that it has layers, which are not layers but are actually ‘modes’.
But I like to think of them as ‘layers’. As I said before, Vim was forged in a world where your keyboard was your only friend. So it had to do more than just input alphanumeric digits. It also had to get you around.
Before we talk any more about modes, I would like you to look at your keyboard.
If you want to learn to see the real beauty of Vim, then as of right now, you need to stop thinking of your keyboard the way you probably do.
You probably look at your keyboard and see the key with the letter ‘g’ on it and think, yep, that is the ‘g’ key. It’s not.
As hard as this might be at first, you need to stop thinking of the key with the ‘g’ on it as the ‘g’ key, and think of it as just a blank key, that just happens to be called ‘g’ because that’s what someone decided to call it once. Up till now, you’ve found that the key called ‘g’ just happens to output the letter ‘g’ on your screen most of the time. But that’s not what it does in Vim.
Depending on what ‘mode’ your in, and what other keys you have just hit, your key called ‘g’, and all the rest of your keys can do any number of things. They may move your cursor, delete something, paste something, copy or ‘yank’ something… lots of things. There’s one mode where they more or less do what you are used to them doing, but funnily enough it’s not the mode that you’ll find yourself in the most.
Remember when I said that ‘what was the source of your initial angst, becomes your favourite thing about it.’?
Well the ‘thing’ I am referring to here is known as ‘normal mode’.
Normal mode is the mode that Vim is in by default, and it’s the mode that Vim is in when you first open your editor. It’s called normal mode because it’s the mode that programmers will find themselves in most of the time and is where you do most of your text ‘editing’, and it’s not the mode where your key called ‘g’ puts a ‘g’ on the screen.
Normal mode is the mode where you navigate through your text file, and make all manner of edits and changes, but it’s not where you ‘insert’ the text. The mode where you ‘insert’ the text is called ‘insert mode’ (which makes so much sense once you think about it).
Insert mode is where your keys will feel a little more familiar, in that your keys that are named after letters and numbers will output those letters and numbers on the screen when you press them.
The other two most commonly used modes are ‘visual’ mode and ‘command-line’ mode. When I asked you before to hit :q to exit, hitting the ‘:’ puts you in command-line mode. I’ll explain more about those two in another post soon.
For now, I just want you to know about these four modes:
Normal mode: The mode you’re normally in and the mode you start in. Normal mode is for navigating your document and making a near infinite amount of changes to it using only the keyboard. Normal mode is your friend. Normal mode is your personal assistant, and it’s very good at its job. Eventually you’ll start to wish you could take your personal assistant wherever you go.
Insert mode: This is the mode where you insert text. It is not the default mode and is not where you spend most of your time. By the end of this series you’ll understand why.
Command-line mode: This is where you input commands to Vim similar to the command prompt in MS-DOS, or the bash terminal.
Visual mode: This is used for highlighting text, so you can copy (yank), paste (put), indent and other various things to highlighted blocks of text.
So you might be wondering:
‘Yeah this is great, but why the hell does this make Vim so much better?’
Good question. Above all things, the most important reason that utilizing the keyboard in this way makes Vim so fast, is because your hands spend the minimum amount of time moving from one device to another. I promise, once you start to get used to it, you’ll understand what I mean. You can do a very large number of things without the palms of your hands leaving their resting place.
If you’re a pretty efficient touch typist already, in no time your fingers will be performing the tango with your keyboard. They’ll dance across your keys like Antonio Banderas in that movie where he dances a lot… whatever it’s called.
Before you know it the limiting factor for your productivity will be your touch typing speed and the speed you can think. As many enthusiastic Vim users have pointed out before me, it reduces the minutiae and downtime between movements and inputs, allowing you more mental energy to focus what matters most – your work.
Vim also has a mind boggling number of plug-ins and customizations that allow it to do an enormous amount of things to streamline your workflow and make it more efficient.
Since I’m still a beginner Vim user of sorts, I’ll leave it up to any seasoned Vim vets to list their favourite plug-ins and extensions in the comments section!
Personally I also find that on top of all that, I just love using it. It makes working in it a game. I’m always asking myself if Vim has a way of making something faster and less repetitive (which is fundamental to the programming mentality – don’t repeat yourself).
OK Geoff, enough is enough, I want to do something already.
Good, me too.
Next week we’ll be going through some of the basics of normal mode, and how to quickly navigate your way around a file in Vim; this is the cornerstone of what separates Vim and Vi like editors from all the rest.
From here will be a series of introductory posts which will go through a couple more of the basics of Vim, which I will eventually compile into a downloadable pdf, or maybe even an eBook!
Hopefully that’s not too unsatisfying for you. If I’ve piqued your curiosity then be sure to subscribe to get notified when all the rest of the articles come out (never more than once per week).
Thanks so much for reading (all the way to the end no less!)
If you want to know more about me and why I started this blog check out my about page.