When you SSH into a remote server, there is often no GUI editor available, but Vim is almost always installed. This guide covers the minimum you need to edit files confidently on a server.
1. Modes: The Core Idea
Vim is a modal editor. When you open a file, you start in Normal mode, where keys are commands rather than text. This is the main reason beginners feel lost.
| Mode | How to enter | Purpose |
|---|---|---|
| Normal | Esc |
Move, delete, copy, paste |
| Insert |
i (and others, see ยง4) |
Type text |
| Command-line |
: in Normal mode |
Save, quit, search & replace |
| Visual |
v / V / Ctrl+v
|
Select text |
Tip: If you ever get lost, press Esc a few times to return to Normal mode.
2. Survival Kit
Suppose you want to edit a file named train.py:
vim train.py open the file (creates it if it doesn't exist)
i enter Insert mode and start typing
Esc back to Normal mode
:w save
:q quit
:wq or :x save and quit
:q! quit WITHOUT saving (use it when you mess up)
With just these, you can already edit config files and scripts on a server.
3. Moving the Cursor (Normal Mode)
h j k l left / down / up / right (arrow keys also work)
w / b next word / previous word
0 / $ start / end of line
gg / G start / end of file
:42 or 42G jump to line 42
Ctrl+d / Ctrl+u scroll down / up half a page
% jump between matching brackets ( ) [ ] { }
4. Entering Insert Mode
i insert before the cursor
a insert after the cursor
I insert at the start of the line
A insert at the end of the line (very common)
o open a new line below
O open a new line above
5. Editing
x delete one character
dd delete (cut) the whole line
3dd delete 3 lines
dw delete a word
D delete to end of line
yy copy (yank) the whole line
p / P paste below / above
u undo
Ctrl+r redo
. repeat the last change (extremely useful)
The Grammar of Vim
Vim commands compose: operator + motion. d means delete and w means one word, so dw deletes a word. Once you see this pattern, you no longer need to memorize every command.
| Operator | Meaning |
|---|---|
d |
delete |
y |
yank (copy) |
c |
change (delete, then enter Insert mode) |
A few useful combinations:
y$ copy to end of line
d0 delete to start of line
dG delete to end of file
ciw change the word under the cursor
ci" change everything inside the quotes
ci( change everything inside the parentheses
dt) delete up to (but not including) the next )
6. Visual Mode
v select by character
V select by line
Ctrl+v select a block (columns)
After selecting, press d to delete, y to copy, or > / < to indent.
Comment out multiple lines:
- Move to the first line, press
Ctrl+v, then pressjto extend the selection down. - Press
I(capital i) and type#. - Press
Esc. Every selected line now starts with#.
7. Search and Replace
/lr search forward for "lr"
n / N next / previous match
* search for the word under the cursor
:%s/old/new/g replace every "old" with "new" in the file
:%s/old/new/gc replace with confirmation for each match
:noh clear search highlighting
8. Server Tips
Read-only viewing
view log.txt
# or
vim -R log.txt
This prevents accidental edits. For large logs, press G to jump straight to the end.
Multiple files
:vsp other.py split vertically (side by side)
:sp other.py split horizontally
Ctrl+w w switch between windows
Pasting code without broken indentation
When you paste code into Vim through a terminal, auto-indent can stack up and ruin the formatting. Turn on paste mode first:
:set paste before pasting
:set nopaste after pasting
Forgot sudo?
If you edited a system file and get a permission error when saving, you don't need to start over:
:w !sudo tee %
Common pitfalls
-
Screen frozen? You probably pressed
Ctrl+sout of habit, which pauses terminal output. PressCtrl+qto resume. -
"Found a swap file" warning? Vim creates a
.swpfile while editing. This warning appears if Vim crashed, your SSH connection dropped, or the file is open elsewhere. PressRto recover, orDto delete the old swap file if you're sure it's stale. -
Can't copy text to your local machine after
set mouse=a? HoldShiftwhile dragging to use the terminal's own selection instead.
9. Configuration: ~/.vimrc
~/.vimrc is Vim's config file. Vim reads it at every startup, so settings you put here apply automatically. The default config on most servers is minimal, so start with these:
syntax on " syntax highlighting
set number " show line numbers
set tabstop=4 " a Tab displays as 4 spaces wide
set shiftwidth=4 " indent by 4 spaces
set expandtab " insert spaces instead of Tab (essential for Python)
set autoindent " keep indentation on new lines
set hlsearch " highlight all search matches
set incsearch " search as you type
set mouse=a " enable the mouse
Text after " is a comment and is ignored by Vim.
To create the file:
vim ~/.vimrc
# press i, paste the config, press Esc, then type :wq
10. What's Next
-
Run
vimtutoron your server. It's an interactive tutorial bundled with Vim that takes about 30 minutes, and it's more effective than any article, including this one. - In the first week, stick to sections 2 through 5 and try not to use the arrow keys or mouse.
-
After that, deliberately practice composed commands like
ciw,dt), and..
For heavy development work, consider VS Code with Remote-SSH, and keep Vim for quick edits and log inspection. The two work well together.
Top comments (0)