Basic Vim Commands in Linux: Essential Guide for Beginners
In Linux, basic
vim commands include i to enter insert mode for typing, :w to save changes, :q to quit, and :wq to save and quit. Use Esc to switch from insert mode back to command mode where you can navigate and run commands.Syntax
Vim operates mainly in two modes: insert mode for typing text and command mode for commands and navigation.
Basic syntax to start Vim editing a file:
vim filename- opens or creates a file namedfilename.- Inside Vim, press
ito enter insert mode and start typing. - Press
Escto return to command mode. - Use
:wto save (write) changes. - Use
:qto quit Vim. - Use
:wqto save and quit.
bash
vim filename # Inside vim: # i # enter insert mode # <type your text> # Esc # return to command mode # :w # save changes # :q # quit # :wq # save and quit
Example
This example shows how to open a file, add text, save, and exit Vim.
bash
vim example.txt # Press i to enter insert mode # Type: Hello, Vim! # Press Esc to go back to command mode # Type :w to save # Type :q to quit
Output
Hello, Vim!
Common Pitfalls
Beginners often get stuck because Vim starts in command mode, so typing doesn't work until you press i to enter insert mode.
Another common mistake is trying to quit with :q without saving, which Vim blocks if there are unsaved changes.
Always press Esc before typing commands like :w or :q.
bash
Wrong way: # Type text immediately after opening vim (no effect) # Try :q without saving (error) Right way: # Press i to enter insert mode # Type text # Press Esc # Type :w to save # Type :q to quit
Quick Reference
| Command | Description |
|---|---|
| vim filename | Open or create a file |
| i | Enter insert mode to type text |
| Esc | Return to command mode |
| :w | Save (write) changes |
| :q | Quit Vim |
| :wq | Save and quit |
| :q! | Quit without saving changes |
| h, j, k, l | Move cursor left, down, up, right |
Key Takeaways
Press i to enter insert mode before typing text in Vim.
Use Esc to switch back to command mode to run commands.
Save changes with :w and quit with :q or :wq to save and quit.
If you want to quit without saving, use :q!.
Use h, j, k, l keys to navigate the text in command mode.