back to blog

Vim: what is it and why should you learn to use it?

Published 12:00 13th May 2022

General introduction to Vim

Vim is a text editor commonly known and mentioned as being fast, powerful. It can run right on the terminal while still providing an intuitive graphical interface for users to manipulate. In addition, it must be mentioned that it is created to optimize all necessary operations in your text editing process to use only the keyboard. This is also my favorite thing about Vim

Vim is an upgrade of another editor released in 1976 called Vi, and then we have Vi Improved or more commonly known as Vim as it is now.

The question will arise, why at this age we have to consider using Vim? Well, for me personally, it's mainly because I'm too lazy! Once I put my hand down to start the process of coding, I want my hand to stick to the keyboard! Really, the act of lifting your hand to grab the mouse and drag and drop it a bit broke my coding flow. And Vim emerged as a solution.

For me, learning how to code using Vim is like learning how to play an instrument. It will be difficult in the beginning, but those difficulties will gradually become accuracy and efficiency in the future.

And in today's post, I will introduce to you the basic operations that make the name of Vim!

In addition, I am a PHP + JS developer, so my main working environment is on Ubuntu. Personal machine using WSL Ubuntu 20.04

Vim is usually included when you install Linux, BSDs or MacOS operating systems. And in this series, I will completely install WSL Ubuntu-22.04 and start from scratch.

Basic operations with Vim

Exit Vim

Vim can be installed and bundled almost anywhere. And sometimes you accidentally fall inside the Vim maze and can't get out ? For example, merge non fast forward with git for example.

image.png

The first thing you can learn here is to get rid of Vim. If you haven't pressed an edit by mistake, you can safely exit Vim by typing :q, where : to put you in Command and q for quit . If the file has been changed, you can choose to discard the changes entirely, leave the file as it is and force exit Vim by typing :q!, or save the changes and exit normally. with the combination :wq where w stands for write.

Open Vim

Vim can be opened by typing vim right in the terminal. Add the second parameter which is the address to the file you want to open. Vim can also open files that do not exist. Let's open up the terminal and type vim hello.txt. My machine has just installed the operating system, there is no file at all, so I will receive the interface like the image below.

image.png

Modes in Vim

Vim has quite a few modes, in which we need to know 4 basic modes are

  1. Normal Mode - usually used for cursor navigation
  2. Edit Mode - used for data entry
  3. Command Line Mode - used to run commands
  4. Visual Mode - used to make selections

We can switch between these modes via keyboard shortcuts. Right now I'm in Normal Mode.If you want to write Hello, World, you need to press i to switch to Edit Mode then type, then type Esc to return to Normal Mode. Please switch to Edit Mode and type quite a few words, quite a few lines. You can always copy a certain piece of code as well.

image.png

Basic navigation

Navigate each block

Are you familiar with moving left and right up and down with a combination of 4 right arrow keys? Let's continue to get into the habit of moving with the hjkl key combination. This is quite necessary. The main reason for this layout, is that a standard developer can usually type 10 fingers (if you still type with 2 fingers only, you should learn to type 10 fingers right away), 2 index fingers placed on 2 anchor keys is f and j, and typing will revolve around this key area, so if you master the navigation right at the base key row, you will reduce the movement of lifting your right hand to find the arrow key row, press the button to navigate and then lift your right hand back to the base row, find the j key to lower your index finger ?

image.png

There is a little trick for beginners, you will see that h is on the leftmost and l is on the right, it's easy to practice. the rest, j is the key with anchor, will be down because the downward operation will be more common than the up operation, because we are reading the text from top to bottom and ? the only left is k is going up!

Navigate within line

  • 0 - go to the first block of the line, for example my 2nd line, press 0 to go to the beginning
  • ^ - go to the first block with a non-null character, for example my 2nd line, pressing ^ will bring the cursor to the c in the word console
  • $ - go to the last block with a non-null character.
  • g_ - go to the last block regardless of whether it is empty or not.
  • f{i} - stands for forward, type f and 1 character, Vim will move the cursor to the next right position with that character, if you want to continue searching Search for that character, press ;
  • F{i} - also searches for the value with that character, but searches in the opposite direction, to the left of the cursor
  • t{i} - short for til, it also searches for that character like f but only asymptotically, placing the cursor just before the found position
  • T{i} - same, is to search the opposite side of t

Navigate screen

  • H - go to first block on current screen
  • L - go to the last block on the current screen
  • gg - go to first block of file
  • G - go to the last block of the file

Sometimes your code file is quite long, you usually use Page Up/Page Down right? Here we also have

  • ctrl f - jumps one screen down
  • ctrl b - jumps one screen to the top
  • ctrl d - jump halfway down
  • ctrl u - jumps halfway up

I often use the latter 2 more when I move up and down but still want to track where I am, what the previous / next part is.

Navigate between words

Here I want to navigate between words, but in Vim, there are 2 types of words for you to choose from.

  • word - is a set of letters, numbers and underscores _ (shift -)
  • WORD - is a set of non-null characters separated by space (space)

With the above definition, we can consider it like this

  • 192.168.1.1 - there are 7 words
  • 192.168.1.1 - has 1 WORD

And you can navigate between words like this:

  • w - jumps to the first block of the next word to the right
  • W - jumps to the first block of the next WORD on the right
  • b - jumps to the first block of the adjacent word to the left
  • B - jumps to the first block of the adjacent WORD to the left
  • e - jumps to the last block of the right adjacent word
  • E - jumps to the last block of the adjacent WORD to the right

Search in files

  • * - goes to the next nearest position with the same word as the current cursor position.
  • # - otherwise, we move the pointer to the position with the same word, but the closest one in front of the pointer.
  • /{index} - typing / will put you in Command Mode, now you type the characters you want to search for, press Enter and it will take you to the next occurrence, next Continue pressing n to search next, or N to search in the opposite direction
  • ?{index} - the reverse of the above command

Motion

Above are the basic navigation commands that I often use. They are called Motion in Vim. You can use the syntax {count} {motion} to repeat the change many times. For example, I want to go down 9 lines below, instead of typing jjjjjjjj, I can type 9j with the same result

Edit text

Above I mentioned using i to enter Insert Mode and manipulate, then return to Normal Mode by pressing Esc. To be precise, i here is not Insert but rather i in Inside, let's learn more.

  • i - enter Insert Mode, place the cursor to the left of the current block. For example, if the cursor is at block c in the word abcde, pressing i and typing will add characters between b and c
  • a - enter Insert Mode, place the cursor to the right of the current block. . For example, if the cursor is on block c in the word abcde, pressing a and typing will add characters between c and d
  • I - put the cursor in the first non-empty block of the line and Insert Mode from the left
  • A - move the cursor to the last block of the line and Insert Mode to the right
  • o- creates a new line directly below the current line and Insert Mode
  • O - creates a new line just above the current line and Insert Mode
  • x - right in Normal Mode, typing x will delete 1 character at the current block the cursor is on
  • r{c} - right in Normal Mode, type r and then type another character to replace that character in the current block position *u - in Normal Mode, want undo to return to previous state *ctrl r - in Normal Mode, want redo to the following state

Select text

In Normal Mode, we can create a text selection, or in Vietnamese, highlight

  • v- enter Visual Mode, use motions to select text
  • V - also enters Visual Line Mode, uses motions to select text on line by line

Combo Action

About the Combo Action

In Vim we have 4 main actions that can be used in most modes, except Insert Mode, which are

  • d - stands for delete, used to delete and save the deleted part to the clipboard
  • c - stands for change or cut rather, used to delete, also try to save the deleted part to clipboard and enter Insert Mode
  • y - stands for yank, in Vim is the copy operation
  • p - stands for paste, used to paste the contents of the clipboard out

An action combination will have the full form {action} {count} {motion}. Some examples of action combinations, using the aforementioned motions so that you can visualize them more easily:

  • delete the current line and delete the 5 lines above: d5k (you press 5k to any line, d5k deletes each line)
  • delete the current line and delete the 3 lines below, then enter Insert Mode: c3j
  • copy from cursor position to end of current word and next 2 word: y3w
  • delete characters until the next ' is found, delete that ' and save the deleted one to the clipboard: df'
  • still deletes characters until the next ' is found, but doesn't delete that ', and saves the deleted one to the clipboard: dt'
  • Only paste usually just press p to paste always at the position the cursor is standing, or if you want to paste 3 times in a row, type 3p

Also, we have some combinations of action itself

  • dd - delete current cursor line, save to clipboard
  • cc - delete the current cursor line, save to clipboard, enter Insert Mode right at the deleted line
  • yy - copy the line with the cursor, save the whole line to the clipboard
  • D - equivalent to d$, delete from current cursor position to end of line, save to clipboard
  • C - equivalent to c$, delete from current cursor position to end of line, enter Insert Mode, save to clipboard
  • Y - equivalent to yy, copy the current line

Combine with inside/around

There are 2 count that I use a lot that I mentioned above are 2 motion i and a, which will now act as count, which stands for inside and around . For example, I have the text "Hello, World" and place the cursor anywhere in this text

  • i - manipulate everything inside. Want to remove the word Hello, World? type di", it will find the " pair and delete everything inside that pair.
  • a - index operation, for example you want to change "Hello, World" to 'Hi!'?, type ca" then type 'Hi!' This * count understands the symmetrical brackets () [] {}. For example, in a phrase if {}, you navigate the cursor anywhere between { and }, then the two commands ci{ and ci} both give the same result.

Because of such an effect, count is also a bit picky motion, the motion it supports include:

  • w and W - word and WORD
  • s - stands for sentence, is motion available when combined with this combination i, a, defined as 1 clause, 1 sentence ending with ., !, ?
  • p - short for paragraph, is motion available when combined with this combination i, a, consisting of text that combines multiple sentence until a line break.
  • t - stands for tag html, is a symmetric pair of type <element></element>
  • quote marks ', ", ```
  • brackets (), [], {}, <>
  • b - represents round brackets round bracket, dab is equivalent to da( and da)
  • B - represents curly braces curly bracket, daB is equivalent to da{ and da}

Summary

You may be a bit overwhelmed with the number of "basic" keymaps that are so many. However, once you get to the stage like me, master the basics, and earn the self-titled Muscle Memory Vim, it becomes a lot easier to navigate and modify your text.

This article hopes to somehow arouse your curiosity. And in the following articles, I will introduce more carefully the pieces surrounding Vim.