Intermediate · 12 min · practise every command in the built-in terminal

vim — The Programmer's Editor

vim has a reputation for being hard to exit — but once you learn it, you'll be fast.

Opening files

vim filename.txt
vim +50 file.txt         # Open at line 50
vim +/searchterm file    # Open at first match

THE MOST IMPORTANT THING: Modes

vim has modes:

ModeHow to enterWhat it does
NormalEscNavigate, commands
Inserti a oType text
Visualv VSelect text
Command:Save, quit, search

Basic workflow

  1. Open file: vim file.txt
  2. Press i to enter Insert mode
  3. Type your text
  4. Press Esc to return to Normal mode
  5. Type :wq to save and quit

Essential Normal mode keys

h j k l        # Move left/down/up/right
w W            # Next word
b B            # Previous word
0 ^            # Start of line
$              # End of line
gg             # Top of file
G              # Bottom of file
50G            # Go to line 50
/text          # Search forward
n / N          # Next/previous match
dd             # Delete line
yy             # Copy line
p              # Paste
u              # Undo
Ctrl+R         # Redo

Insert mode entry points

i    # Insert before cursor
a    # Append after cursor
I    # Insert at line start
A    # Append at line end
o    # New line below
O    # New line above

Command mode (:)

:w          # Save
:q          # Quit
:wq or :x   # Save and quit
:q!         # Quit without saving
:w file     # Save as file
:50         # Go to line 50
:%s/old/new/g   # Replace all
:set number     # Show line numbers
:syntax on      # Syntax highlighting

Practice tasks

  1. Open vim, write 'Hello vim!', save and exit
  2. Open /etc/passwd in vim to inspect it, then quit without saving

Commands in this lesson

Advanced Commands