DaleSchool

Terminal Text Editors

Beginner25min

Learning Objectives

  • Open, edit, and save files with nano
  • Understand vim's mode system and perform basic editing
  • Use essential vim Normal mode commands
  • Explain why terminal editors are needed on servers

Working Code

Example 1: Editing a file with nano

nano is the most intuitive terminal editor. Keyboard shortcuts are always shown at the bottom of the screen.

nano hello.txt

The file opens and you can start typing immediately:

Hello, terminal editor!
This is line two.

Save and exit:

  • Ctrl+O then Enter — save (Write Out)
  • Ctrl+X — exit

The ^ at the bottom means Ctrl.

Example 2: Useful nano shortcuts

Open nano and practice these while editing:

| Shortcut | Action | | -------- | --------------------- | | Ctrl+O | Save | | Ctrl+X | Exit | | Ctrl+K | Cut current line | | Ctrl+U | Paste | | Ctrl+W | Search | | Ctrl+G | Help | | Ctrl+/ | Go to a specific line |

Example 3: Getting started with vim — how to exit first

Opening vim for the first time can be intimidating. The most important thing to learn first is how to quit.

vim test.txt

vim opens. You're in Normal mode.

How to exit:

  • :q + Enter — quit (when no changes were made)
  • :q! + Enter — force quit without saving
  • :wq + Enter — save and quit

Pressing Esc multiple times always returns you to Normal mode.

Try It Yourself

vim's Mode System

The core of vim is modes. This is what makes it different from other editors.

Normal mode -> i (insert) -> Insert mode
Insert mode -> Esc        -> Normal mode
Normal mode -> v          -> Visual mode
Visual mode -> Esc        -> Normal mode
Normal mode -> :          -> Command mode

vim always starts in Normal mode.

Basic editing workflow:

vim myfile.txt
  1. i — enter Insert mode (-- INSERT -- appears at the bottom)
  2. Type your text
  3. Esc — return to Normal mode
  4. :wq + Enter — save and quit

Essential Normal Mode Commands

In Normal mode, keystrokes are commands. All cursor movement and editing happens via keyboard.

Movement

| Key | Action | | ----- | --------------------------- | | h | left | | l | right | | j | down | | k | up | | w | jump to next word start | | b | jump to previous word start | | 0 | jump to line start | | $ | jump to line end | | gg | jump to file start | | G | jump to file end | | :10 | jump to line 10 |

Editing

| Key | Action | | -------- | --------------------------------- | | i | Insert mode before cursor | | a | Insert mode after cursor (append) | | o | New line below + Insert mode | | O | New line above + Insert mode | | dd | Delete current line (cut) | | yy | Copy current line (yank) | | p | Paste | | u | Undo | | Ctrl+R | Redo | | x | Delete character under cursor |

Search and Replace

| Key/Command | Action | | --------------- | ---------------------- | | /word | Search forward | | ?word | Search backward | | n | Next search result | | N | Previous search result | | :%s/old/new/g | Replace all |

Visual Mode: Block Selection

v       -> character-wise selection
V       -> line-wise selection
Ctrl+V  -> rectangular block selection

After selecting:

  • d — delete
  • y — copy
  • > — indent
  • < — unindent

Real-world Scenario: Editing git commit messages

Running git commit without -m opens vim:

git config --global core.editor vim  # set vim as default editor

git commit  # vim opens

Writing a commit message in vim:

  1. i — Insert mode
  2. Type the message (feat: add new feature)
  3. Esc — Normal mode
  4. :wq — save and quit

If vim feels uncomfortable, switch to nano:

git config --global core.editor nano

"Why?" — Why You Need Terminal Editors

Servers don't have GUI editors like VS Code or Cursor. When you SSH into a server to edit config files or make quick changes, a terminal editor is essential.

Editor selection guide

| Scenario | Recommended Editor | | ---------------------- | -------------------------- | | Quick file edits | nano (easy and intuitive) | | Frequent server work | vim (available everywhere) | | Want powerful features | neovim (modern vim) | | Already using VS Code | code filename (locally) |

Why learning vim is worth it:

  1. Pre-installed on virtually every Linux/Mac server
  2. Hands never leave the keyboard — fast
  3. Full editing without a mouse
  4. Highly customizable

Common Mistakes

Mistake 1: Text won't type in vim

When vim opens, you're in Normal mode. Keystrokes are interpreted as commands.

# Typing "hello" in Normal mode:
# h -> move left
# e -> move to end of word
# l -> move right
# l -> move right
# o -> new line + Insert mode

Always press i to enter Insert mode before typing text.

Mistake 2: Trying to save without pressing Esc first

# Typing :wq in Insert mode
# <- ":wq" gets inserted as text

# Correct sequence
Esc -> :wq -> Enter

Mistake 3: Opening multiple files by accident

# When you accidentally open multiple files
# :q to close the current file and move to next
vim file1.txt file2.txt

:n  # next file
:N  # previous file

Deep Dive

vim config file (~/.vimrc)

Customize vim's default behavior:

" ~/.vimrc
set number          " show line numbers
set tabstop=2       " tab width 2 spaces
set expandtab       " tabs as spaces
set autoindent      " auto indent
set hlsearch        " highlight search results
set incsearch       " incremental search
syntax on           " syntax highlighting

Create the file if it doesn't exist: vim ~/.vimrc

neovim: Modern vim

neovim is a modern fork of vim with better defaults and a richer plugin ecosystem:

brew install neovim
nvim filename

Usage is identical to vim. It's becoming increasingly popular among developers.

Using VS Code as a terminal editor

In local development, you can set VS Code as your terminal editor:

git config --global core.editor "code --wait"

VS Code opens for git commit messages and closes when you're done.

Note: this doesn't work on remote servers (SSH).

  1. Open nano practice.txt, type 3 lines, save with Ctrl+O, and exit with Ctrl+X.
  2. Verify the contents with cat practice.txt.
  3. Open vim practice.txt, press G to go to the end, o to add a new line, then Esc -> :wq to save.
  4. In vim, try dd to delete a line, then u to undo.
  5. Search with /searchword and press n to jump to the next result.

Q1. What's the vim command to force quit without saving?

  • A) :exit
  • B) :q
  • C) :q!
  • D) :quit!