Linux Basics 2 · Lesson 1 · 40 min

Text editors: vim (and nano)

On a server there's no mouse and no Notepad. Every config file, script and cron job gets edited in the terminal. vim is on almost every Linux machine ever made, and once your fingers learn it, it's the fastest way to edit text there is. nano is the friendly one for quick jobs. This lesson makes you comfortable in both, starting with the famous question: how do I get out of vim?

You will learn

  • vim's big idea: modes (normal, insert, visual, command-line)
  • Saving and quitting: :w, :q, :wq, :q!, ZZ
  • Moving, deleting, copying and pasting without the mouse: hjkl w b dd yy p u .
  • Search and replace: /word, n, :%s/old/new/g, :g/pattern/d
  • nano for quick edits, and choosing your default editor with $EDITOR and sudoedit

vi, vim, and which one you have

vi is from 1976. vim ("Vi IMproved", 1991) is what you actually get today. On most systems vi starts a small vim.

Rocky / RHEL

A minimal install has vi (the vim-minimal package) but no vim command. Install the full one: sudo dnf install vim-enhanced. It brings vimtutor too. nano isn't installed by default either: sudo dnf install nano.

Ubuntu / Debian

Ubuntu Server comes with both vim and nano. Programs that open "your editor" (like crontab -e) use nano unless you choose otherwise.

The big idea: modes

vim starts in normal mode, where every key is a command. It's not for typing text. That's what confuses everyone at first, and it's also what makes vim fast: moving and changing text are single keys, with no Ctrl-this or mouse-that.

ModeHow you get thereWhat keys do
Normalwhere you start. Esc always brings you backmove, delete, copy, paste, undo
Inserti (before the cursor), a (after), o (new line below), A (end of line)type text, like any editor. Bottom line shows -- INSERT --
Visualv (characters) or V (whole lines)select, then d delete, y copy
Command-line: (or / to search)type a command, press Enter

Getting out (and saving)

:w       # write (save)
:q       # quit. Refuses if you have unsaved changes: E37
:wq      # save and quit (also :x or ZZ)
:q!      # quit and THROW AWAY changes

Stuck? Press Esc two or three times (you can't press it too often), then type :q! and Enter. That works from anywhere.

Moving around

h j k l      # left, down, up, right (arrow keys work too, but your hands stay home this way)
w  b  e      # next word, back a word, end of word
0  ^  $      # start of line, first character, end of line
gg  G        # first line, last line
:42          # line 42      (or 42G)
/error       # search forward for "error", then n = next, N = previous

Most commands take a count in front: 5j moves down 5 lines, 3dd deletes 3 lines, 2w jumps two words.

Changing text: verbs and motions

vim commands are little sentences: a verb (d delete, c change, y yank/copy) plus a motion (where to). Learn a few of each and they combine:

x      # delete one character
dw     # delete to the next word          cw   change a word (delete it, then type)
dd     # delete the whole line            yy   copy (yank) the line
d$     # delete to end of line (also D)   ciw  change the word under the cursor
p  P   # paste after / before the cursor (a deleted line pastes as a line)
u      # undo              Ctrl+R   redo
.      # repeat the last change, over and over (vim's secret weapon)

dd then p is how you move a line: delete cuts it into vim's register, and p puts it back somewhere else.

Search and replace

:%s/old/new/g        # every "old" in the whole file (% = all lines, g = every match on a line)
:s/old/new/          # just the first match on the current line
:10,20s/http:/https:/g   # only lines 10 to 20
:g/DEBUG/d           # delete every line that contains DEBUG
:v/ERROR/d           # delete every line that does NOT contain ERROR
:set number          # line numbers on (:set nonumber turns them off)

Put settings you always want in ~/.vimrc, for example a line set number. vim reads it every time it starts.

The best 30 minutes you'll spend

Run vimtutor. It's a vim lesson that happens inside vim, on a practice file you can't break. The practice terminal has a short version; a real system has the full one.

nano: the quick one

nano has no modes: you just type. The shortcuts are listed at the bottom of the screen, where ^ means Ctrl:

nano notes.txt
# Ctrl+O  save ("write out"), then Enter to confirm the name
# Ctrl+X  exit (asks to save if you changed something)
# Ctrl+W  search      Ctrl+K  cut a line      Ctrl+U  paste it

nano is perfect for a two-line change. vim wins for anything bigger, and it's the one you'll find on every server, container and rescue system.

Your default editor, and editing system files safely

Tools like crontab -e, git commit, systemctl edit and sudoedit open the editor named in the EDITOR variable:

echo 'export EDITOR=vim' >> ~/.bashrc
source ~/.bashrc
sudoedit /etc/motd

sudoedit is the safe way to edit root-owned files. It copies the file to a temporary file you own, opens your editor with your settings, and copies it back as root when you save and quit. The editor itself never runs as root. That's better than sudo vim, which runs a whole editor (that can run shell commands!) with full power.

Ubuntu's own way to choose

On Ubuntu, select-editor saves your choice for crontab -e, and sudo update-alternatives --config editor changes the system-wide editor command. EDITOR in your .bashrc works on both families.

Practice: fix everything, without a mouse ✍️

Your home folder has files that need editing: a poem, a text full of typos, a shopping list in the wrong order, an app config pointing at an old domain, and a noisy server log. The practice editor is a real (small) vim: counts, dd, p, u, ., visual mode, search and :%s all work.

Quick check

1. You opened a file in vim and pressed some keys. Now strange things happen and you just want out, without saving. What do you type?

2. What does :%s/http:/https:/g do?

3. Why is sudoedit /etc/hosts better than sudo vim /etc/hosts?

Finished the missions and the quiz? Mark it done to track your progress.