LearnWithShobhan

nano for beginners: edit files in the terminal (and how to exit vim)

Shobhan Biswas··5 min read
>_From the course CLI Mastery: 57 lessons, lifetime access.

Sooner or later you will have to change a file on a server, and there will be no mouse and no VS Code. You need an editor that lives inside the terminal. The friendliest one is nano: it prints its own cheat sheet on the screen, so you can use it on day one.

This post covers nano from the first keystroke, why there are several terminal editors in the first place, and the one vim trick everybody needs.

Text editor vs word processor

A word processor (Word, Google Docs, Pages) stores formatting: bold, fonts, headings. A text editor stores only characters. No styling, nothing hidden.

Code, configuration files, scripts and plain notes are all plain text, so you use a text editor. VS Code and Sublime Text are text editors too. On a server you will use one that runs in the terminal.

Open a file in nano

nano notes.txt

If notes.txt does not exist, nano opens an empty buffer, and creates the file when you save. Run just nano to start with no file name.

The nano editor showing a short shopping list and the shortcut bar at the bottom with Ctrl+G Help, Ctrl+O Write Out, Ctrl+X Exit
The shortcuts are printed at the bottom. ^ means the Ctrl key, so ^X is Ctrl + X.

Type normally. The arrow keys move the cursor, Backspace deletes, Enter starts a new line. You can also click with the mouse in many terminals, but the keyboard always works.

The shortcuts you need

ShortcutWhat it does
Ctrl + OWrite Out: save the file
Ctrl + XExit nano
Ctrl + GOpen the help screen
Ctrl + KCut the current line
Ctrl + UPaste what you cut
Ctrl + WSearch for text
Ctrl + A / Ctrl + EJump to the start / end of the line

You do not have to memorise them. The two rows at the bottom of the screen show what is available. If you are ever lost, press Ctrl + G.

Save and quit

This is the part that surprises beginners. Press Ctrl + X after you changed something and nano asks:

Save modified buffer?
 Y Yes   N No   ^C Cancel

A buffer is the temporary copy of your text held in memory. Your edits are only in that buffer until you save them.

You pressResult
YSave. nano then asks for the file name, press Enter to keep it
NQuit without saving, your changes are gone
Ctrl + CCancel, go back to editing

If you opened a file by name, the name is already filled in. Type a different name at that prompt and nano saves a new file with your text, a quick way to duplicate a file. If you try to save over an existing name, it asks before overwriting.

You can also save without leaving: Ctrl + O, press Enter, and keep typing.

Why more than one terminal editor exists

When you read tutorials you will meet other names. Here is where they came from.

Timeline of terminal editors: ed leads to vi and Vim, Emacs from the 1970s, and Pine and Pico leading to nano
The editors on the left are powerful and hard to learn. nano was made for people who just want to edit a file.
  • ed is the oldest, written for early Unix when output went to a printer, not a screen.
  • vi and Emacs came with video terminals. Both are very powerful, and both have a steep learning curve. Emacs needs many key combinations, and vi works in modes.
  • Vim means "vi improved" and is installed on nearly every server.
  • nano descends from Pico, the simple editor in the Pine email program. It is made for people who do not want to memorise anything, which is why it prints the shortcuts for you.

The vim escape: how to quit when you did not choose it

Some commands (such as git commit without a message, or crontab -e on some systems) drop you into vim without asking. It looks like a normal terminal that does not respond to typing. Here is the survival kit:

StepKeys
You are stuckPress Esc first, to be sure you are not in insert mode
Quit, nothing changedType :q and Enter
Quit and throw away changesType :q! and Enter
Save and quitType :wq and Enter
Start typing textPress i, then type. Press Esc when done

Vim is worth learning later. For now this is enough to get out safely.

Set your default editor

Many tools open "your default editor". Tell them to use nano by adding this line to your shell config:

export EDITOR=nano

The config files (.bashrc, .zshrc) are explained in Customize your shell.

Key takeaways

  • ✓A text editor stores characters only. nano is a terminal text editor made for beginners.
  • ✓nano file opens or creates a file. The shortcuts are printed at the bottom, ^ means Ctrl.
  • ✓Ctrl + O saves, Ctrl + X exits. On exit, Y saves, N discards, Ctrl + C cancels.
  • ✓Edits live in a buffer until you save them.
  • ✓Stuck in vim? Press Esc, then :q! to quit or :wq to save and quit.

From editing one file to running a server

Editing files in the terminal is the first step toward managing real servers. CLI Mastery takes you from here to files, permissions, shell scripts, and finally an AWS server you connect to with SSH and configure yourself. 57 lessons, lifetime access. Try the free lesson on absolute and relative paths.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading