Vim is a bizarre editor for most people. It is a complex editor with two modes : insert mode and command mode. When vim is starting it is by default in command mode that means that all keys represent commands. If you press the key i then you are in insert mode where you can type anything and all letters are just... letters. You have to press Esc to go back to command mode.

To save a file, you have to be in command mode and do :w following by Enter.
If you want to save and quit it' s :x and Enter again.
That's the really basic to survive in a Vim editor.


But why using Vim if it's that hard ?

Vim learning curve

Speed

Let's take an example where you want to replace a variable name with another one. One way to do that is to open the search and replace tool but most of us don't do that and instead prefer to do it manually. Well, here's what you do without Vim :

Without vim

Now let's take a look at how it would look like with Vim.

With vim

Well, that's way faster and way more accurate.

Another few examples

Quickly replacing a text inside single quotes.

Vim quickly replace

This is done using ci' in command mode. It stands for change inside a single quote (but can be used for anything between two characters).

Vim replace character

Here two commands are needed, first : r/ (replace character with /) and for the other one, . will redo last action.

Using the h, j, k, l instead of arrow keys

Arrow keys are an issue because they are far away from the main keyboard letters. In Vim command mode, every action is typically represented by a letter so you want your fingers to be always as close as possible to letter keys. Using h, j, k, l keys to move your cursor respectively to the left, bottom, top and right is a good choice. At first it is really hard and you would do almost anything to continue to use your arrow keys but trust me, you have to keep trying.

Vim j,j,k,l keys

I learn to use those keys instead of arrow keys thanks to Vim Adventures. It is an educational game to learn how to use Vim and master all the different commands. I recommend it, at least at the beginning.

Vim is available almost everywhere

If you are at work and you must use a specific editor like IntellijIDEA, PHPStorm, Atom or even SublimeText, they all have a vim mode in their plugins/packages.

Here' s a few links :

Plus, you'll impress your coworkers by typing faster without touching your mouse.

My keyboard commands cheat sheet

You can find well-designed cheat sheet on Vim commands on the internet but I wanted to share mine which includes only the keys that I use on a regular basis.

Editing

  • r replace a single character (while staying in command mode)
  • s substitute a single character
  • S substitue a line
  • cw change to the end of a word
  • c$ change to the end of a line
  • cib change inside a brace ()
  • ciB change inside a bracket {}
  • ci[character] change inside a [character] (i.e : ci')

Insert mode

  • i insert before text
  • I insert at the beginning of a line
  • a append after cursor
  • A append at the end of a line
  • o new line after cursor
  • O new line before cursor
  • ea edit at the end of a line
  • Esc exit insert mode (and go to command mode)

Copy/paste/delete/cut

  • yy copy a line
  • [number]yy copy number line(s)
  • y$ copy from the cursor to the end of the line
  • p paste after cursor
  • P paste before cursor
  • dd delete a line
  • D or d$ delete from cursor to the end of line
  • [number]dd delete number line(s)
  • dw delete word
  • x delete a character

All delete actions are in fact cut actions as well

Undo/Redo

  • u undo
  • Ctrl + r redo

Cursor movements (hard)

  • j k l m respectively as left, bottom, up and right arrow
  • w jump forward to end of a word (without punctuation)
  • W jump forward to the end of word
  • e jump to the end of a word (without punctuation)
  • E jump to the end of a word
  • b jump backwards to the start of a word (without punctuation)
  • B jump backwards to the start of a word
  • 0 start of a line
  • $ end of a line
  • gg move to the top of the file
  • G move to the bottom of the file
  • [number]G move to line number
  • f[character] move to next occurrence of character character
  • t[character] move before next occurrence of character character

Visual mode (hard)

  • v enter visual mode
  • V start linewise visual mode
  • Ctrl + v visual block mode (like in a gif above)
  • aw marks a word
  • ab marks a block with ()
  • aB marks a block with {}
  • ib marks an inner block ()
  • iB marks an inner block {}

Exiting

  • :q exit without saving
  • :w save
  • :x quit and save

Adding ! to the end of those commands force the action. Example : :q!

Conclusion

Well, it takes a good month to really master Vim in every way possible. Don't be afraid to start and you'll see that soon enough, you'll be writing, moving blocks of code very fast and accurately using only your keyboard. However, I think that Vim is not made for everyone and should not be used for HTML or CSS.

Vim joke