I want to start by saying that I am a beginner in using Vim.

I’m going to use this blog post to add all the commands I learn and use the most in everyday development.

Vim Modes:

Vim has 3 main modes: NORMAL, INSERT and VISUAL.
The most you'll be using are NORMAL and INSERT.

Normal Mode

The normal mode is where you start when opening Vim.
In this mode you simply type Vim commands.

Insert Mode

The insert mode is where you write text, as you are normally used to.
To enter the insert mode simply press i.
To exit the insert mode press ESC

Visual Mode

The visual mode is basically where you select text e.g. to copy it
To enter the visual mode, simply press v.
To exit the visual mode press ESC.

Creating and editing a new file

Create a file

First, open your command line session on your local machine.
Next start a new vim file by typing vim my_file.txt and then press ENTER.

vim sample.txt

Good job! Now you're inside your sample.txt file in the NORMAL mode of Vim.

Enter the INSERT mode

To enter the INSERT mode, as we have already mentioned, just press the lowercase i.

Now add some random text to the file, like

behind the word mountains

Once you're done, press ESC to exit from the INSERT mode.

Save the file

Now that you're back in the NORMAL mode, type :wq and press ENTER to save the file and exit Vim.

Now you're back to your command line session, let's check that you've successfully saved your sample.txt running:

vim sample.txt

If you have done all right, you'll see again the text you've entered before.
Because we aren't making any changes, to exit without saving run :q! and press ENTER.

Update:

I've discovered a faster way to exit from a file saving or not the changes.
Use ZZ to exit the file saving the changes, or use ZQ to exit the file without saving.

Let’s see the navigation:

h, j, k, l are the commands used to move around your code.

They work exactly like keyboard arrows.

h is used to go left one character at a time
l is used to go right one character at a time
j is used to go down one character at a time
k is used to go up one character at a time

Move by words

If you want to move word by word you can use w, b, W or B.

w and W will move word by word after the cursor position, with the single difference that W skips punctuations and empty spaces.

b and B will move word by word before the cursor position, with the single difference that B skips punctuations and empty spaces.

Go to the beginning/end of the line

What if you want to move your cursor at the start of the line?
Simply use 0 (zero).

In case you want to go the end of the line, use $.

I often use the combination 0w to put the cursor at the first character of the line.

gg and G (To the begin/end of the file).

What if you want to go to the beginning of the file?
Simply run gg.
In the same way you can go to the end of the file running G.

% (move to matching character)

This is another command that I use very often, especially when writing Flutter UI.
Let's see an example:

return Scaffold(
      appBar: AppBar(
        title: 'Title',
      ),
      body: ListView.separated(
                              ^
        itemBuilder: (_, index) {
          return Something();
        },
        separatorBuilder: (_, __) {
          return const SizedBox(height: 20);
        },
        itemCount: 5,
      ),
    );

Consider that you are at the ^ position and you want to move to the closing parenthesis, simply run % go to the end of the ListView widget.
This works also for {} and [].

Editing

Let's start the most important thing about Vim, editing.
We've already seen that to enter the insert mode, we simply press i.

But with Vim you can edit words simply from the COMMAND MODE

Check out some of my favourite commands:

I (capital i)

This key press will automatically go before the first character of the line setting the insert mode.
For example:

hi = 'Hello';
     ^

Imagine that you are at the ^ position, and you want to add the word final before the variable named hi.
To do this you can simply press I and then you can write final.

A (capital a)

Exactly the opposite of I.
Instead of moving your cursor before the first character of the line, it will go after the last character of the line.
For example

final hi = 'Hello'
      ^

What if you are at the ^ position and you want to add a semicolon ; at the end of the line?
To do this you can simply press A and then you can write ;.

cw (change word) and caw change a word)

This command is one that I use the most.
It is used to change a word with another.
For example:

void sum(int a, int b) => a - b;
     ^

As you can see here there is a typo, instead of sum, we have to write difference.
To do this simply press cw followed by the word difference.
This command will automatically go into insert mode after cancelling the word.

What if your cursor is at this position?

void sum(int a, int b) => a - b;
       ^

Without the necessity to move your cursor with b you can simply run caw
. In this way Vim knows to change the whole word, and will not start deleting from the cursor position.

dw (delete word)

What if you want to simply delete a word?
For example:

/// This is the documentation of some code code.
                                           ^ 

As you can see, here code is written twice, when you are the at ^ position, simply press dw to delete the extra code.

daw (delete a word)

Let's take the example used before:

/// This is the documentation of some code code.
                                             ^ 

As you can see the position of the cursor is not at the beginning of the word, in case you run dw, you will delete only de.
This is because dw deletes the word starting from the cursor point.
If you want to delete the whole word, simply run daw.

f (find after)

final a = 5;
  ^

If you want to move faster to a specific character you can use f followed by the character you want under the cursor.
Imagine that you want to move to the number 5, run f5.

F (capital F -> find before)

Let's take another example:

final apples = 4;
                ^ 

and you want to move the cursor to the a of the word apples.
Simply run Fa to go the the first a before the character position.

t and T (through)

This is another command that I use everyday, in combination with other commands.
t works exactly like f, so it is used to find something with the difference that it will move the cursor one character before the char that you searched for, for example:

final helloWorld = 'Hello, World!";
      ˆ

What if you want to change the hello into an hi.
In this case I run ctW (change through W) and then I simply write hi.
So we've said to vim that we want to CHANGE THROUGH the first occurence of a W.

D (capital d => delete till end of line)

final oranges = 5; // TODO: set oranges to 5
                  ^

Here we simply want to delete everything from the cursor position through the end of the line, to do this we simply run: D

C (capital c => change till end of line)

Similar to D, you can delete through the end of the line, but C will also put into INSERT mode after after deletion.
For example:

final oranges = 5; // TODO: set oranges to 5
                      ^

If you want to change the comment, you can simply press C.

dd (delete entire line)

This is something that I use very often.
Simply delete the whole line under the cursor.

{num}{command} (repeat a command {num} times)

This is something very powerful about Vim.
Every command that you run, can be repeated as much as you want.
Let's see an example:

good line
bad line
bad line
bad line
good line

Here we simply want to delete every bad line.
To do this, put your cursor anyway in the first bad line and then run 3dd.
Boom! 3 lines deleted in a second.
Of course you could have also performed dd followed by . and ..
For everyone who doesn't knew ., this is a powerful command that repeats your last command.
But with the numbers, everything is faster.

dgg and dG (delete through begin/end of file)

We've already seen that to move to the begin of the file we can simply run gg, and to go to the end of the file G.

We can combine this command with d (delete), to delete through the begin/end of the file.
For example:

good line
bad line
^
bad line
bad line
bad line

To clear every line from the cursor position, simply run dG.

Follow me on Twitter for more updates about Vim and Flutter development