DEV Community

Giovanni Naufal
Giovanni Naufal

Posted on

How (and why) I use Vim


Vim in action
Vim in action

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, VS Code, Notepad++, Emacs, Vim… Some have IDE-like features, others are most basic and simple, like the standard notepad.

When I worked at Campus Code we were 8 developers, and almost all used Vim. We have a standard configurations that you can check out here.
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:

Vim initial screen
Vim initial screen

And now, what do I do?

If you have no clue about how to use Vim, you are not alone. In 2017 Stack Overflow announced that the question "How to exit Vim?" reached 1 million visits. That's the same as to say that 1 in every 20k users accessing the site are just trying to exit Vim.

exit vim
Just type :q and Enter, seriously

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.

Normal mode:

vim normal mode
The mode is shown at a bar below the editor

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.

Insert mode

vim insert mode
When the lower bar is showing INSERT, you can write

Press 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.

Emmet-vim in action pressing <ctrl>+y
Emmet-vim in action pressing +y

To exit 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 find 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
:%s/foo/bar/g replace foo by 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!

Top comments (5)

Collapse
 
gionaufal profile image
Giovanni Naufal

Great! I always forget about that. I've remapped my caps-lock to be a CTRL or ESC, depending on how it's pressed, só I almost never use the real ESC key too.

Collapse
 
gionaufal profile image
Giovanni Naufal

SpaceVim seems nice, but I learned using Vim using the dotfiles of the company I used to work, and as I also use tmux in my workflow, I figured it would be much trouble to swtich to another vim distribution.

Collapse
 
waynee95 profile image
waynee95 • Edited

I remember the struggle on how to quit vim. First time I encountered it, was in a class where we had to work on a Raspberry Pi connected via ssh and the only Editor we had was Vim.

It was a really terrible experience. After probably 1 year I thought, maybe I should give it a second try.

Nearly 1 year passed and I am now using Vim as my main editor. It will take a bit of time to get used to but it's worth it in the end. Everyone should at least try Vim for a few days, but I don't think it will work out for everyone. However, it gives text editing a whole new feel.

Nowadays, I find myself typing ":wq" into non-vim programs 😂👌

 
vintharas profile image
Jaime González García

CTRL-C also works. I find easier to type than CTRL-[ 😄

Collapse
 
jorinvo profile image
jorin

Nice post!

The best explanation of vim I have seen so far has been this answer on stackoverflow :)

stackoverflow.com/a/1220118/986455