Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Terminal Knowledge  ›  Capstone Project

Terminal Editors — Vim, Nano and Emacs

Vim / Nano / Emacs 12 minServers are edited in terminal editors: nano is simplest, Vim is almost always present and works in modes, and Emacs is modeless and powerful but rarely preinstalled.

Terminal Editors — Vim, Nano and Emacs

At 2 a.m. you SSH into web1 to fix one line in the nginx config. There is no graphical editor, no mouse and no VS Code. You get whatever terminal editor is installed, and you need to change the line, save and get out without panic.

The rule

In Vim you are in Normal mode until you ask to type.

Three editors dominate servers:

Nano is the gentlest: you type straight away and the shortcuts are listed at the bottom. Ctrl+O then Enter saves, Ctrl+X exits, Ctrl+W searches.

Vim (or its ancestor vi) is present on almost every Unix-like system, which is why it is worth learning. It is a modal editor:

  • Normal mode (where you start): keys are commands. dd deletes a line, u undoes, /listen searches for "listen", n jumps to the next match.
  • Insert mode: press i and keys type text. Press Esc to go back to Normal.
  • Command line: from Normal, : opens a prompt. :w saves, :q quits, :wq saves and quits, :q! quits and throws away changes.

Emacs is not modal: you type directly and commands use Control or Alt combinations, such as Ctrl+X Ctrl+S to save and Ctrl+X Ctrl+C to quit. It is powerful and extensible but rarely installed on servers by default.

Tools such as git commit, crontab -e and visudo open the editor named in the EDITOR environment variable, so you can choose:

export EDITOR=nano
sudo nano /etc/nginx/sites-available/notes
sudo vim /etc/nginx/sites-available/notes

The shortcut

Lost in Vim? Press Esc, then type a colon command. Esc always returns you to Normal mode, however many times you press it, and from there :wq or :q! always works.

Changing the port in vimStep 1 of 4
  1. Open

    sudo vim /etc/nginx/sites-available/notes. You are in Normal mode.

The trap: typing text while still in Normal mode. Letters become commands and edit random parts of the file. If that happens, press Esc and u until the damage is undone, or leave with :q!.

Drills

Answer fast — trust the rule.

quick drill

In Vim, which command quits and discards unsaved changes?

quick drill

You open a file in Vim and want to start typing text. Which key first?

quick drill

How do you save and exit in nano?

quick drill

Which editor are you most likely to find already installed on a minimal Unix server?

Remember

  • In Vim you are in Normal mode until you ask to type.
  • Shortcut: lost? Esc, then :wq or :q!.
  • Trap: typing text in Normal mode; set EDITOR if you prefer nano.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOne line at 2 a.m.

Explain, step by step, how you would change one port number in an nginx config file on a server using Vim, and how you would recover if you accidentally typed text in the wrong mode.