Computing and the Command Line

vim Essentials

Why learn vi: it's on every Unix system, required by POSIX, and you'll land in it sooner or later. vim's modes (Normal, Insert, Visual, Command-line), how to get out (Esc, :q!), and how to save. Moving, deleting, copying, and pasting; undo and redo; search and search-and-replace. The verb-plus-motion grammar that makes vim fast, vimtutor for practice, and a minimal ~/.vimrc.

  • 5 min
  • 9 steps
  • 2 questions
  • Lesson 20 of 80

In this lesson

  1. Why bother with vi
  2. Modes
  3. Getting in and out
  4. Moving around
  5. Changing text
  6. The language of Normal mode
  7. Practice and a .vimrc
  8. Your turn
  9. So

Why bother with vi

You can do everything in this course with nano. So why learn vi too? Because it’s always there. The POSIX standard for Unix systems requires vi, so it’s on every server, rescue system, and minimal container, including ones without nano 1. Sooner or later some program (git, crontab -e, visudo) will drop you into it, and you need to know at least how to save and get out. And once learned, vim is very fast: you edit without leaving the home row of the keyboard 1 2.

vi was written by Bill Joy in 1976. Linux systems ship vim, “vi improved,” usually under both names 1.

Modes

vim is a modal editor: the same key does different things in different modes 2:

  • Normal mode, where you start: keys are commands (move, delete, copy). Typing x deletes a character.
  • Insert mode: keys type text, like any editor.
  • Visual mode: select text, then act on it.
  • Command-line mode: type a command after :, like :w to save.

Esc always takes you back to Normal mode 2. When in doubt, press Esc.

Center, Normal mode, where keys are commands. i goes to Insert mode to type text and Esc comes back; : goes to Command-line mode for :w, :q, :wq, :q!, and Enter or Esc comes back; v goes to Visual mode to select text and Esc comes back. Lower left, grammar, [count] verb motion: verbs d delete, c change, y copy; motions w, b, 0, $, gg, G, iw, ip; examples dw delete a word, 3dd delete 3 lines, c$ change to end of line, yip copy a paragraph. Lower right, a survival kit: h j k l move; x and dd delete a character or line; p paste; u and Ctrl-R undo and redo; /word and n search and find next; :%s/old/new/g replace all; Esc Esc :q! get out without saving; vimtutor, a 30-minute lesson.
Normal mode is home base. Everything else is one key away and Esc back. Credit: StudyCorner diagram · CC BY 4.0 · Source

Getting in and out

me@linuxbox:~$ vim notes.txt

The survival kit 1 2:

Keys Does
i Insert before the cursor (start typing)
a, A Insert after the cursor, at the end of the line
o, O Open a new line below, above, and insert
Esc Back to Normal mode
:w Save (“write”)
:wq or :x Save and quit
:q Quit (refuses if there are unsaved changes)
:q! Quit and throw away changes

Lost and want out without saving: Esc, Esc, :q!, Enter.

Quick check

You’ve opened a file in vim, typed some junk, and want to leave WITHOUT saving. What do you type?

Moving around

In Normal mode 1 2:

Keys Moves
h j k l left, down, up, right (arrow keys work too)
w, b forward, back a word
0, $ start, end of the line
gg, G top, bottom of the file
42G or :42 line 42
Ctrl-D, Ctrl-U half a page down, up

Changing text

The main editing commands, all in Normal mode 1 2:

Keys Does
x delete the character under the cursor
dd delete (cut) the line
yy yank (copy) the line
p, P paste after, before
u undo
Ctrl-R redo
. repeat the last change
J join this line and the next

Search with /word and Enter; n finds the next match, N the previous 1. Replace with a command 1:

:%s/oldword/newword/g      every match in the file (% = all lines, g = every match on a line)
:%s/oldword/newword/gc     the same, asking about each one

The language of Normal mode

What makes vim fast is that its commands combine. Many Normal-mode commands are a verb followed by a motion, optionally with a count 2:

  • Verbs: d delete, c change (delete and go into Insert mode), y yank (copy).
  • Motions: anything that moves, like w, b, $, 0, G, plus “text objects” like iw (inner word), i" (inside quotes), and ip (inner paragraph).

So dw deletes a word, d$ deletes to the end of the line, 3dd deletes three lines, ci" changes the text inside quotes, and yip copies a paragraph. Learn a few verbs and a few motions and you know dozens of commands 2.

Quick check

In Normal mode, what does d2w do?

Practice and a .vimrc

Run vimtutor. It’s a lesson that comes installed with vim, takes about half an hour, and is the best way to get the keys into your fingers 2. Then do your next few small edits in vim instead of nano.

A small ~/.vimrc makes it friendlier:

syntax on
set number
set relativenumber
set expandtab shiftwidth=4 tabstop=4
set ignorecase smartcase
set incsearch hlsearch

Your turn

Exercises

  1. Run vimtutor and work through at least lessons 1 and 2.
  2. Open ~/shell-course/shopping.txt in vim, add a line at the bottom with G then o, save and quit with :wq.
  3. Open it again, delete the first line with dd, then undo with u. Leave without saving.
  4. Replace every oat milk with whole milk, confirming each one.
  5. Make vim your editor for one command only: EDITOR=vim sudoedit /etc/hostname, then quit without saving.
Answers
  1. vim ~/shell-course/shopping.txt, G, o, type the item, Esc, :wq.
  2. gg, dd, u, then :q! (or just :q, since undoing restored the file to its saved state).
  3. :%s/oat milk/whole milk/gc, then y for each match.
  4. sudoedit opens vim on a copy of the file; :q! leaves it unchanged. EDITOR=vim before a command sets the variable just for that command.

So

vi is on every Unix system, so learn at least the way out: Esc, then :q! to quit without saving or :wq to save. vim starts in Normal mode, where keys are commands; i to type, Esc to come back. Its commands combine as verb plus motion (dw, 3dd, ci"), and vimtutor teaches the rest in half an hour.

Lesson complete

Nice work.

1day streak
0/1today's goal
–correct

Up next · 7 min

Your First Script

Next lesson
Sources for this lesson
  1. 1
    William Shotts. The Linux Command Line, Seventh Internet Edition (25.12A). LinuxCommand.org (print edition by No Starch Press). 2026. verifiedFree CC BY-NC-ND 3.0 book, release 25.12A of July 18, 2026. Part 1, Learning the Shell: the shell and terminal emulators, prompts ($ vs. # for the superuser), command history (most distributions keep the last 1,000 commands), Shift-Ctrl-C/V for copy and paste; navigation and the directory tree; exploring the system (ls options and the long listing, file, less, the guided tour of /, symbolic links); manipulating files (wildcards and character classes, mkdir, cp, mv, rm, ln; no undelete, test wildcards with ls first); working with commands (four kinds of commands, type, which, help, --help, man and its sections, apropos, whatis, info, alias); redirection; expansion and quoting; Readline keyboard tricks, completion, history search; permissions; processes. Later parts cover the environment, vi, packages, storage, networking, find, archiving, regular expressions, text processing, and shell scripting.
  2. 2
    Anish Athalye, Jon Gjengset, Jose Javier Gonzalez Ortiz. Editors (Vim) (The Missing Semester of Your CS Education, 2020). MIT CSAIL. 2020. verifiedCC BY-NC-SA. Vim is the most popular command-line editor and a modal editor: Normal (move and edit), Insert, Replace, Visual (plain, line, block), and Command-line modes; Esc returns to Normal. :w, :q, :wq, :e, :help. Vim's interface is a programming language: movements are nouns (hjkl, w b e, 0 ^ $, gg G, search /), edits are verbs (d delete, c change, y yank, p paste, u undo, Ctrl-R redo, x, ., o/O), counts multiply, and text objects (i( a( i") select. vimtutor comes installed with Vim.