Vim is one of the most powerful text editors used by developers and system administrators. It might look a bit tricky at first, but once you get the hang of it, you’ll love how fast and efficient it is. In this artical, we’ll cover everything you need to know about Vim, from opening and closing files to editing, searching, and saving your work. Let’s get started!
How to Open and Close Vim
Step 1: Create a New File
Before opening Vim, you can create a new file using the touch
command in your terminal:
touch myfile.txt
This creates an empty file named myfile.txt
.
Step 2: Open the File in Vim
To open the file in Vim, type:
vim myfile.txt
Now, you’re inside Vim and ready to edit your file.
Understanding Vim Modes
Vim works in different modes. The two main modes are:
- Normal Mode: This is the default mode when you open Vim. You can move around the file and run commands here. Press
ESC
to go back to Normal Mode. - Insert Mode: This is where you can type and edit text. Press
i
to enter Insert Mode. - Visual Mode: In this mode you can select the text visually, to go into the visual mode you have to press
v
or just select the text with by mouse.
Moving Around in Vim
In Normal Mode, you can move the cursor using these keys:
h
: Move leftj
: Move downk
: Move upl
: Move right
For faster navigation:
2w
,5w
: Move 2 or 5 words forward.0
: Go to the start of the line.$
: Go to the end of the line.G
: Go to the end of the file.gg
: Go to the start of the file.:10
: Go to line 10 (replace 10 with any line number).
Editing in Vim
Adding Text
Press i
to enter Insert Mode and start typing.
Deleting Text
In Normal Mode:
x
: Delete a single character.dw
: Delete a word.d$
: Delete from the cursor to the end of the line.dd
: Delete the entire line.
Undo and Redo Changes
Did you make a mistake? No worries! In Vim to undo & redo the changes you have made press :
u
: Undo the last change.U
: Fix the whole line and bring it back to its original state.Ctrl + r
: Redo the changes.
Cut, Copy, and Paste
deleting a whole line is simple just press dd
and if you want to past the deleted line you can just press p
at the desired location. The past also w
dd
: Cut a line.p
: Paste the cut line.
To copy a block of text:
- Move to the beginning of the text you want to copy.
- Press
v
to start visual mode. - Use your mouse, the arrow keys or
h
j
k
l
to select the text. - Press
y
to yank (copy) the selected text.
Replacing Text
r
: Replace a single character.ce
: Change a word (deletes the word and lets you type a new one).
Searching in Vim
Searching in Vim is super easy, I just said that i don’t think the searching is that good in vim but, not gonna lie, it’s pretty fast because you don’t have to pick your hands up from your keyboard just to search some word 😄.
/hello
: Search for the word “hello” in the file.?hello
: Search for “hello” in the upward direction.n
: Go to the next match.N
: Go to the previous match.
Replacing Text in Vim
You can also replace text quickly but the command is a little big. It’s not event that big I think you can handle some big stuff because later in the series you will write some big command 🙂.
:s/old/new
: Replace the first “old” with “new” in the current line.:s/old/new/g
: Replace all “old” with “new” in the current line.:%s/old/new/g
: Replace all “old” with “new” in the entire file.:%s/old/new/gc
: Replace all “old” with “new” but ask for confirmation each time.
Saving and Exiting Vim
This is the most important thing, we don’t want to loose our progress that’s why we ctrl + s
all the time to save our work. Although in vim you can’t actually ctrl + s
but what you can do is :
:q!
: Quit without saving changes.:wq
: Save changes and quit.:w
: Just save changes:q
: Simply quit without saving
Why Should You Learn Vim?
Vim is lightweight, fast, and available on almost every Linux or Unix system. It’s perfect for editing code, writing scripts, or configuring files. Once you learn the basics, you’ll see how much time it can save you.
Pro Tips for Using Vim
- Practice Moving Around: Get comfortable with
h
,j
,k
, andl
for navigation. - Use Visual Mode: Press
v
to select text for editing. - Customize Vim: You can tweak Vim to suit your needs by editing the
.vimrc
file.