Lab 11: nano — Text Editing

Objective

Edit files with nano: open, navigate, write, save, search, and use keyboard shortcuts. nano is the easiest terminal editor to learn — ideal for quick config file edits on servers.

Time: 25 minutes | Level: Foundations | Docker: docker run -it --rm ubuntu:22.04 bash


Step 1: Install and Verify

apt-get update -qq && apt-get install -y -qq nano
nano --version | head -1

📸 Verified Output:

 GNU nano, version 6.2

Step 2: nano Interface Overview

When you open nano, the screen shows:

  GNU nano 6.2            filename.txt              Modified
  ─────────────────────────────────────────────────────────
  (your file content here)


  ─────────────────────────────────────────────────────────
  ^G Help    ^O Write   ^W Where   ^K Cut     ^X Exit
  ^F Find    ^R Replace ^T Execute ^U Paste   ^J Justify

💡 The ^ symbol means Ctrl. So ^X = Ctrl+X (exit). The shortcuts are always visible at the bottom — you never need to memorise them.


Step 3: Create and Edit a File

📸 Verified Output:

To edit this in nano:

Essential nano shortcuts:

Key
Action

Ctrl+O then Enter

Save (Write Out)

Ctrl+X

Exit

Ctrl+W

Search (Where is)

Ctrl+K

Cut entire line

Ctrl+U

Paste (Un-cut)

Ctrl+G

Help

Ctrl+/

Go to line number

Alt+U

Undo

Alt+E

Redo

Arrow keys

Navigate

Ctrl+C

Show cursor position


Step 4: nano Command-Line Options

💡 nano -w is important for editing config files where line wrapping would corrupt the syntax. Always use -w when editing /etc/nginx/nginx.conf, /etc/ssh/sshd_config, etc.


Step 5: Creating a Shell Script with nano

📸 Verified Output:


Step 6: Search and Replace in nano

Within nano, to search and replace:

  1. Press Ctrl+\ (on some systems Alt+R)

  2. Type search string → Enter

  3. Type replacement string → Enter

  4. Press A to replace all, or Y/N for each

To navigate to a specific line:

  1. Press Ctrl+/

  2. Type line number → Enter


Step 7: nano for System Configuration

📸 Verified Output:


Step 8: Capstone — Edit a Simulated NGINX Config

📸 Verified Output:


Summary

Shortcut
Action

Ctrl+O

Save file

Ctrl+X

Exit

Ctrl+W

Search

Ctrl+\

Search and replace

Ctrl+K

Cut line

Ctrl+U

Paste

Ctrl+/

Go to line number

Alt+U

Undo

nano -l

Show line numbers

nano -w

No line wrap

Last updated