A Beginner’s Guide to Vim
An introduction to the command line text editor, and a few helpful tips to get you started.
This post covers
- What is vim?
- Why I’ve started learning it and what I’m enjoying about it
- Tips for no0bs and getting started
It doesn’t cover
- Plugins
- .vimrc
- Specific shortcuts
- How to exit vim*
What is vim?
vim is a text editor which runs in the terminal on Unix-like systems.
The earliest version of vim is now 27 years old (!) and was created by Bram Moolenaar. vim stands for VI iMproved, as vim is based on vi. other successors (like nvi and elvis) also exist.
Oh…. So what’s vi?
vi (pronounced VEE-EYE) is the original text editor on Unix. Everything in vi is available in vim, plus some extras.
Cool! Can I use vi then?
Not as easily as you might think! If you try and open a file using vi <filename>
it just… opens in vim. Here’s why:
Let’s go through this:
In Unix systems, cd
stands for Change Directory and will, with no arguments after it, relocate you to the home directory. (It wasn’t necessary to do this, I just wanted to demonstrate that what happened next was location agnostic).
which
returns the path of the requested command (in this case, vi)
. As there can be multiple versions of the same command located in different directories, which
will show the one you are currently using in the terminal. Even if multiple matches exist, it loads up the first match in the shell’s LOOKUP PATH environment and ignores the rest, which is why only one result ever comes back.
l
is an alias that exists in my ~./bash_profile
and is short for ls -l,
, or list the contents of this directory, in long format, on one line
. Basically, this is “show me all the things”. (You can find a ton more flags here for ls
, which should send you promptly down the rabbithole of flags in general. If you’re going to do this, at least do it in the command line instead of googling and type man ls
where theman
command shows you info from the program manual and prints them out. Neat!)
And now to the interesting bit. The first section of what’s returned shows the file permissions, but because it starts with an l
it shows it’s actually a symbolic link (it’s a ‘soft link’ to another file or location, in much the same way that you can create a shortcut on the desktop to a program or file location somewhere else when using a graphic interface). So when I type in vi
, the actual location it points to is….. /usr/bin/vim
. And that’s why vim
opens.
Why should I learn it?
Chances are if you’re reading this article you’re already at least partially invested. Skip ahead if you don’t need convincing.
People have different experiences of learning vim (some argue that there’s a huge and very steep learning curve to get the basics under your belt) but everyone agrees: once you’re up to a moderate level of usage, it makes you quicker at editing your code. As one resource puts it, you can learn to edit at the speed of thought. You will be more efficient. The thing is, you need to put the effort into learning enough to meet a minimum threshold for actually reaping this efficiency, and that’s no mean feat. Up until this point, you will just be slow (slower than normal, which can be frustrating). But as with all skills, the more you practice,
- the faster your brain will recognise and start to automate which keys do what (often mistakenly called ‘muscle memory’)
- the more competent you will become at combinations and problemsolving
(For example, at the very first hurdle, I used to find it super confusing that (spoilers!) j
was down and k
was up because my brain expected them to be the other way round. It took a few hours but now it’s totally subconscious and I’m really happy I persisted.)
Additionally, it’s fast. I came from a background of using Atom, which is resource-heavy as it runs on Electron, and it loads an entire web browser for each instance of Atom that’s open, so can be slightly laggy and slow to load. Not so for vim!
Lastly, this is your chance to learn something new, both the syntax and the conceptual & abstract elements. Programming culture has appropriated the word grok
from its sci fi etymology to mean that you can not only regurgitate the words or characters in a practical way, but you understand the spirit of the language. The differences between computer programming languages are more than just syntax and application. There exists a profoundly different world-view underlying them, and this is your chance to learn a new one. For a nice introduction to vim, read this, written by the person who made it.
For me, learning how to use vim has been awesome. Do you remember what it felt like when you started learning how to program? When something worked and you felt like a f**king wizard? I’m still relatively new to vim but I’m constantly amazed with how expressive it can be with such directness; such terse commands wield power and efficacy. I still get that wizard feeling when I learn a new command or appreciate a new combination in vim, and I still have so much to learn. I currently help teach at a coding bootcamp and in my students’ first couple weeks I encourage them to not be afraid of seeking out new shortcuts using this repo of basics (please get in touch if you have any additions!). The appreciation I have of vim is like the feeling I first had when I first learnt ⌘-control-up/down
in Atom, but on steroids.
OK fine shut up, I’m in. What do I do?
- Complete vimtutor
Typevimtutor
in the terminal. Anywhere. Just do it.
Yay! Now, ignore your arrow keys and use h
, j
, k
and l
instead.
Things to remember:
- vim is called a modal text editor because it has many modes, and the two you get introduced to first are insert mode and command (or normal) mode.
- when you load up vim, it starts in normal mode. In this mode, characters you type get interpreted as commands, like
x
will delete the character which is currently under the cursor. To get back to this mode, pressesc
- in normal mode, vim interprets upper and lower case differently;
o
andO
are not synonymous (one inserts a line above the cursor, one inserts it below). Another nice illustration of this is thatw
ande
move the cursor forwards to the start of the next word and the end of the current word respectively.W
andE
do the same jobs, but ignore punctuation. A subtle, but useful, distinction. - in insert mode, the characters you type form words and sentences as you’re used to seeing in a text editor. To enter insert mode, press
i
.
There really isn’t much more I can say — vimtutor does a great job of explaining. It covers a lot of the basics, but definitely augment this with reading some more about vim online, or read sections of this mega manual.
2. Make your own list of shortcuts….in vim
This is not so much about the content, the actual shortcuts. Rather, this gives you a purposeful and low-stakes chance to try out what you’re learning; simply reading about things and trying them once or twice isn’t going to cut it. It also gives you some accountability and makes sure you’re actually practicing. I had vimtutor and my cheatsheet open at the same time as shown above.
3. Practice fun tutorials online
There are a ton of resources as you’d expect. Make time in your calendar to do at least half an hour a day. Go!
4. Mess stuff up and be really, really slow
I’m enjoying being slow and learning how to vim because I keep doing something and then thinking ugh there has got to be a quicker way to do this and then I research and bam! A new understanding of how one keystroke can save me four.
5. Complete vimtutor again… and again
It’s bite sized, it’s free, and it covers the foundations without assuming any prior knowledge. I’d like to get to the point where I could teach vimtutor in my sleep.
6. Build up your own procedure for learning shortcuts
I’ve been referencing this cheatsheet and seeing how I can integrate each shortcut (a minimum of one new one a day) into my practice, just so I get a chance to try it out a few times. Remember, “little by little, one travels far”.
7. Stop expecting numbered lists to be a substitute for doing the work
You really just need to go practice. I’m guilty of this too though and I loved reading this.
I’m well aware that there are arguments for using neovim/nerdtree/other plugins, but for this article I’m just focussing on getting to grips with the syntax and paradigm as a beginner. Should you start programming, I’d at the very least recommend putting syntax enable
so you have some syntax highlighting.
* Seriously.
What I’m going to cover next time:
Combos!
Disabling your arrow keys
Remapping your caps lock
key to esc
Building a .vimrc file
Some more vim evangelising