✏️ Lesson 10: Surviving a Text Editor

Edit files in the terminal with nano, and learn the four keystrokes that get you out of vi alive

Video: Text Editors: nano and vi (Linux 101 video)
Prefer to watch? This short video shows nano and vi on a real Ubuntu 24.04 machine, with on-screen captions and no voiceover. Click to play, or watch it on YouTube.

Why you need a terminal editor

Almost everything on a Linux system is controlled by text files: the settings for the web server, the list of users, the schedule of jobs. To change one you need to edit it, and on a server there is often no mouse and no desktop, only the terminal. So every administrator needs to be able to open a file, change it, save it and leave, without panicking.

Two editors are on nearly every Linux machine:

On a fresh Ubuntu Server, nano is already there. The minimal practice container used earlier in this series has neither, so for this lesson we installed both (sudo apt install nano vim-tiny, the way you learned in lesson 6). All screens below are real, captured from a running terminal. To keep them short, runs of empty lines in the middle of a screen have been shortened.

nano: the friendly one

  GNU nano 7.2                        notes.txt

                                  [ New File ]
^G Help      ^O Write Out ^W Where Is  ^K Cut       ^T Execute   ^C Location
^X Exit      ^R Read File ^\ Replace   ^U Paste     ^J Justify   ^/ Go To Line

Type nano notes.txt and the screen is taken over by the editor. If the file does not exist yet, nano says [ New File ] and creates it when you save. The top line shows the program and the file name, the big middle area is the file, and the two lines at the bottom are your cheat sheet.

The ^ in those lines means the Ctrl key. So ^X Exit means "hold Ctrl and press X". Nano has no hidden modes: you simply start typing and the text appears where the cursor is. The arrow keys, Home, End, Page Up and Page Down move around.

  GNU nano 7.2                        notes.txt *
Shopping list
eggs
bread
tea

^G Help      ^O Write Out ^W Where Is  ^K Cut       ^T Execute   ^C Location
^X Exit      ^R Read File ^\ Replace   ^U Paste     ^J Justify   ^/ Go To Line

We typed a heading and three lines. Look at the top: a * appeared after the file name. That star means "changed but not saved". Now save with Ctrl+O ("write Out"):

  GNU nano 7.2                        notes.txt *
Shopping list
eggs
bread
tea

File Name to Write: notes.txt
^G Help             M-D DOS Format      M-A Append          M-B Backup File
^C Cancel           M-M Mac Format      M-P Prepend         ^T Browse

Nano asks for the file name to write, with the current name already filled in. Press Enter to accept it:

  GNU nano 7.2                        notes.txt
Shopping list
eggs
bread
tea

                               [ Wrote 4 lines ]
^G Help      ^O Write Out ^W Where Is  ^K Cut       ^T Execute   ^C Location
^X Exit      ^R Read File ^\ Replace   ^U Paste     ^J Justify   ^/ Go To Line

[ Wrote 4 lines ] is the confirmation, and the star is gone. Leave with Ctrl+X, and the shell is back. Check what is in the file:

ubuntu@practice:~$ nano notes.txt
ubuntu@practice:~$ cat notes.txt
Shopping list
eggs
bread
tea
ubuntu@practice:~$

The file really contains what we typed. That is the whole core loop: open, type, Ctrl+O and Enter to save, Ctrl+X to exit.

nano: cut, paste, search and a safety net

Open the file again with nano notes.txt:

  GNU nano 7.2                        notes.txt
Shopping list
eggs
bread
tea

                                [ Read 4 lines ]
^G Help      ^O Write Out ^W Where Is  ^K Cut       ^T Execute   ^C Location
^X Exit      ^R Read File ^\ Replace   ^U Paste     ^J Justify   ^/ Go To Line

[ Read 4 lines ] tells you it loaded the existing file. Three tricks you will use constantly:

  GNU nano 7.2                        notes.txt *
Shopping list
bread
eggs
tea

^G Help      ^O Write Out ^W Where Is  ^K Cut       ^T Execute   ^C Location
^X Exit      ^R Read File ^\ Replace   ^U Paste     ^J Justify   ^/ Go To Line
  GNU nano 7.2                        notes.txt *
Shopping list
bread
eggs
tea

Search: tea
^G Help         M-C Case Sens   M-B Backwards   ^P Older        ^T Go To Line
^C Cancel       M-R Reg.exp.    ^R Replace      ^N Newer
  GNU nano 7.2                        notes.txt *
Shopping list
bread
eggs
tea

                        [ This is the only occurrence ]
^G Help      ^O Write Out ^W Where Is  ^K Cut       ^T Execute   ^C Location
^X Exit      ^R Read File ^\ Replace   ^U Paste     ^J Justify   ^/ Go To Line
  GNU nano 7.2                        notes.txt *
Shopping list
bread
eggs
tea

Save modified buffer?
 Y Yes
 N No           ^C Cancel

Y saves and leaves, N leaves and throws the changes away, and Ctrl+C cancels and takes you back into the file. We pressed N. The file on disk is exactly as it was before this session:

ubuntu@practice:~$ nano notes.txt
ubuntu@practice:~$ cat notes.txt
Shopping list
eggs
bread
tea
ubuntu@practice:~$

So in nano you can always change your mind. If you ever press something odd and the screen looks strange, Ctrl+C or Ctrl+X is the way out, and Ctrl+G opens the built-in help.

Files you may not change

System files belong to the administrator (root). Look what happens when an ordinary user tries to edit one:

ubuntu@practice:~$ ls -l /etc/hostname
-rw-r--r-- 1 root root 9 Oct  5 16:14 /etc/hostname
ubuntu@practice:~$ nano /etc/hostname
ubuntu@practice:~$ cat /etc/hostname
practice
ubuntu@practice:~$
  GNU nano 7.2                      /etc/hostname
practice

                     [ File '/etc/hostname' is unwritable ]
^G Help      ^O Write Out ^W Where Is  ^K Cut       ^T Execute   ^C Location
^X Exit      ^R Read File ^\ Replace   ^U Paste     ^J Justify   ^/ Go To Line

Nano warned us straight away: File '/etc/hostname' is unwritable. We typed a letter anyway and tried to save:

  GNU nano 7.2                      /etc/hostname *
xpractice

               [ Error writing /etc/hostname: Permission denied ]
^G Help      ^O Write Out ^W Where Is  ^K Cut       ^T Execute   ^C Location
^X Exit      ^R Read File ^\ Replace   ^U Paste     ^J Justify   ^/ Go To Line

Error writing /etc/hostname: Permission denied, the same message you met in lesson 5. We then left with Ctrl+X and N, and the file was untouched (the last command above shows practice, unchanged).

To edit a system file you need to run the editor with sudo: **sudo nano /etc/hostname** (from the documentation, not run here, because the practice container has no sudo; lesson 5 explained what sudo does). One good habit: before you change an important file, make a copy first with sudo cp /etc/hostname /etc/hostname.bak, so you can always put it back.

vi: how to get out alive

Now the one that surprises everybody. The vi editor has modes. At the start it is in normal mode, where the keys are not text but commands. To type text you must switch to insert mode. We prepared a small to-do file and opened it:

buy milk
walk dog
~
"todo.txt" 2 lines, 18 bytes

The file is shown with the cursor at the top and a ~ on every empty line. At the bottom: the file name, 2 lines, 18 bytes. There is no menu and no help line. Now a beginner does the natural thing: starts typing. We typed hello:

buy milk

walk dog
~

Nothing like "hello" appears! Each letter was a command. h, e, l, l moved the cursor around, and the final o means "open a new line below and start inserting", so there is now an extra blank line under buy milk. We pressed Esc and tried the polite way out, typing :q and Enter:

buy milk

walk dog
~
E37: No write since last change (add ! to override)

E37: No write since last change (add ! to override). This is the moment people give up and close the terminal window. Do not! The message tells you exactly what to do: **vi will not quit because there is something unsaved, and ! means "I mean it".** So:

The : opens a command line at the bottom of the screen. The first key to press, if you are ever lost, is Esc, which always takes you back to normal mode. Then type your : command.

We chose :q!. The file is as it was:

ubuntu@practice:~$ printf 'buy milk\nwalk dog\n' > todo.txt
ubuntu@practice:~$ vi todo.txt
ubuntu@practice:~$ cat todo.txt
buy milk
walk dog
ubuntu@practice:~$

Actually editing in vi

Now the right way. Open the file, then:

We pressed o, typed feed cat:

buy milk
feed cat
walk dog
~

Pressed Esc, then dd deleted the new line (the cursor was on it):

buy milk
walk dog
~

and **u** brought it back, with a message at the bottom saying what was undone:

buy milk
feed cat
walk dog
~
1 more line; before #2  2 seconds ago

Then :wq and Enter saved the file and quit:

ubuntu@practice:~$ vi todo.txt
ubuntu@practice:~$ cat todo.txt
buy milk
feed cat
walk dog
ubuntu@practice:~$

One more thing worth knowing: the small vi that Ubuntu installs by default does not display which mode you are in. The full vim shows -- INSERT -- at the bottom while you are in insert mode (from the documentation, not run here). If you are unsure, press Esc a couple of times. It never hurts.

How do you generate a random string?<br>
Ask someone to quit vim.<br>
(After this lesson, that someone is not you.)

Which one should I use?

Use nano whenever you have the choice. It is perfectly good for editing settings, and it tells you what to press. Learn the four vi survival keystrokes (Esc, **:q!**, **:wq**, **i** or **o** to type) because it is the editor that shows up on minimal systems and inside other tools. Many tools (like git, crontab -e and visudo) open your default editor. If you land in one you do not like, leave with the keys above, and on Ubuntu you can choose the default with the select-editor command (from the documentation, not run here).

🧪 Try it yourself

Use nano in your own home folder:

1. Create diary.txt with nano, type three lines, save and exit. Show it with cat. 2. Open it again, move the second line to the end of the file with Ctrl+K and Ctrl+U, and save. 3. Search for a word in it with Ctrl+W. 4. Make a change, then leave with Ctrl+X and answer N. Check that the change is not in the file. 5. Open the file with vi diary.txt, press o, type a line, press Esc and save and quit with :wq. Then open it again and leave with :q. 6. Type letters in vi without pressing i or o first, see what happens, press Esc and leave with :q!.

The cheat sheet

← Lesson 9 · ↑ All lessons · Lesson 11: Pipes, Redirection and Your First Script →