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
To go to the start of a line of text, we use the
And to go to the end, we use the
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
Because of some reason with my Neovim, I can't show you the GIF right now :(
Goto line with
If you have feel tired when move to the beginning or the end of a long text or large code manually by movement keys,
G handles all for you.
gg goes to the first line, and
G goes to the end line.
The example of
The example of
Search text with
/ and repeat the search using
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 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. =NORMAL=
 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. =NORMAL= /Neovim
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
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. =NORMAL= /Neovim
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. =INSERT=
Easy, right? :D
Now, let's change to
normal mode and continue!
Add a new line with
To add a new line in Vim without using
Enter key, we will use
O. It will also change the mode to
This is a example of
And this is a example of
Delete a character with
If you want to delete character without changing the mode,
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
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
dstands for delete in Vim, which usually is cut in other editors.
ystands for yank in Vim, which usually is copy in other editors.
Ppaste the content,
ppaste after the cursor, and
From here, you will ask
Wait, how can I copy/cut the content and paste it?.
- Change to
- Position your cursor into where you would like to copy/cut
vto select characters, or
Vfor whole lines
- Move to the end where you want to copy/cut
dto copy (or
- Move your cursor into where you would like to paste
pto paste the content after the cursor, or
Pto 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.
insert mode and
normal mode, Vim also has a special mode called
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:
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:
:save file-name.file-type: Save As
:q!: Quit (without saving)
:help: Show Vim help
PANIC! when you do a mistake, press
u to undo and
Ctrl-R to redo.