Nano to Vim: editing files on a server without panic
Tags: Linux, Beginner
Intro – Why you need to be calm with nano and vim
When you SSH into a remote Linux box you’ll quickly discover that the two most common text editors are nano (simple, menu‑driven) and vim (powerful, modal). Both feel intimidating at first, especially when you need to edit a config file and then “save and quit.” This guide shows you the exact keystrokes you need, why they work, and how to avoid the classic panic moments that make beginners think they’ve broken the server.
It’s for anyone who:
- Has a basic Linux command line (can
ssh,ls,cat). - Has never used nano or vim, or can use one but not the other.
- Wants a quick cheat‑sheet that works on any distro (Ubuntu, CentOS, Debian, etc.).
Part 1 – Getting the editors on your server
| Editor | Install command (Debian/Ubuntu) | Install command (RHEL/CentOS) |
|---|---|---|
| nano | sudo apt-get update && sudo apt-get install -y nano |
sudo yum install -y nano |
| vim | sudo apt-get update && sudo apt-get install -y vim |
sudo yum install -y vim |
Tip: Most cloud images already ship with nano. If you’re on a minimal container you may need to install one of them first.
Part 2 – Nano basics: the “what you see is what you get” editor
2.1 Opening a file
nano /etc/hosts
When nano starts you’ll see a status bar at the bottom with shortcuts like ^G Help (^ means the Ctrl key).
2.2 The essential shortcuts
| Action | Keystroke | What you see |
|---|---|---|
| Save (write out) | Ctrl + O |
Prompt: File Name to Write: /etc/hosts – press Enter to confirm |
| Exit (quit) | Ctrl + X |
If the buffer is dirty, nano asks “Save modified buffer?” – press Y (yes) or N (no) |
| Cancel a prompt | Ctrl + C |
Returns you to the editor |
| Search | Ctrl + W |
Type the search term, Enter to jump |
| Cut line | Ctrl + K |
Removes the current line (stores in cut buffer) |
| Paste line | Ctrl + U |
Inserts the cut buffer at cursor |
2.3 A quick workflow
- Edit the file.
- Press
Ctrl + O, then Enter to write changes. - Press
Ctrl + Xto leave.
If you accidentally press Ctrl + X before saving, nano will ask if you want to save – just answer Y and you’re back on track.
Part 3 – Vim fundamentals: the modal editor
Vim has three main modes you need to know:
| Mode | How to enter | How to leave |
|---|---|---|
| Normal (navigation) | Open a file (vim file.txt) – you start here. |
– |
| Insert (typing) | Press i (or a, o, etc.) |
Press Esc |
| Command‑line (ex commands) | Press : while in Normal mode |
Press Enter after the command |
3.1 Opening a file
vim /etc/hosts
You land in Normal mode. The cursor is a block, not a blinking line.
3.2 The essential commands
| Action | Normal‑mode keystroke | Command‑line (:) |
What you see |
|---|---|---|---|
| Enter Insert mode | i (insert before cursor) |
– | You can type text; bottom shows -- INSERT -- |
| Save (write) | – | :w |
No output if successful |
| Quit (exit) | – | :q |
If there are unsaved changes, Vim warns |
| Save + quit | – | :wq or :x |
Returns to shell |
| Quit without saving | – | :q! |
Forces exit |
| Undo | u |
– | Reverts last change |
| Redo | Ctrl + r |
– | Re‑applies undone change |
| Search forward | /pattern then Enter |
– | Highlights match; press n to go to next |
| Search backward | ?pattern then Enter |
– | Press N for previous |
3.3 A minimal workflow
- Open the file (
vim /etc/hosts). - Press
ito start inserting. - Make your changes.
- Press
Escto return to Normal mode. - Type
:wqand hit Enter.
If you forget to type : before wq, Vim will treat wq as normal‑mode keystrokes (moving the cursor). Just press Esc, then :.
Part 4 – When to choose nano vs. vim
| Situation | Best editor | Why |
|---|---|---|
| You need to edit a file quickly and see the shortcuts on screen | nano | All commands are shown at the bottom; no mode switching. |
| You’ll be editing the same file repeatedly, need macros or search/replace | vim | Powerful patterns, registers, and plugins. |
| You’re on a very low‑resource VM (e.g., Alpine) | nano (smaller binary) | Vim may pull extra dependencies. |
| You want to learn a skill that many sysadmins expect | vim | Appears in most production environments. |
Hands‑on example – Edit /etc/hosts with both editors
Below is a complete, copy‑and‑paste script you can run on any Linux VM. It demonstrates opening the file, adding a line, saving, and exiting with both nano and vim.
#!/usr/bin/env bash
set -euo pipefail
# 1. Ensure both editors are present (ignore errors if already installed)
if command -v apt-get >/dev/null; then
sudo apt-get update -qq
sudo apt-get install -y -qq nano vim
elif command -v yum >/dev/null; then
sudo yum install -y -q nano vim
fi
# 2. Show the original /etc/hosts (read‑only)
echo "=== Original /etc/hosts ==="
cat /etc/hosts
echo "==========================="
# 3. Use nano in a non‑interactive way: echo a line into a temporary file,
# then open nano, write out, and quit automatically via a here‑document.
TMPFILE=$(mktemp)
cat <<'EOF' > "$TMPFILE"
127.0.0.1 localhost
# Added by tutorial
127.0.1.1 myserver.example.com
EOF
# Replace /etc/hosts with our prepared file using nano's "writeout" feature.
# The -c flag forces nano to start in "restricted" mode where it reads the file
# but we can still write out with Ctrl‑O via a simulated keypress using `xdotool`.
# For automation we simply copy the file (demonstration purpose).
sudo cp "$TMPFILE" /etc/hosts
echo "Edited /etc/hosts with nano (simulated)."
# 4. Verify the change
echo "=== After nano edit ==="
grep myserver /etc/hosts || echo "Line not found!"
# 5. Now edit the same file with vim in a scripted way.
# We'll use `ex` (vim's command‑line mode) to append a comment.
sudo ex -sc 'normal Go# Added by vim script' -sc 'wq' /etc/hosts
echo "Edited /etc/hosts with vim (ex mode)."
# 6. Final view
echo "=== Final /etc/hosts ==="
cat /etc/hosts
What the script does
- Installs
nanoandvimif they’re missing. - Prints the current
/etc/hosts. - Creates a temporary file with the desired content and copies it over (this mimics a nano “write out”).
- Uses
ex(the command‑line version of vim) to append a comment line, showing how you can automate vim edits without opening the full UI. - Displays the final file so you can verify the changes.
Run the script with:
chmod +x edit_demo.sh
./edit_demo.sh
You should see the added lines and no errors.
Common mistakes / troubleshooting
- Pressing the wrong key in vim’s Normal mode – If you type
wqwithout a leading:, vim moves the cursor. Fix: always pressEscfirst, then type:. - Nano says “File exists, overwrite?” – This appears when you open a new file with
Ctrl + Oand the name already exists. Press Y to confirm, or choose a different name. - Vim won’t quit because of “No write since last change” – You have unsaved edits. Use
:wqto write and quit, or:q!to discard changes. - Screen flickers or characters disappear after exiting nano – Some terminals need a reset. Run
resetafter quitting, or use a modern terminal emulator (e.g., GNOME Terminal, iTerm2). - File permissions prevent saving – Editing
/etc/hostsas a regular user fails. Prefix the editor withsudo(sudo nano /etc/hostsorsudo vim /etc/hosts).
Try it yourself
- SSH into any Linux box you control.
- Create a test file:
touch ~/test.txt. - Open it with nano, add the line
Hello from nano, save, and quit. - Re‑open the same file with vim, change the line to
Hello from vim, then save and quit. - Verify the final content with
cat ~/test.txt.
If the file shows “Hello from vim,” you’ve mastered the basic workflow!
What’s next
- Vim navigation deep dive – motions (
h j k l,w,b,%) and visual mode. - Customizing nano – edit
~/.nanorcto enable syntax highlighting. - Learning Vim plugins – install
vim-plugand add a file‑browser like NERDTree. - Using screen or tmux – keep your editor sessions alive across SSH disconnects.
Bookmark this guide for your next SSH session!