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
ito start Insert mode and type. - Press Esc to return to Normal mode.
- Type
:wqthen Enter to write (save) and quit. Type:q!to quit without saving.
Example
# 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 savingWhen 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.
nano /etc/hosts
# Ctrl+O - save (Write Out)
# Enter - confirm filename
# Ctrl+X - exit
# Ctrl+W - searchEssential vim workflow
Demonstrates the mode-switching workflow that is essential to understand before using vim.
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 savingVim search and replace
Shows vim's powerful search (/) and substitute (:%s) commands for finding and bulk-replacing text.
# 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