Vim is the most powerful and versatile code editor available for Unix-like systems. It is an extension of the Vi editor developed by Bill Joy. Vim is available by default on most Linux and BSD systems.

Thus, you can use the same editor on all of your systems and remote machines. Vim's rigorous vocabulary also makes it extremely efficient and expressive.

How Does Vim Work?

Vim is different from traditional Linux text editors. It maintains the Unix philosophy of doing one thing and doing it right. The fundamental notion is that as programmers, we spend most of our time editing code, not writing it.

Vim provides several modes to address this. Each mode does something different and is controlled through keystrokes. The essential vim modes are the normal mode, visual mode, insert mode, and command mode.

Illustration of Vim Modes

We use normal mode for reviewing code and performing quick operations. The visual mode is used for highlighting text sections, and the insert mode is where you add texts. You will use the command mode to type in various vim commands.

How to Use Vim in Insert Mode

When you open vim, it starts in normal mode. You can switch into insert mode by entering the i key. This invokes the insert mode at the cursor position. You should see an indication at the bottom left corner.

Now you can type in any text, and vim will copy those into the buffer. To save the progress, switch back to normal mode by pressing the escape key <ESC>. Now type the following vim command.

        :w
    

There are other ways to go into the insert mode. For example, you can insert text right below the current line by pressing o in the normal mode. Use O to insert text above the current line.

Inserting Text in Vim

Use I for inserting text at the beginning of the current line. You can utilize the a key for appending text right after the cursor. Use A for adding text at the end of the line.

To save and quit the current file, switch to command mode, and type the following.

        :wq
    

How to Use Vim in Normal Mode

Vim users spend most of their time in normal mode. Here you can navigate through texts and perform quick edits. Pressing the escape <ESC> key from any mode will take you to the normal mode.

Instead of the standard arrow keys, vim uses hjkl (h for left, j for down, k for up, and l for right) for navigation. It may seem counter-productive at first. But, vim does this to save the time it takes users to reach the arrow keys across their keyboard.

Also, like many console-based editors, vim encourages users to stay away from the mouse. You can configure vim for enabling mouse support though.

You can also move around text word-by-word. For example, pressing w in normal mode moves the cursor to the beginning of the next word. You can move to the beginning of the current word using b and end via e.

Navigating Vim Using Words

Use 0 for navigating to the beginning of a line and $ for moving to the end. Pressing H gets the cursor to the top of the screen, M to the middle, and L to the bottom. You can scroll up and down using Ctrl+u and Ctrl+d. If you press gg in normal mode, vim will move the cursor to the top. Enter G to move to the end.

You can learn vim editing commands once you're comfortable moving around in normal mode. Use x to delete a character, s to substitute. You delete texts using the d operator. It takes a motion as its argument. The syntax for this is shown below.

        d{motion}
    

A motion can be anything. For example, pressing dw in normal mode deletes the current word. If you type d$, vim will delete everything to the end of the line. Similarly, typing d0 deletes to the beginning of the line. Use dd for deleting the entire line.

Deleting Texts in Vim

However, you don't need to delete something and go into replace mode for making changes. The c operator allows us to make changes to a vim motion directly from normal mode.

        c{motion}
    

So, when you press cw, vim deletes the current word and puts you into insert mode. Your cursor needs to be at the beginning of the word since vim will delete from the current position. Use ciw to get around this. This makes changes inside the current word. Use cc for changing the whole line.

Changing Texts in Vim

Use y for copying motions and p for pasting them. So, yw copies the current word, and yy copies the entire line. You can repeat the previous command using the dot . operator, undo changes using u, and redo them using Ctrl+r.

If you want to find something in your code, use the following vim command in Linux.

        /{regex}
    

Here, regex is a regular expression. Press n for going to the next match and N for the previous match.

Finding Strings in Vim

How to Use Vim in Visual Mode

Using vim in visual mode allows us to select blocks of text using movement keys. This is how power users move code blocks in vim. Enter v in normal mode for switching to the visual mode.

You can now highlight portions of text or code using the navigation keys hjkl. Use Ctrl+v for switching into visual block mode. Here, you can select blocks of text. You can highlight lines by going into visual line mode. Use V to select visual line mode.

Block Selection in Vim

This allows us to make edits to a block of text at once. For example, you can select a text block and press y to copy the text into the vim buffer.

How to Use Vim Command Mode

We can access the command mode by typing : in normal mode. It will bring the cursor to the bottom of the screen, followed by a colon. Below are some of the most useful vim command in Linux.

  • :w save changes to file
  • :wq save and quit
  • :saveas FILE save the current file as FILE
  • :q  quit vim
  • :q! quit and discard changes
  • :e FILE open FILE for editing
  • :help open help

Bookmark this vim cheat sheet for quick access to useful vim commands.

Mastering Vim Basics

Vim is a robust editor that eliminates the gap between thinking and editing. Writing code becomes way more exciting once you're proficient at vim. Although you'll need years of practice to master it truly, understanding vim basics should get you started on the right track.