Table of Contents
- Description
- Getting Started with Vim
- Why Vim?
- Vim Modes Explained
- Editing Workflow in Vim
- Tips for Beginners
- Navigation Commands and Copy & Paste
- Other Useful Vim Commands
- Conclusion
Description
Learn how to use the Vim editor to create, read, append, and edit files. Understand its modes and commands with simple examples.
Getting Started
Create, Edit, and Navigate Like a Pro in vim.
Vim (Visual Improved) is a powerful text editor used in Linux and Unix-based systems. It's an enhanced version of the classic vi
editor and is known for being lightweight, fast, and highly customizable.
If you're working in DevOps, cloud environments, or learning Linux, knowing Vim will save you a ton of time especially when editing files directly on a remote server.
Why Vim?
- Available on nearly every Unix/Linux system
- Doesn’t rely on a GUI, perfect for SSH and remote work
- Efficient for coding, config editing, and scripting
- Highly customizable
Vim Modes Explained
There are three main modes in Vim
- Command Mode (default): Used to navigate, delete, copy, paste, and run commands.
- Insert Mode: Lets you type and insert text like in a normal editor.
-
Extended Mode (also called Last Line mode): Accessed via
:
to save, quit, and execute other commands.
Syntax and Usage
To open or create a file using Vim, use this syntax.
vim file1
If file1 doesn’t exist, Vim will create it.
Editing Workflow in Vim
Press i to enter Insert Mode and start typing.
Once done, press Esc to return to Command Mode.
Type :w and hit Enter to save the file.
Type :q and hit Enter to quit Vim.
Note: You can use :wq to save the file and quit from the vim.
You can also do *:q! * to quit
Tips for Beginners
Key/Command | Function |
---|---|
i |
Enter Insert Mode (start typing) |
Esc |
Return to Command Mode |
:w |
Write/save the file |
:q |
Quit Vim |
:wq |
Save and quit Vim |
:q! |
Quit without saving |
Navigation Commands and Copy & Paste
Once you're in Command Mode (press Esc
on your keyboard), here are some basic commands to help you edit and navigate files efficiently.
Editing Commands
Command | Description |
---|---|
yy |
Copy (yank) the current line |
p |
Paste below the current line |
dd |
Delete the current line |
x |
Delete the character under the cursor |
u |
Undo the last change |
Ctrl + r |
Redo the last undone change |
Navigation Commands
Command | Description |
---|---|
h |
Move left |
l |
Move right |
j |
Move down |
k |
Move up |
G |
Go to the last line of the file |
gg |
Go to the first line of the file |
:n |
Go to line number n (e.g., :15 ) |
Note: Make sure you're in Command Mode (press Esc
) before using these commands.
Other Userful Vim Commands
Once you're comfortable with the basics, here are a few more commands that can make working with Vim even more productive:
Useful Commands in Vim
Command | Description | Example |
---|---|---|
:set number |
Show line numbers | :set number |
:set nonumber |
Hide line numbers | :set nonumber |
/text |
Search for "text" in the file |
/server to find the word "server"` |
n |
Jump to next search match | After using /text , press n
|
N |
Jump to previous search match | After using /text , press N
|
:%s/old/new/g |
Replace all occurrences of "old" with "new" | :%s/error/fixed/g |
:w filename |
Save the file with a new name | :w notes_backup.txt |
:e filename |
Open another file in Vim | :e /etc/hosts |
:!command |
Run a shell command without leaving Vim |
:!ls to list directory contents |
Find and Replace
Let’s say you have a file with repeated instances of the word Linux
, and you want to replace it with Windows
.
:%s/Linux/Windows/g
Conclusion
Vim might seem tricky at first, but once you learn the basics, it becomes a powerful and fast tool for editing files right from the terminal.
With just a few commands, you can create, edit, save, and navigate files easily even on remote servers. Keep practicing, and it’ll soon feel natural.
Top comments (0)