How (and why) I use Vim
There’s much discussion of what is the best text editor among programmers and developers on the web. There are a lot of options, being some of the most popular; Atom, Sublime, Brackets, Visual Studio, Notepad++, TextWrangler, Emacs, Vim… Some have IDE-like features, others are most basic and simple, like the standard notepad.
In this post I will not try to convince you about what is the best editor or IDE. I’ll only show the advantages that I found using Vim, why I use it and how you can use it too.
First, a little bit of history…
Vim (Vi IMproved) is a improved clone of the Vi editor. It is open source and it was launched in 1991. To start it, just type
vim in your terminal (Linux or MacOS). You will see a screen like that:
And now, what do I do?
If you have no clue about how to use Vim, you are not alone. Recently Stack Overflow announced that the question “How to exit Vim?” reached 1 milion visits. That’s the same as to say that 1 in every 20k users accessing the site are just trying to exit Vim.
As you probably noticed, Vim doesn’t have graphic interface nor mouse support (it sort of has, but no one uses it), so navigation must be made through the keyboard. This is easier than it looks, but it requires a little bit of practice.
To open a file with Vim just type
vim <path_to_file> in your terminal. If you want to open the folder that you are at,
vim . .
Vim was built thinking that, as developers, we pass most of our time editing text, not necessarily writing new stuff. That’s why it has 2 main modes: Normal (navigation) and Insert.
This is the standard mode when you open Vim. Navigation is made with the keys h (left), j (down), k (up) and l (right). If you want you can use the arrows, but only the fact that you have to move your hand from the characters to reach them makes the alphabetic navigation more efficient.
Some other important commands:
w move the cursor to the beginning of the next word
e move to the end of the next word
b move to the beginning of the previous word
x erase the character below the cursor
u undo last change
<ctrl>+r redo last undone change
v access visual mode (for selection). To select something, navigate through hjkl or arrows, and the selected text will be in another color.
y copy what’s selected in visual mode. It can be used combined with other commands, for example
yw to copy the next word,
yy to copy the whole line.
d delete what’s selected in visual mode. Like
y it can be used with other commands.
c delete what is selected and opens Insert mode (change). Like the above, it can be used with other commands.
i to enter insert mode. As the name says, on it you can insert, or write, what you want. Here Vim works like any other editor, and depending on your configuration you can have autocomplete options and even shortcuts to create HTML tags like Emmet.
To exist insert mode, press
ESC. I find it better to work changing between these two modes.
Other useful commands
In normal mode:
:e <path-to-file> open a file
:w save the current file
:q close the file (and Vim)
foo in the file. Press Enter to go to t he word,
n to go to the next occurrence of the word and
N to the previous
bar in the whole file. See here other replace options
g10 jump the cursor to line 10 (for example)
gg jump the cursor to the first line of the file
G jump the cursor to the last line
There are just some basic tips. Using Vim requires patience and persistence, but at least to me it was worth it, because it increased considerably my work speed and efficiency.
Do you have another useful Vim tips or questions? Leave them here in the reply!