Vim tutorial for beginner! (Part 2)
Wumi4 (460)

Hello everyone!

Last 3 days ago, I have just introduced you the Vim tutorial part 1. Now, because I'm free now, so today, I will show you the next tutorial of this series!

This is (again) the next series of my Vim tutorial, also the final (and most informative) part! So if you haven't read the first article, check out it here.

Ok, so let's get started!

Go to the start and the end of the text with 0 and $

To go to the start of a line of text, we use the 0 key.

And to go to the end, we use the $ key.

Find the next or previous occurrence of the word under cursor

To find the next (or previous occurrence) of the word that under the cursor, we use * and #.

Because of some reason with my Neovim, I can't show you the GIF right now :(

Goto line with g and G

If you have feel tired when move to the beginning or the end of a long text or large code manually by movement keys, gg and G handles all for you. gg goes to the first line, and G goes to the end line.

The example of gg:

The example of G:

Search text with / and repeat the search using n and N

Search text is a vital part in any text editor, and Vim isn't a exception.

To search text in Vim, we will use /, and give the text we are looking for. n and N are used to repeat the search for next or previous occurrences.

Let's say we have our Neovim looks like this (this is a simplified version of Neovim):

Hello Neovim!

This [i]s a simplified version.

Your Neovim may look different with this version.


(the [] is our cursor)

We want to find the next occurrence of the word Neovim, so we will use the / key and type Neovim like this:

Hello #[Neovim]!

This is a simplified version.

Your [Neovim] may look different with this version.


Now, we can see that we have two Neovims, one under our cursor (#[] is the word under the cursor in our case) and one at the final line. We want to edit the second Neovim to Vim, so we will press Enter to accept and use n to move to the next one ( the second one ):

Hello [Neovim]!

This is a simplified version.

Your #[Neovim] may look different with this version.


Now, we have move our cursor to the next word, let's edit it by go to the end with e, change to insert mode, delete all of the word and replace them with Vim.

Here's our result:

Hello Neovim!

This is a simplified version.

Your Vi[m] may look different with this version.


Easy, right? :D

Now, let's change to normal mode and continue!

Add a new line with O and o

To add a new line in Vim without using Enter key, we will use o and O. It will also change the mode to insert mode.

This is a example of o:

And this is a example of O:

Cool! Right?

Delete a character with x and X

If you want to delete character without changing the mode, x and X are the best choice. x delete the word under the cursor, while X delete characters in the left of the cursor.

Replace a character with r

To replace a character without changing the mode, we will use r, and then type the word that we are looking for.

Copy, cut and paste in Vim

Copy, cut and paste are likely 3 most important features in any single text editor. To do this important features, we use the d, y, p or P.

  • d stands for delete in Vim, which usually is cut in other editors.
  • y stands for yank in Vim, which usually is copy in other editors.
  • p and P paste the content, p paste after the cursor, and P paste before.

From here, you will ask Wait, how can I copy/cut the content and paste it?.

Here's how:

  • Change to normal mode
  • Position your cursor into where you would like to copy/cut
  • Press v to select characters, or V for whole lines
  • Move to the end where you want to copy/cut
  • Press d to copy (or y to cut)
  • Move your cursor into where you would like to paste
  • Press p to paste the content after the cursor, or P to paste before the cursor

Repeat the previous command with .

To repeat the previous command, just simply type .

Now, type (or copy) some long text/code, move to a place, do the command d2w and then type . to do the same again.

Visual mode

Besides insert mode and normal mode, Vim also has a special mode called visual mode.

In this mode, you choose text using movement keys before decided what to do with it.

Here's a example, in here, we use visual mode to choose the end part of a Markdown text, then use d to cut it:

The end

Now, with the knowledge from this part and the second part, I bet that you are quite confident to enter to real Vim now.

Here are most important commands that you should remember:

  • :w: Save
  • :save file-name.file-type: Save As
  • :q: Quit
  • :q!: Quit (without saving)
  • :help: Show Vim help

Also, don't PANIC! when you do a mistake, press u to undo and Ctrl-R to redo.

Now, that's all today! Thank you so much for spending times reading this article, I hope you enjoyed it!

Bye! :D

Edit: I'm sorry for the glitch that you have when watching example GIFs, it was all of my screen recorder's fault! :(((

You are viewing a single comment. View All
CodeLongAndPros (1539)

Ok so one thing: in Vim, the delete operator pulls into registers 1-9, and so it is cut. However, they yank operator (kill in EMACS) places it in one and zero. So doing ihello<ESC>xyy"2p"0pgives you hellohell.

Wumi4 (460)

@CodeLongAndPros Oops, I'm sorry, I take info about copy paste cut from Vim fandom and for some reason I say d is copy and y is cut! I have fixed it!

About the yank operator, when I do the same, it just show 4 word hell. Not sure if I do right or not.

CodeLongAndPros (1539)

@Wumi4 Yeah I didn't intend that. Try :h registers.