Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Text Editors for Linux: Nano and Vim Basics

Linux configuration work often means editing text files in the terminal. You do not need to master every editor, but you should be comfortable with one simple editor and one advanced editor.

Which Editor Should You Start With

  • Nano: easiest for beginners, clear key hints on screen.
  • Vim: powerful for long-term workflows once basics are learned.

A practical path is to start with Nano and learn core Vim actions over time.

Nano Essentials

Open a file:

nano /etc/hostname

Common actions:

  • Save: Ctrl + O
  • Exit: Ctrl + X
  • Search: Ctrl + W
  • Cut line: Ctrl + K
  • Paste line: Ctrl + U

Useful extras:

  • Go to line: Ctrl + _
  • Show line numbers: Alt + Shift + 3 (varies by terminal)
  • Read file into buffer: Ctrl + R

Vim Essentials

Open a file:

vim /etc/hostname

Core mode model:

  • Normal mode: navigation and commands
  • Insert mode: typing text
  • Command mode: save, quit, and file commands

Starter actions:

  • Enter insert mode: i
  • Save and quit: :wq
  • Quit without saving: :q!
  • Delete line: dd
  • Undo: u

Navigation and search basics:

  • Move by words: w, b
  • Jump to line: :42
  • Search forward: /text
  • Repeat search: n
  • Replace in file: :%s/old/new/g

If Vim feels confusing, run:

vimtutor

Editing as Root Safely

Avoid running your whole editor session as root when possible.

Prefer this workflow for protected files:

sudoedit /etc/ssh/sshd_config

sudoedit copies the file to a temporary location, opens it in your editor, then writes back with elevated permissions.

Safe Editing Workflow

Before editing important system files:

  1. Create a backup copy.
  2. Edit changes in small steps.
  3. Validate syntax if applicable.
  4. Restart or reload only after validation.

Example:

sudo cp /etc/fstab /etc/fstab.bak
sudo nano /etc/fstab
sudo mount -a

Common Beginner Mistakes

  • Editing with wrong permissions and forgetting sudo.
  • Leaving trailing spaces in config formats that are strict.
  • Making multiple changes at once and losing track of the root cause.

Choosing an Editor for Daily Work

  • Use nano when you need fast, low-risk config edits.
  • Use vim when you want speed, repeatable text operations, and remote workflow efficiency.
  • Use a GUI editor for long writing sessions or when formatting tools are integrated.

Keep changes small and test after each edit.

Key takeaways

  • Nano is fast to learn and ideal for first edits.
  • Vim becomes valuable once you learn its mode model.
  • Always back up critical configs before editing.
  • Small, testable edits reduce recovery time.
Last change: