Prerequisites
Before using Vim, make sure you have:
- SSH access to your VPS
- Vim installed (or vi which comes pre-installed)
💡 Vim is a powerful modal editor with a steep learning curve but incredible efficiency once mastered.
Step 1: Install Vim (If Not Available)
ssh hxroot@YOUR_SERVER_IP -p 22
apt update
apt install vim -y
Step 2: Open a File
vim filename.txt
Or for system files:
sudo vim /etc/nginx/nginx.conf
Step 3: Understand Modes
Vim has multiple modes:
- Normal mode - Default, for navigation and commands (press ESC to return)
- Insert mode - For typing text (press i to enter)
- Visual mode - For selecting text (press v to enter)
- Command mode - For saving, quitting, searching (press : to enter)
Step 4: Basic Workflow
To start editing:
Press i (enter insert mode)
Type your text:
Return to normal mode:
Press ESC
Save and quit:
:wq
Essential Vim Commands
Navigation (Normal Mode)
- h j k l - Left, Down, Up, Right
- w - Jump to start of next word
- b - Jump to start of previous word
- 0 - Start of line
- $ - End of line
- gg - Start of file
- G - End of file
- Ctrl+F - Page down
- Ctrl+B - Page up
Insert Mode Entry
- i - Insert before cursor
- a - Append after cursor
- I - Insert at beginning of line
- A - Append at end of line
- o - Insert new line below
- O - Insert new line above
Editing (Normal Mode)
- x - Delete character under cursor
- dd - Delete current line
- dw - Delete word
- yy - Copy (yank) current line
- p - Paste after cursor
- P - Paste before cursor
- u - Undo
- Ctrl+R - Redo
- . - Repeat last command
Search and Replace
- /pattern - Search forward
- ?pattern - Search backward
- n - Next match
- N - Previous match
- :s/old/new/g - Replace in current line
- :%s/old/new/g - Replace all in file
Saving and Quitting (Command Mode)
- :w - Save file
- :q - Quit
- :wq - Save and quit
- :q! - Quit without saving
- :x - Save and quit (if changes exist)
Visual Mode for Selection
Enter visual mode:
Press v (character-wise) or V (line-wise)
Move cursor to select text.
Then:
- d - Delete selection
- y - Copy selection
- p - Paste over selection
- > - Indent right
- < - Indent left
Real-World Examples
Edit configuration file:
sudo vim /etc/ssh/sshd_config
/Port (search for Port)
i (enter insert mode)
Change port number
ESC (back to normal)
:wq (save and quit)
Replace all instances:
:%s/olddomain.com/newdomain.com/g
Delete 5 lines:
5dd
Copy 3 lines and paste:
3yy
p
Vim Configuration (~/.vimrc)
Create a basic vimrc:
set number " Show line numbers
set relativenumber " Show relative line numbers
set tabstop=4 " Tab width
set shiftwidth=4 " Indent width
set expandtab " Use spaces instead of tabs
set autoindent " Auto indent
set syntax on " Syntax highlighting
set hlsearch " Highlight search results
set ignorecase " Case insensitive search
set smartcase " Override ignorecase if uppercase used
set mouse=a " Enable mouse support
Save as ~/.vimrc
Common Vim Commands Cheat Sheet
| Action | Command |
|---|---|
| Save | :w |
| Quit | :q |
| Save & Quit | :wq or ZZ |
| Quit (no save) | :q! |
| Delete line | dd |
| Delete word | dw |
| Undo | u |
| Copy line | yy |
| Paste | p |
| Search | /pattern |
| Replace all | :%s/old/new/g |
Vim Tutor (Learn Vim Practically)
vimtutor
This is an interactive tutorial that teaches Vim basics in 30 minutes.
✅ You can now edit files using the powerful Vim editor on your Hostxpeed VPS.