Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux vim and nano editors

Reference for the vim and nano terminal editors: modes, essential keys, saving and quitting, search and replace, the EDITOR variable and non-interactive editing.

vim and nano are the two terminal text editors you meet most often on servers. nano is modeless: you type and the text appears, and the shortcuts are listed at the bottom of the screen. vim (Vi IMproved, the successor of vi) is modal: the same keys do different things depending on the mode, which is hard at first but very fast once learned. A minimal vi is present on practically every Unix system, so knowing a few vim keys is a survival skill.

Programs such as git commit, crontab -e and visudo start the editor named in the VISUAL or EDITOR variable.

Terminal
vim file.txt
nano file.txt

Neither editor is installed on the runner behind the live examples here, and they need an interactive terminal, so the editor sessions are described rather than run. The runnable examples cover the parts that work without a terminal: choosing an editor and making the same edits in a script.

Try it

Bash Choosing the editor and editing without one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Making the same changes a vim session would

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Essential keys

vim starts in normal mode. Press a key to switch:

Keys Action
i, a, o Enter insert mode before the cursor, after it, or on a new line below.
Esc Return to normal mode.
:w, :q, :wq, ZZ Save, quit, save and quit, save and quit.
:q! Quit and discard changes.
h j k l or arrows Move left, down, up, right.
w, b, 0, $, gg, G Next word, previous word, line start, line end, file start, file end.
x, dd, yy, p, u, Ctrl+r Delete character, delete line, copy line, paste, undo, redo.
/text, n, N Search, next match, previous match.
:%s/old/new/g Replace in the whole file (add c to confirm each).
v, V, Ctrl+v Visual selection by character, line or block.
:set number Show line numbers.

nano uses Ctrl shortcuts; ^ means Ctrl and M- means Alt:

Keys Action
Ctrl+o Write out (save).
Ctrl+x Exit, asking whether to save.
Ctrl+w Search; Alt+w repeats it.
Ctrl+\ Search and replace.
Ctrl+k, Ctrl+u Cut the line, paste it.
Ctrl+g Open the help screen.
Alt+u Undo.
Ctrl+_ Go to a line number.

Usage notes

  • "I can't exit vim." Press Esc first, then type :q! and Enter to leave without saving, or :wq to save.
  • Typing letters does strange things means you are in normal mode: press i to insert text.
  • Permission denied on save in vim: :w !sudo tee % > /dev/null writes the buffer through sudo. Better is sudoedit file, which edits a temporary copy as you and copies it back.
  • Swap files. If an editor crashes, vim leaves .file.swp; reopening offers recovery. Delete the swap file only after you are sure you do not need it.
  • Set a default. Put export EDITOR=vim (or nano) and export VISUAL=$EDITOR in ~/.bashrc. On Debian-based systems sudo update-alternatives --config editor sets the system-wide default.
  • vi is often vim in disguise with a smaller feature set, so some vim features (visual block, syntax highlighting) can be missing when you run vi.
  • Configuration: ~/.vimrc for vim (for example set number, set expandtab, set tabstop=2), ~/.nanorc for nano (set linenumbers, set tabsize 2).
  • Learn with vimtutor, a 30-minute interactive lesson that ships with vim.
  • Scripts should not call interactive editors. Use sed, ed, awk or a here-document to change files non-interactively.