TExcelTAD
Home › Linux & Bash › nano & vi Editors

nano & vi Editors

Sooner or later you'll need to edit a file on a machine that has no graphical editor: a config file on a server, a commit message, a crontab. Two terminal editors are worth knowing: nano, which is simple, and vi/vim, which is installed on virtually every Unix system in existence.

Note: Editors are interactive programs: they take over the terminal and wait for key presses, so they can't run in this course's playgrounds. The keys are listed below to practise on your own machine; the runnable examples show how scripts edit files without an editor.

nano: the friendly one

Start it with nano FILE. You type text directly, and the most important shortcuts are listed at the bottom of the screen (^ means Ctrl):

Keys Action
Ctrl+O, Enter Save ("write Out")
Ctrl+X Exit (asks to save if there are changes)
Ctrl+W Search ("Where is")
Ctrl+\ Search and replace
Ctrl+K / Ctrl+U Cut line / paste
Ctrl+_ Go to line number
Alt+U Undo
bash
nano ~/.bashrc          # edit a file
nano +25 app.conf       # open at line 25
sudo nano /etc/hosts    # system files need root

vi / vim: the everywhere one

vi is modal: keys mean different things depending on the mode. This confuses everyone at first (the classic question is "how do I exit vim?"), but the basics fit in one table:

Mode How to enter What keys do
Normal Esc (the starting mode) Move, delete, copy, paste: commands
Insert i (before cursor), a (after), o (new line below) Typing inserts text
Command-line : from normal mode Commands such as save and quit

The survival kit:

Keys (normal mode) Action
i … type … Esc Insert text, then back to normal mode
:w Enter Save
:q Enter Quit (fails if there are unsaved changes)
:wq or ZZ Save and quit
:q! Quit without saving
h j k l Left, down, up, right (arrow keys work too)
gg / G / :42 First line / last line / line 42
dd / yy / p Delete (cut) line / copy line / paste
u / Ctrl+R Undo / redo
/word then n Search forward, next match
:%s/old/new/g Replace everywhere in the file

Tip: If you're ever stuck in vi, press Esc twice and type :q! Enter. Run vimtutor on a machine with vim installed for a 30-minute interactive lesson.

Choosing your default editor

Programs that need you to edit something (git commit, crontab -e, visudo, kubectl edit) start the editor named in the EDITOR (or VISUAL) environment variable. Scripts typically fall back to a default when it's not set:

bash · try it yourself
echo "editor: ${EDITOR:-nano (fallback)}"
export EDITOR=vim
echo "editor: ${EDITOR:-nano (fallback)}"
editor: nano (fallback)
editor: vim

To make the choice permanent, put export EDITOR=nano in your ~/.bashrc. For Git specifically: git config --global core.editor nano.

Editing files without an editor

Scripts can't press keys, so automation edits files with other tools. These one-liners cover most real cases:

Task Tool
Write a whole file cat > file <<'EOF' ... EOF
Append a line echo "line" >> file
Replace text sed -i 's/old/new/' file
Delete matching lines sed -i '/pattern/d' file
Insert after a line sed -i '/pattern/a new line' file
bash · try it yourself
cat > app.conf <<'CONF'
# application settings
port=8080
debug=true
log_level=info
CONF
cp app.conf app.conf.orig
sed -i 's/^port=.*/port=9090/' app.conf
sed -i '/^debug=/d' app.conf
echo "workers=4" >> app.conf
cat app.conf
echo "--- what changed:"
diff app.conf.orig app.conf
# application settings
port=9090
log_level=info
workers=4
--- what changed:
2,3c2
< port=8080
< debug=true
---
> port=9090
4a4
> workers=4

Keeping a copy of the original before editing, and checking the result with diff, is a good habit whether you edit by hand or by script.

Making an edit only once

Automation often runs repeatedly, so edits should be idempotent: running them twice gives the same result as once. Check before appending:

bash · try it yourself
printf 'export PATH="$HOME/bin:$PATH"\n' > profile.txt
line='export EDITOR=nano'
for attempt in 1 2 3; do
  grep -qxF "$line" profile.txt || echo "$line" >> profile.txt
done
cat profile.txt
export PATH="$HOME/bin:$PATH"
export EDITOR=nano

grep -qxF checks for the exact line (-x whole line, -F fixed text, -q quietly); only the first attempt appends it.

Which editor should I learn?

If you… Use
Just need to change a line on a server now nano
Work on servers daily Learn basic vim: it's always there, and it's fast once learned
Prefer a GUI VS Code with Remote - SSH edits server files in a local window

Test yourself

How do you quit vi without saving your changes?

  1. :wq
  2. :q!
  3. Ctrl+X
  4. ZZ

In nano, which shortcut saves the file?

  1. Ctrl+S
  2. Ctrl+O
  3. Ctrl+X
  4. :w

Which variable do git commit and crontab -e use to choose an editor?

  1. $SHELL
  2. $EDITOR
  3. $TERM
  4. $PATH