Editing in the Terminal: nano
nano is a text editor that runs inside the terminal, where you open a file with nano and its name, move with the arrow keys, save with Ctrl+O and quit with Ctrl+X; and if vim opens by accident, Esc and then :q! get you out.
Novice · Unix · Linux · Updated
After this lesson you can
- Open, change, save and quit a file in nano with the arrow keys, Ctrl+O and Ctrl+X
- Leave vim without saving, with Esc and :q!, when a command opens it by accident
Key takeaways
- nano and a file's name opens that file in an editor inside the terminal, or starts a new file if there's none with that name.
- There's no mouse. You move with the arrow keys and type to change the text.
- Ctrl+O saves (press Enter to keep the name), and Ctrl+X quits. In nano's list of keys at the bottom, ^ means Ctrl.
- If a command drops you into vim, press Esc, type :q! and press Enter to leave without saving.
The new apartment's thermostat keeps the place at 78 degrees, and you'd like 70. Its settings live in a text file, one setting a line. Files like this run all sorts of things: a home server, a Raspberry Pi in a closet, a game server for friends, the server behind a website. Often the only way in is a terminal, with no mouse and no window to click. Changing one line there is a job for a text editor that runs inside the terminal, and nano is the one almost every Linux system has. IT techs and web developers use it to fix a setting on a server in seconds.
These lines make the practice thermostat's settings file, in the apartment from Deleting Safely: rm and rmdir, and print it:
mkdir -p ~/terminal-practice/apartment && cd ~/terminal-practice/apartment
printf '# Thermostat settings for the new apartment\ntemperature=78\nunits=F\nnight_mode=on\n' > thermostat.conf
cat thermostat.conf
# Thermostat settings for the new apartment
temperature=78
units=F
night_mode=on
A settings file like this is also called a configuration file, and its name often ends in .conf. The line starting with # is a note for people; the program skips it.
Open a file in nano
Type nano and the file's name:
nano thermostat.conf
GNU nano 8.7.1 thermostat.conf
# Thermostat settings for the new apartment
temperature=78
units=F
night_mode=on
[ Read 4 lines ]
^G Help ^O Write Out ^F Where Is ^K Cut ^T Execute ^C Location
^X Exit ^R Read File ^\ Replace ^U Paste ^J Justify ^/ Go To Line
That's a window 14 lines tall; nano fills yours. The top line shows nano's version and the file's name. The file's text fills the middle, a message line sits near the bottom, and the last two lines list nano's most useful keys. In that list, ^ (read "Control") means hold down Ctrl: ^X Exit means Ctrl+X quits. If you give nano a name that doesn't exist yet, it opens an empty page and makes the file when you save.
Change a line
There's no mouse here, so the arrow keys move the cursor. Press the down arrow once to get to temperature=78, then End to jump to the end of the line (Ctrl+E does the same, handy on a keyboard with no End key). Press Backspace twice to delete 78, and type 70. The text changes as you type, like in any editor.
Save with Ctrl+O, quit with Ctrl+X
Saving is called Write Out in nano. Press Ctrl+O, and nano asks where to write, with the file's own name filled in:
GNU nano 8.7.1 thermostat.conf *
# Thermostat settings for the new apartment
temperature=70
units=F
night_mode=on
Write to File: thermostat.conf
^G Help M-D DOS Format M-A Append M-B Backup File ^T Browse
^C Cancel M-M Mac Format M-P Prepend ^Q Discard buffer
The * after the name on the top line means "changed, not saved yet". Press Enter to keep the name, and the message line says [ Wrote 4 lines ]. Now press Ctrl+X to quit, and your prompt comes back. Check the change:
cat thermostat.conf
# Thermostat settings for the new apartment
temperature=70
units=F
night_mode=on
If you press Ctrl+X with changes you haven't saved, nano asks Save modified buffer? first (a buffer is nano's copy of the file while you edit). Press Y to save, N to throw the changes away, or Ctrl+C to go back to editing.
On a Mac, typing nano opens pico, a close cousin with a slightly different screen; Ctrl+O and Ctrl+X work the same way.
Landed in vim? Here's the way out
Some commands open an editor for you, and on many systems, a Mac included, that editor is vim, whose command is often vi. Running git commit without a message is a common way to land there. vim is a powerful editor, but its keys work differently from nano's, and the first time, getting out is the hard part. Practice the way out now, on purpose:
vi thermostat.conf
# Thermostat settings for the new apartment
temperature=70
units=F
night_mode=on
~
~
~
~
~
~
~
"thermostat.conf" 4L, 81B 1,1 All
The ~ lines are past the end of the file, and the bottom line shows its name, its size in lines (4L) and bytes (81B), and where the cursor is. (Some systems open a smaller vim for vi, with a plainer bottom line; the way out is the same.) To leave without changing anything:
- Press Esc. It stops whatever vim was doing, and pressing it twice does no harm.
- Type
:q!(read "colon q bang"). The colon starts a command,qmeans quit, and!means "even if there are unsaved changes". It shows on the bottom line as you type. - Press Enter. Your prompt comes back, and the file is as it was.
If you type :q without the ! after changing something, vim refuses with E37: No write since last change (add ! to override): add the !. To save your changes and quit instead, type :wq (write, then quit).
Check yourself
2 questions to try on the lesson page.
So what
nano is enough to change any settings file, on your own computer or on a server across the world, and knowing :q! means vim can never trap you.