Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

vim

Edit text files in the terminal with the vi/vim editor (keys and commands, no mouse needed).

Level
RHCSA exam
Group
Text and search
Needs root
No
Package
vim-enhanced (vi = vim-minimal)
Also called
vi, vimtutor

What it does

vim is on almost every Linux server (vi is the small version). It has modes:

  • Normal mode (command mode): keys are commands: move, delete, copy, paste. vim starts here.
  • Insert mode: keys type text. Enter with i, a or o; leave with Esc.
  • Command-line mode: commands after : like :w (save) or :q (quit), and searches after /.
  • Visual mode: select text with v (characters), V (lines) or Ctrl+V (block).

Most commands can take a number first: 5dd deletes 5 lines, 3w moves 3 words.

Learn the basics in 30 minutes with vimtutor.

How to write it

vim [OPTIONS] [FILE ...]
vim +NUMBER FILE
vim -d FILE1 FILE2

Main parts

Move (normal mode)

h j k l
Left, down, up, right (arrow keys also work).
w b e
Next word, back one word, end of the word.
0 ^ $
Start of the line, first character, end of the line.
gg G
First line, last line. 25G (or :25) goes to line 25.
Ctrl+F Ctrl+B
One page down, one page up.
%
Jump to the matching bracket.

Insert

i a
Insert before the cursor / after the cursor.
I A
Insert at the start / at the end of the line.
o O
New line below / above, in insert mode.
Esc
Back to normal mode.

Edit

x
Delete the character under the cursor.
dd
Delete (cut) the line. 5dd = 5 lines.
dw d$
Delete to the next word / to the end of the line.
cw cc
Change a word / change the whole line.
yy yw
Copy (yank) the line / a word.
p P
Paste after / before the cursor.
u Ctrl+R
Undo / redo.
.
Repeat the last change.
r
Replace one character.
>> <<
Indent / unindent the line.
J
Join the next line to this one.

Search and replace

/text ?text
Search forward / backward.
n N
Next / previous match.
*
Search the word under the cursor.
:s/old/new/
Replace the first "old" in this line.
:%s/old/new/g
Replace every "old" in the whole file.
:%s/old/new/gc
Replace everywhere, but ask each time.

Files and quitting

:w
Save. :w newname saves to another file.
:q
Quit (only when nothing changed).
:wq :x ZZ
Save and quit.
:q!
Quit and throw the changes away.
:w !sudo tee %
Save a file you opened without root (asks the sudo password).
:e file
Open another file.
:r file
Read (insert) a file under the cursor.

Settings

:set number
Show line numbers (:set nonumber to hide).
:set paste
Paste text without automatic indent.
:syntax on
Colors for code.
:set list
Show tabs and line ends (find hidden characters).

Options 28

Most used

+number
Open the file at this line. + alone = the last line.
+/pattern
Open the file at the first match of the pattern.
-R
Read-only mode (view). Same as the view command.
-d
Diff mode: show 2 to 8 files side by side with the differences.
-o[N], -O[N]
Open the files in N windows, one above the other / side by side.
-p[N]
Open the files in N tabs.
-c command
Run this ex command after the first file is loaded.
--cmd command
Run this command before any vimrc is read.
-n
No swap file (good for very big files or read-only media).
-r [file]
Recover a file after a crash, from its swap file.
-b
Binary mode.
-M
Do not allow changes (stronger than -R).
-m
Do not allow writing the file.
-u vimrc
Use this vimrc instead of the normal one (-u NONE = no settings).
-N
No compatible mode (normal vim behavior).
-C
Compatible mode (act like old vi).
-E
Start in improved Ex mode.
-e
Start in Ex mode.
-s
Silent mode (for scripts, with -e).
-y
Easy mode (works like a simple editor).
-Z
Restricted mode: no shell commands.
-x
Encrypt the file when writing (asks for a key).
-i viminfo
Use another viminfo file.
--clean
Start without your settings and plugins.
--noplugin
Do not load plugins.
-V[N]
Verbose messages (level N).
-h, --help
Show help.
--version
Show the version and the built-in features.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Open a file and save after an edit

    vim /etc/hosts
    (press i, type your line, press Esc, then type :wq and Enter)

    The basic cycle: i → type → Esc → :wq.

  2. 2Leave without saving when you made a mistake

    vim /etc/fstab
    (press Esc, then type :q! and Enter)

    :q! throws all changes away. Your safety exit.

  3. 3Open a file at a line number from an error message

    vim +42 /etc/httpd/conf/httpd.conf

    Error messages often say "line 42". Jump straight there.

  4. 4Replace a word in the whole file

    vim /etc/ssh/sshd_config
    :%s/^#PermitRootLogin yes/PermitRootLogin no/

    ^ anchors the match at the line start. Then :wq.

  5. 5Look at a file without changing it

    vim -R /var/log/messages

    Read-only: no risk of saving by mistake. Same as view.

  6. 6Compare two config files side by side

    vim -d /etc/ssh/sshd_config /etc/ssh/sshd_config.rpmnew

    Differences are colored. ]c jumps to the next difference, do takes the other side's version.

  7. 7Save a file you opened without sudo

    vim /etc/motd
    :w !sudo tee % > /dev/null

    % means the current file. Type the sudo password, then :q! to leave.

  8. 8Comment out many lines at once

    vim app.conf
    (Ctrl+V, move down with j, press I, type #, press Esc)

    Block insert puts # before every selected line.

  9. 9Show hidden tabs and spaces in a YAML file

    vim deploy.yaml
    :set list

    YAML breaks on tabs. :set list shows them as ^I.

  10. 10Recover a file after a crash or a lost SSH session

    vim -r notes.txt

    vim keeps a swap file (.notes.txt.swp). Delete the swap file after a good recovery.

  11. 11Run vim commands in a script (no screen)

    vim -Es -c '%s/8080/9090/g' -c 'wq' app.conf

    Batch edit without opening the editor. For simple jobs sed -i is easier.

  12. 12Learn vim in 30 minutes

    vimtutor

    A lesson file you edit while you learn. Do it once; it pays back every day.

Watch out

  • In insert mode, :wq is just text. Press Esc first.
  • "E37: No write since last change" means you tried :q with unsaved changes. Use :wq or :q!.
  • "Found a swap file" means the file is open elsewhere or vim crashed. Choose (R)ecover or (Q)uit, never edit twice at the same time.
  • Use visudo for /etc/sudoers and crontab -e for crontabs, not plain vim.

Files

~/.vimrc
Your vim settings (like set number).
/etc/vimrc
System-wide settings.
~/.viminfo
History of commands, searches and marks.

Tips to remember

  • Lost? Esc Esc :q!.
  • Remember the grammar: number + verb + move: d3w = delete 3 words, y$ = copy to the end of the line.
  • . repeats the last change. It saves a lot of typing.