Where to start First steps track · 15 stages
Linux Labs

vim — editing files in the terminal

4 min vim / vi

The bare minimum for editing configs; Esc always returns to Normal mode, where letters are commands, not text.

Cheat sheet

8 commands Printable version
  • i enter insert mode to type text
  • Esc go back to Normal
  • :w save
  • :q quit
  • :wq save and quit
  • :q! quit without saving
  • /text search forward through the file
  • :%s/a/b/gc replace 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

5

Command-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

3

Quitting and saving

i              enter insert mode (type text)
Esc            return to Normal
:w             save
:q             quit
:wq  or  ZZ    save and quit
:q!  or  ZQ    quit WITHOUT saving (discard edits)
:x             save (only if there were changes) and quit
:w !sudo tee % > /dev/null   save a file opened without permission (a trick)

Navigation and editing

h j k l        left/down/up/right   ( w/b — forward/back by word )
gg / G         to the start / end of the file  ( :N — to line N )
0 / ^ / $      to the start / to the first non-blank / to the end of the line
/text  Enter   search forward (n next, N previous); ?text — search backward
dd / yy / p    delete a line / copy / paste
dw / cw        delete / change a word   ( x — a character, D — to end of line )
3dd / 5j       a count before a command (delete 3 lines / move down 5)
v / V / Ctrl+v selection: character / line / block (then d, y)
u / Ctrl+r     undo / redo   ( . — repeat the last edit )
:%s/old/new/g  replace across the whole file   ( /gc — confirm each one )

Useful bits

:set number          line numbers ( :set nonumber — remove them )
:set paste           don't mangle indentation when pasting with the mouse
:noh                 clear search highlighting
:sp file / :vsp file split the window horizontally/vertically ( Ctrl+w + arrows to move )
ma … `a              set mark a and jump to it
:e!                  reread the file, discarding edits
gg=G                 auto-indent the whole file
~/.vimrc             config: set number / set expandtab / syntax on

Troubleshooting

5

I'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".
Loading…