Tutorial

Edit Files in the Terminal with nano and vim

Unni P
byĀ  Unni PĀ Ā·Ā on
Linux
Learn to change the contents of a file without a mouse. Start with nano, which works the way you expect, then meet vim, which does not, and find out how to get out of it. Includes the two shortcuts that unstick every beginner.

You can now move around the filesystem and create, move, and delete files. What you cannot do yet is change what is inside a file. That is what this tutorial is for.

On a server there is no Notepad and no mouse. You edit files with a program that runs inside the terminal itself. You will meet the two you will actually encounter:

  • nano, which behaves roughly how you expect a text editor to behave
  • vim, which does not, but which is installed on practically every Linux machine in existence

A configuration file has been prepared for you at /home/laborant/server.conf.

Part 1: nano, the friendly one

Opening a file

Type nano followed by a filename:

$ nano notes.txt

If the file exists, nano opens it. If it does not exist, nano starts an empty document and will create the file when you save. Either way, the terminal is replaced by the editor, which looks roughly like this:

  GNU nano 7.2                      notes.txt

ā–ˆ



                    [ New File ]
^G Help      ^O Write Out   ^W Where Is   ^K Cut
^X Exit      ^R Read File   ^\ Replace    ^U Paste

Reading the bottom bar

Those last two lines are the whole reason nano is friendly: it tells you the shortcuts as you work. You do not have to remember anything.

The one piece of decoding you need is the caret symbol. ^ means the Ctrl key. So ^O means hold Ctrl and press O. It is not a capital letter and there is no Shift involved.

The two that matter right now:

Shown asMeansDoes
^OCtrl and OWrite Out, which is nano's name for save
^XCtrl and XExit

Typing, saving, and leaving

Typing works exactly as you would hope. Letters appear, Enter makes a new line, Backspace deletes, and the arrow keys move around.

To save and leave:

  1. Press Ctrl and O. Nano shows the filename at the bottom and waits.
  2. Press Enter to confirm that name.
  3. Press Ctrl and X to exit back to your shell.

That middle step catches people out. Ctrl and O does not save immediately, it asks you what to call the file first. Pressing Enter accepts the name it is already suggesting.

Note

šŸ’” Since there is no separate command in this series for reading a file, opening it in nano and pressing Ctrl and X to leave without saving is a perfectly good way to look at what is inside something.

Your turn

Create a file at /home/laborant/notes.txt containing this exact sentence:

Linux is not so scary after all.
Show me the steps
cd /home/laborant
nano notes.txt

Type the sentence, then:

  • Ctrl and O, then Enter to save
  • Ctrl and X to exit

Part 2: vim, the one on every server

Why bother

nano is easier, so why learn vim at all? Because nano is not guaranteed to be installed, and vim very nearly is. Minimal server images, rescue environments and containers frequently ship with vim and nothing else. Knowing just enough vim to change a line and get out is a genuinely useful survival skill.

Just enough is the goal here. Nobody expects you to be fast at it.

The thing that makes vim different

Most editors have one mode: you type, and letters appear. vim has several, and it starts in one where typing does not insert text at all.

The two that matter:

ModeWhat typing does
NormalKeys are commands. Pressing d does not write a d, it starts a delete.
InsertKeys are text, exactly like nano.

vim opens in normal mode. This is the source of nearly all beginner confusion. You open a file, start typing, and the screen does bizarre things, because every keystroke is being read as an instruction.

Getting between the modes

        press i
NORMAL ---------> INSERT
       <---------
       press Esc
  • i switches from normal mode into insert mode, at the cursor. Think i for insert.
  • Escape switches back from insert mode to normal mode.

When you are in insert mode, vim shows -- INSERT -- at the bottom left. If you cannot see that, you are in normal mode.

Saving and quitting

From normal mode (press Escape first if unsure), type a colon. The cursor jumps to the bottom line, where you can type a command:

Type thisThen EnterResult
:wq↵Write and quit. Save and leave.
:q↵Quit, but refuses if you changed anything
:q!↵Quit and throw away changes. Your escape hatch.

:q! is worth memorising today. If you ever open vim, make a mess, and want out with nothing saved, press Escape and type :q! then Enter. You are never trapped.

Note

šŸ’” On this machine vim is configured to show line numbers down the left, counted relative to the line your cursor is on, so the numbers appear to change as you move. That is a deliberate setting, not a fault. There is also a status line at the bottom showing the filename, line and column. Stock vim on a fresh server usually shows none of this.

Your turn

Open /home/laborant/server.conf. It currently reads:

# Web server configuration
listen_port = 8080
max_connections = 100
timeout_seconds = 30
log_level = debug

Change the last line so that log_level is set to info instead of debug. Leave every other line exactly as it is.

Show me the steps
vim /home/laborant/server.conf

Then, in order:

  1. Use the arrow keys to put the cursor on the word debug
  2. Press i to enter insert mode, and check for -- INSERT -- at the bottom
  3. Use Backspace to delete debug, then type info
  4. Press Escape
  5. Type :wq and press Enter

If it goes wrong at any point, press Escape and type :q! then Enter to leave without saving, and start again.

Part 3: changing a file without wrecking it

Adding is not replacing

Here is a mistake beginners make constantly. They open a config file, want to add one setting, start typing immediately, and overwrite what was already on that line.

Adding a line means moving the cursor to the end, making a new line, and typing there. In vim, one handy way is the o key: pressed in normal mode, it opens a fresh line below the cursor and puts you straight into insert mode.

Your turn

Add this line to the end of /home/laborant/server.conf:

enable_cache = true

All five existing lines must still be there when you are done.

Show me the steps

In vim:

  1. Open the file, then press G (capital G) to jump to the last line
  2. Press o to open a new line below and enter insert mode
  3. Type enable_cache = true
  4. Press Escape, then type :wq and Enter

In nano it is simpler: arrow down to the bottom, press Enter for a new line, type, then Ctrl and O, Enter, Ctrl and X.

What you learned

nanovim
Opennano filevim file
Start typingJust typePress i first
Stop typingNot a thingPress Escape
SaveCtrl and O, then Enter:w
QuitCtrl and X:q
Save and quitCtrl and O, Enter, Ctrl and X:wq
Quit, discarding changesCtrl and X, then N:q!
Shortcuts shown on screenYesNo

The three things to actually remember:

  • ^ means Ctrl on nano's bottom bar, so ^O is Ctrl and O
  • vim starts in normal mode, where your keystrokes are commands, and i gets you into insert mode where they are text
  • :q! is the escape hatch. Escape, then :q!, then Enter leaves vim without saving anything, no matter what state you have got yourself into

Use nano when it is available and you just want the job done. Learn the handful of vim keys above for the machines where nano is not an option.

Next up: finding out what machine you are actually logged into, and who you are.

About the Author

Unni P

Unni P

Site Reliability Engineer

More tutorials you might like

Learn by doing, not just by reading or watching

Sign up for a free account to start a VM playground right on this page, track your progress, and get notified about new learning materials.

Sign up for free