vim — editing files in the terminal
The bare minimum for editing configs; Esc always returns to Normal mode, where letters are commands, not text.
Cheat sheet
8 commands Printable version-
ienter insert mode to type text -
Escgo back to Normal -
:wsave -
:qquit -
:wqsave and quit -
:q!quit without saving -
/textsearch forward through the file -
:%s/a/b/gcreplace across the whole file, confirming each replacement
Overview
vim/vi is available almost everywhere — knowing at least how to quit
and save is essential. The key idea is modes:
- Normal (the default) — navigation and commands;
- Insert — typing text (enter with
i); - Visual — selecting (enter with
v); - Command — commands through
:(save/quit).
Esc always returns to Normal.
Options
5Command-line flags, not in-editor commands: vim +42 file opens the file
straight at line 42, vim -R file opens it read-only (protection against
accidental edits), vim -u NONE starts without your personal
~/.vimrc — useful for checking whether your config is the cause of odd
behavior.
| Option | What it does | Example |
|---|---|---|
-R |
Open the file read-only — guards against accidental edits. | vim -R /etc/nginx/nginx.conf |
+N |
Open the file with the cursor on line N. | vim +42 file |
-u FILE |
Start with the given vimrc instead of the personal one; NONE skips config entirely. | vim -u NONE file |
-d |
Compare two files side by side (diff mode). | vim -d file1 file2 |
-p |
Open several files, each in its own tab. | vim -p file1 file2 |
Nothing found. Try a shorter query.
Recipes
3Navigation and editing
Useful bits
Troubleshooting
5I'm stuck, everything breaks as I type
You're in Normal mode, where letters are commands. Esc, then :q! to
quit without saving, and start over carefully (i to type).
Can't save: "E212: Can't open file for writing"
No permission (you didn't open it with sudo). Save with `:w !sudo tee %
/dev/null
, or quit with:q!andsudo vim file`.
Pasting with the mouse "staircases" the indentation
:set paste before pasting (Insert's auto-indent adds extra indentation
otherwise), then :set nopaste.
Accidentally made a mess of edits
u undoes step by step; :q! quits, discarding everything; :e!
rereads the file from disk.
Need something simpler
nano file — hints at the bottom (see nano). Fine for configs.
See also
- files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, copy-paste in the terminal.
- env-vars — env, export, $PATH, ~/.bashrc vs profiles; why "command not found".
Found an inaccuracy? Report an error in the article