How (and why) I use Vim
Tags: vim, vi, ide, texteditor
Published: 2019-03-01 17:44:23

---
title: How (and why) I use Vim
published: true
description: Tips about Vim and how using it can improve productivity
tags: vim, vi, ide, text editor
---

---

![Vim in action](https://thepracticaldev.s3.amazonaws.com/i/9vlhgbdlr5e8znzad1yr.png)
_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](https://atom.io), [Sublime](https://www.sublimetext.com/), [VS Code](https://code.visualstudio.com/), [Notepad++](https://notepad-plus-plus.org/), [Emacs](https://www.gnu.org/software/emacs/), [Vim](http://www.vim.org/)… Some have IDE-like features, others are most basic and simple, like the standard notepad.

When I worked at [Campus Code](https://campuscode.com.br) we were 8 developers, and almost all used Vim. We have a standard configurations that you can [check out here](https://github.com/campuscode/cc_dotfiles).
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](https://thepracticaldev.s3.amazonaws.com/i/0842l3g36p4zszk1y2dk.png)
_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](https://stackoverflow.blog/2017/05/23/stack-overflow-helping-one-million-developers-exit-vim/) 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](https://thepracticaldev.s3.amazonaws.com/i/gqtgc68s0kphmnod2992.png)
_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](https://thepracticaldev.s3.amazonaws.com/i/95dmdf9yntn980xaj059.png)
_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](https://thepracticaldev.s3.amazonaws.com/i/5pokjrtu9wg91rzmvmwf.png)
_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](https://github.com/mattn/emmet-vim).

![Emmet-vim in action pressing <ctrl>+y](https://thepracticaldev.s3.amazonaws.com/i/996kk5d0qh14071db9j2.gif)
_Emmet-vim in action pressing <ctrl>+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!