Vim (Vi Improved) is a powerful, highly configurable text editor used in Linux and Unix-like operating systems. It's a must-know tool for developers, system administrators, and anyone working in a terminal environment.
Vim is a modal editor. That means it has different modes for different tasks:
To open a file in Vim, use the following command in the terminal:
vim filename.txt
If the file doesn't exist, Vim will create it.
You start in Normal mode. From here, you can:
h, j, k, l (left, down, up, right).: to enter Command mode.i to enter Insert mode.
To start typing, press i (for Insert):
i – Insert before the cursor.a – Insert after the cursor.o – Open a new line below the cursor.dd – Yanks (similar to Ctrl+x) a line.p – Pastes that line you yanked/cut.x – Deletes.u – Undo.
To return to Normal mode, press Esc.
Press : to enter Command mode. You can:
:w – Save the file.:q – Quit Vim (without saving).:wq – Save and quit.:q! – Quit without saving.:/pattern – Search for text (press Enter to search).:%s/pattern/replacement/g – Replace all occurrences of a pattern.| Command | Description |
|---|---|
i |
Enter Insert mode |
Esc |
Return to Normal mode |
:w |
Save the file |
:q |
Quit Vim (without saving) |
:wq |
Save and quit |
:q! |
Quit without saving |
:set number |
Show line numbers |
:set nonumber |
Hide line numbers |
:s/pattern/replacement/g |
Replace all occurrences of a pattern |
:w filename.txt |
Save to a new file |
:vsp filename.txt |
Split the window vertically |
:sp filename.txt |
Split the window horizontally |
:w >> filename.txt |
Append to a file |
:e filename.txt |
Open a new file |
:help |
Open Vim's built-in help system |
vim mynotes.txt
Press i to enter Insert mode.
Type your text.
Press Esc to return to Normal mode.
Type :wq and press Enter to save and exit.
vim mynotes.txt
Press Esc.
Type :%s/old/new/g and press Enter to replace all instances of "old" with "new".
Vim is a powerful and flexible text editor that can be used for everything from writing code to editing configuration files. While it has a steep learning curve, it's worth the effort once you get the hang of it.
Vim is a must-have tool for anyone working in a Linux environment. It's fast, efficient, and can be customized to suit your workflow. With practice, you'll find it to be one of the most useful tools in your toolkit.