Today we’ll talk about vim, the ubiquitous keyboard-driven highly customizable CLI text editor, first released in 1991 and, at the time of posting, it is currently at version 8.1. By the way, this post was written using 100% vim.
Why use vim
As I said earlier, vim is a modal editor. That means you don’t need to use the mouse while editing in vim at all. The bindings are all close (at least in an qwerty keyboard), so vim ends up being ergonomic as hell. Also, as it’s slogan says, vim is ubiquitous. It is pre-installed on most *nix distributions. That means you can use vim at work, at home, and even at the server.
However, I think vim’s slogan should be “vim: once in, never out. Seriously.” Lots of people joke about the sequence of characters you have to type to leave vim:
- :wq - saves and quits vim (“
:” is the prefix for vim commands);
- :q! - quits without saving.
But seriously, I haven’t met anyone who started using vim then stopped because they started disliking it.
Also, another powerful feature in vim are the modes. You have lots of modes in vim, but the ones people use the most are:
- normal mode: you can move the cursor and use your bindings (you enter this mode by pressing ESC or ^C);
- insert mode: you insert text (you enter this mode by pressing “
i” while on normal mode);
- visual mode: where you can select text, to copy it, cut, paste, etc (you enter this mode by pressing “
v” while or normal mode).
This may seem strange at first, but it really pays off once you’re used to it.
How to start using vim?
If you use linux, vim is almost certainly already installed in your machine. If not, you are using a more bare-bones distribution and you know how to install it. If you use macOS or Windows, follow a tutorial on another site on how to install vim.
vim is hard to get used to. You have modes, you don’t use the mouse, you have weird controls. But don’t worry, it’s like that for everyone. To start, we’ll talk about how to move your little cursor. you can use the arrow keys or HJKL.
vim uses hjkl mainly because of historical reasons. (Picture of an ADM-3A computer)
You should use hjkl because it is closer to the rest of your keyboard, and so you don’t need to move your hand too much to type other commands. And since vim is all about efficiency, you should do the change. (I’ll explain to you how to disable arrow keys soon; be patient.)
To learn more vim commands, run
vimtutor in your terminal. It’ll guide you through the dark arts of vim. And is an awesome vim cheatsheet.
This is where the customization part comes in.
~/.vimrc is your vim configuration file. Here, you can change vim’s bindings, add comands, change vim’s interface, and many more things.
.vimrc uses vimscript, a language hated by many, but it still is 200 times better than vim’s rival, emacs, ((((who uses elisp for configuration)))).
For example, a binding I made and that I use a lot, is one that jumps to the start of a line, and inserts a comment (
This is the binding I’m talking about:
nno c ^i//<Space><ESC>^ Woah! That’s a lot of things for one line. Let’s break it down.
nnosays that you are going to define a binding for normal mode.
ctells vim that you want to bind the
^goes to the start of the line.
ienters insert mode.
//<Space>is the actual thing we are inserting. (We can’t just space here, because the
nnofunction is expecting only 2 arguments) (
<Space>is case-insensitive, just like
<ESC>. this means that both
<ESC>tells vim to enter normal mode again
^Go to the start of the line again, for convenience.
So, as I said earlier, you can paste this in your
.vimrc to disable the arrow keys.
There are more things you can put in your vimrc. If you are curious, here is a link to my vimrc. You’ll require Vundle (a vim plugin manager) to use it, though.
And this is how my vim looks like. For more cool things to put on your vimrc, check out the vim wiki.
Written by Lucas Micheloni de Souza