Things I Learned Today: Vim Basic Commands
This article is directed towards programmer who uses visual studio code and stuffs.
So today I have an assignment from my user (my HR manager) to learn about VIM which stands for Vi Improved. It is a form of text editor that you could use to edit anything via terminal, kinda like visual studio code. I have learnt about Vim more or less throughout my brief 2-years journey using git and other stuffs but I’ve never got any form of education around it. At first, I’m confused as to why people use this not-so-popular text editor in the terminal as their day-to-day text editor? Why don’t they use visual studio code or the more popular choices. Turned out, there’s a lot of benefit from using vim and I’ll try to explain it as a beginner as much as I can.
Why should you use Vim?
So first of all, we have to agree that leaving your keyboard and using mouse is not the desired behavior for programming, right? Imagine if navigating through code and stuffs is fast and easy, done only using hotkeys. Basically, that is what vim does.
Everything that vim does can be done using keyboard only, meaning that you don’t need to stop typing and pick up your mouse everytime there’s a mistake in the previous 2 lines of code. You can do the same in visual studio code and intellij, there is a lot of hotkeys to work with that makes your life easier, but vim forces you to use it, which means you can’t look for workaround from it. (actually, you can, but that kinda defeats the purpose isn’t it?) So if you want to familiarize yourself with coding using only keyboard, vim might be the partner for your journey
Also, I briefly mentioned about git in the introduction, and if you often uses github via terminal (which you should), you could seriously benefit from learning vim, since editing github commits, rebasing, etc, is by default done using vim.
Last thing, Vim is designed to boost programmer’s productivity. This means that a lot of features of vim is designed to make it easier to code. For example, vim has a feature to make you repeat a command several number of times. If you want to copy that list element in HTML 5 times, you can done it with typing yy5p
which doesn’t make sense but it really boost your programming speed because it’s only four character compared to selecting a line, ctrl+c and ctrl+v five times. So I hope you’re more interested in learning vim by now and join me to the next step!
Modes In Vim
There are two modes in vim: command mode and insert mode. When you first enter vim, you enter into command mode in which you can type commands like yy5p
command earlier. The other mode is insert mode, in which you could type normally inside the file. To switch between mode, you need to type esc
to enter command mode, and i
to enter insert mode (when you are in command mode).
So for example, you have a file named poem.txt
and it consists of poem and you want to edit it. What should you do?
First, install vim if you don’t have it
Then type vim poem.txt
, the views should be like this:
Then type i
to enter insert mode, the view should be like this:
Then let’s add a line by typing like usually in any code editor (go to the end of line, enter) and add “but should you?” at the last line
Now, type escape
to enter command mode and type :wq
. This colon means the following is an “ex command” (more or less performing administrative task), and w means write, and q means quit. So this means you’re quitting then saving the current file. Now, if you open the file again, the content should’ve changed.
One advice I’ve heard to be better with vim is you need to get out of insert mode as early as possible, so basically, navigate through the code until there is a change needs to be made, then use insert mode. This will boosts your productivity.
In the next section, I’ll explain more about the commands of command mode since you ought to be familiar with the insert mode (more or less the same with your code editor).
Basic commands of Vim
remember: this commands could only be done in command mode
Navigation Commands:
h -> move left
j -> move down
k -> move up
l -> move right
More navigation commands
w -> move to the beginning of the next word
b -> move to the beginning of the previous word
Helpful commands
dd -> delete an entire line
o -> insert a newline after this row and go to insert mode
y -> copy a selection
yy -> copy an entire line
p -> paste
Repeat commands
any numbers followed by command -> will repeat the command several timesFor example:
5h -> go 5 character to the left
5dd -> delete 5 lines
etc.
Today I’ve also learnt about the visual mode v
, which enables you to select more than one character and do stuffs with it, like copy paste, etc.
Further Consideration
At first, I’m a bit skeptical as to how this compares with vscode. I mean, I can use vscode to do the commands that are used in vim. But after reading several articles, I’m convinced to use vim extension with vscode since vim provides easier for your hand to type and vscode also gives the IDE vibe of renaming variables, extensions, cleaner UI / UX, etc. My opinion might change later though so don’t take my word for it.
Credits
I want to give my thanks to:
- Freecodecamp, NeuralNine, and Florian Dedov for providing me with an awesome 1-hour tutorial about vim
- Salsabila, my friend at GoTo Engineering Bootcamp that writes this amazing article
- GoTo Financial Bootcamp Team for giving me task so I have a reason to learn this :)