Editing with nano & vim

nano is a friendly terminal editor; vim is powerful but modal. Both edit files right in the shell.

Sooner or later you need to edit a file on a server with no graphical editor. Two editors are almost always available.

nano — the easy one

nano file opens a simple editor. The commands are listed along the bottom; ^ means the Ctrl key. Save with Ctrl+O, quit with Ctrl+X.

vim — the powerful one

vim file is modal — it has separate modes for typing and for commands.

  • You start in Normal mode. Press i to start Insert mode and type.
  • Press Esc to return to Normal mode.
  • Type :wq then Enter to write (save) and quit. Type :q! to quit without saving.

Example

Example · bash
# Beginner-friendly editor
nano config.txt
#  Ctrl+O = save,  Ctrl+X = exit

# The classic modal editor
vim config.txt
#  i      -> start typing
#  Esc    -> stop typing
#  :wq    -> save and quit
#  :q!    -> quit without saving

When to use it

  • A sysadmin uses nano to quickly edit '/etc/hosts' on a minimal server that has no graphical editor installed.
  • A developer uses vim's ':s/localhost/0.0.0.0/g' command to bulk-replace a hostname in a config file without leaving the terminal.
  • A DevOps engineer edits a Kubernetes ConfigMap YAML in vim over SSH because it is the only editor available in the pod.

More examples

Edit a file with nano

Opens a file in nano and lists the most-used keyboard shortcuts shown in nano's own footer bar.

Example · bash
nano /etc/hosts
# Ctrl+O  - save (Write Out)
# Enter   - confirm filename
# Ctrl+X  - exit
# Ctrl+W  - search

Essential vim workflow

Demonstrates the mode-switching workflow that is essential to understand before using vim.

Example · bash
vim /etc/nginx/nginx.conf
# i        - enter Insert mode (start typing)
# Esc      - return to Normal mode
# :w       - save
# :q       - quit
# :wq      - save and quit
# :q!      - quit without saving

Vim search and replace

Shows vim's powerful search (/) and substitute (:%s) commands for finding and bulk-replacing text.

Example · bash
# Open file in vim then in Normal mode:
# Search forward
/error

# Replace all occurrences in file
:%s/localhost/0.0.0.0/g

# Replace with confirmation
:%s/http:/https:/gc

Discussion

  • Be the first to comment on this lesson.