The vim Text Editor — Quick Reference
Last Updated: 2025-01-01
3 min read
Overview
vim is a powerful text editor available on virtually every Linux system. While it has a learning curve, knowing the basics allows you to quickly edit configuration files, scripts, and other text files directly on your server without needing to download, edit locally, and re-upload.
Opening a File
vim filename.txt # Open an existing file (or create a new one)
vim +25 filename.txt # Open and jump to line 25
vim +/search filename.txt # Open and jump to first occurrence of "search"
Vim’s Two Main Modes
Vim operates in two primary modes:
| Mode | Purpose | How to enter |
|---|---|---|
| Normal mode | Navigate, delete, copy, paste | Press Esc |
| Insert mode | Type and edit text | Press i, a, o, or I, A, O |
When you open a file, you start in Normal mode. Press i to start typing (Insert mode), and press Esc to go back to Normal mode.
Essential Commands
Entering Insert Mode
| Key | Action |
|---|---|
i | Insert before cursor |
a | Insert after cursor |
o | Open a new line below and enter insert mode |
O | Open a new line above and enter insert mode |
I | Insert at beginning of line |
A | Insert at end of line |
Saving and Quitting (from Normal Mode)
| Command | Action |
|---|---|
:w | Save (write) the file |
:q | Quit (fails if unsaved changes exist) |
:wq | Save and quit |
:q! | Quit without saving (discard changes) |
ZZ | Save and quit (shortcut) |
Navigation (Normal Mode)
| Key | Action |
|---|---|
h j k l | Move left, down, up, right |
| Arrow keys | Also work for navigation |
0 | Jump to beginning of line |
$ | Jump to end of line |
gg | Go to first line of file |
G | Go to last line of file |
:25 | Jump to line 25 |
Ctrl+F | Page down |
Ctrl+B | Page up |
w | Jump forward one word |
b | Jump backward one word |
Editing (Normal Mode)
| Key | Action |
|---|---|
x | Delete character under cursor |
dd | Delete entire current line |
5dd | Delete 5 lines |
yy | Copy (yank) current line |
5yy | Copy 5 lines |
p | Paste below cursor |
P | Paste above cursor |
u | Undo last change |
Ctrl+R | Redo |
Searching
| Command | Action |
|---|---|
/pattern | Search forward for “pattern” |
?pattern | Search backward for “pattern” |
n | Go to next match |
N | Go to previous match |
Search and Replace
:s/old/new/ # Replace first occurrence on current line
:s/old/new/g # Replace all occurrences on current line
:%s/old/new/g # Replace all occurrences in entire file
:%s/old/new/gc # Replace all with confirmation for each match
Practical Hosting Examples
Edit your WordPress config
vim /home/username/public_html/wp-config.php
Edit .htaccess
vim /home/username/public_html/.htaccess
View a file read-only
vim -R /etc/nginx/nginx.conf
Or use view which is equivalent:
view /etc/nginx/nginx.conf
Quick Survival Guide
If you’re stuck in vim, remember:
- Press Esc (maybe a couple of times) to get back to Normal mode
- Type :q! and press Enter to quit without saving
- Type :wq and press Enter to save and quit
Tips
- If
vimfeels overwhelming, trynanoinstead — it’s a simpler editor with on-screen shortcut hints. - Use
:set numberin Normal mode to display line numbers. - Use
:set pastebefore pasting text from your clipboard to preserve formatting. - Practice on a test file before editing production configs.
Tags:
ssh
linux
vim
text-editor