Find the part 1 here: Let’s learn Vim! Part 1
So, last time when we were making our hands dirty with Vim, we ended up writing a very good poem with deep meanings.
In case you are not convinced with the poem, I dare you write a better one. Thank you.
Now, you have seen in the first part of the series that to move the cursor in Vim, you use **h,j,k,l**
in Normal Mode. This is weird until you get accustomed to it.
Apart from that, you can use **w**
and **e**
to navigate through your text.
**w**
: moves the cursor to the beginning of the next word.**e**
: moves the cursor to the end of the next word.
Effects of commands w
and e
In the gif posted above, notice that when you are using the **w**
or **e**
command, it does not skip punctuation apostrophe (’). To always move to the next position after a blank line, use **W**
and **E**
.
On the other hand, say you want to move 3 words at a time. To do that, you hit **3w**
in Normal Mode. That means, in general, you hit **nw**
to move through n words at a time.
I guess you are thinking, what if we want to do the same operations backwards? They are simply **w->b, W->B, e->ge, E->gE**
. Probably, not all of these operations will be heavily used.
Sometimes, you are in the middle of a long line and you just want to move your cursor to the beginning of that line. Instead of continuously hit **h**
or other commands, just hit **0**
.
Zero.
Similarly,
**gg**
— move to the very beginning of your document.**G**
— move to the beginning of the last line of the document.**G$**
— move to the end of the document.**nG**
— move to the nth line where n is a number given by you.
2G, 3G, 1G, 4G
So now you are working on your current directory and you forgot the files that are in that particular directory. In general, we just do a simple **ls**
and list the files and folders. You can do the same from inside Vim. The idea is to somehow broadcast a shell command without quitting Vim.
This can be done by **:!**
followed by the command.
Vim also supports writing the content of your current file to a new one. Probably you have guessed already, the command is **:w <filename>**
.
For selecting some portion of your text, you start with **v**
in Normal Mode. Now if you move your cursor, you will see that the particular portion will be selected.
As you have already seen, we have two text files with same poem. Let’s open them together.
To open multiple files as tabs, you can do either of the two ways. Hit **vim -p file1 file2 file3**
from terminal. Or while staying inside vim, you can write the command **:tabnew filename**
. Let’s open **poem.txt**
this way:
The thing is, when you have multiple tabs, you need to navigate from one to another. This can be done using go command: **gt**
and **gT**
or to a tab n-th tab, **ngt**
. Here, **n**
is a tab number.
You might have a question at this point. What if you are interested in splitting the screen? This would be much convenient while working on multiple sections of the same file.
Vim has taken care of it. To split a screen, you hit **ctrl+w+v**
. It splits the screen in the middle vertically. To do the same in horizontal manner, hit **ctrl+w+s**
.
Notice how we are navigating among the splits. Just use **ctrl+w+<cursor moves>**
.
What if you need to edit multiple files in these separate splits? Can be done easily as mentioned in StackOverflow!
Let’s say, you are interested in writing another poem. And as all the good poems should start with the Roses are red, Violates are blue, A long time ago — you decided to create a new file with this two lines copied in it.
Vim makes it super easy to do this kind of operations. You first need to select the portion of the text you want to copy to a new file and then write in it.
As you previously saw, you first select the portion of the text by using **v**
and moving the cursor. Then hit **:**
. You will see these characters: **:’<,’>**
. They are kind of prompting for a command. If you hit **w <filename>**
, a new file containing the selected portion will be created.
Finally, the last operation I would like to demonstrate would be copy-paste in Vim.
Visually select the portion you want to copy, hit **y**
to copy and then **p**
to paste.
Then there is another method of copying in Visual Block Mode. The command goes like this: **ctrl+v -> shift+v -> y -> p**
.
Notice the difference between the two methods. In the second one, texts are selected as blocks.
So far, I have shown you guys some of the very basic commands and use cases of Vim. There are numerous of them and you can always find more convenient ways to do various operations. My suggestion will be to follow some cheat-sheet where at least important commands are listed to easily pick up.
For starters, here is a cheat sheet you can follow.
Lastly, if you have followed through these two articles, you should feel more comfortable with Vim.
Feel free to point out any mistakes and thanks for reading!