Working Code
Open your terminal and follow along:
vim hello.txt
The screen changes and Vim opens. Don't type anything yet! You're in Normal mode.
Press i. You'll see -- INSERT -- at the bottom of the screen. Now you can write:
Hello, Vim!
This is my first file.
When you're done typing, press Esc. -- INSERT -- disappears and you're back in Normal mode.
Now it's time to save and exit. Type :wq and press Enter:
:wq
You're back in the terminal! Let's check that the file was saved:
cat hello.txt
Output:
Hello, Vim!
This is my first file.
Congratulations! You just created, edited, saved, and exited a file with Vim.
Try It Yourself
Open the file again:
vim hello.txt
Press i to enter Insert mode, then add any sentence on a third line. Then press Esc → :wq to save and exit.
This time, let's exit without saving:
vim hello.txt
Press i, type anything, then press Esc and enter :q!:
:q!
Run cat hello.txt to check. What you just typed is gone. :q! means discard changes and quit.
"Why?" — Why Learn Vim?
When you SSH into a server to edit a config file, there's often no GUI editor:
ssh production-server
# VS Code? Not installed
# Mouse? Not available
# All you have is Vim
vim /etc/nginx/nginx.conf
Vim is installed by default on almost every Unix/Linux system. You'll meet it naturally when managing servers, working inside Docker containers, editing Git commit messages, and more.
I can't exit Vim! (the famous meme)
"How to exit Vim" is a legendary Stack Overflow question with millions of views. Everyone hits this wall once. Don't panic!
Escape hatches:
| Command | Meaning |
|---|---|
:wq | Save and quit (write + quit) |
:q | Quit (only if no changes) |
:q! | Force quit without saving |
:w | Save only (stay in Vim) |
ZZ | Same as :wq (uppercase Z twice) |
The key is to press Esc first. No matter what mode you're in, Esc brings you back to Normal mode — and from there you can type commands starting with :.
vimtutor — the official practice program
Vim comes with a built-in tutorial called vimtutor:
vimtutor
About 30 minutes of hands-on practice opens right inside Vim. Running it alongside this course helps the keys settle into your fingers faster.
Deep Dive
- Create a new file with
vim practice.txt. - Press
ito enter Insert mode and type three of your favorite foods, one per line. - Press
Esc→:wqto save and exit. - Run
cat practice.txtto check the contents. - Open the file again with
vim practice.txt, add a line, then exit with:q!. When you runcat, the added line should be gone. - Try running
vimtutorin your terminal (optional).
Quiz
Which key do you press to start typing text in Vim?
You made changes by mistake. How do you discard them and quit?
How do you return to Normal mode from Insert mode?