CodeTheRobot

Ubuntu Linux for Robotics · Lesson 08 of 96

Text editors: nano, vim and gedit

Editing files in the terminal and on the desktop, saving, quitting and switching.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain text editors: nano, vim and gedit in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

nano is the easiest terminal editor: type normally, Ctrl+O saves and Ctrl+X exits. vim is powerful and installed almost everywhere: it starts in normal mode, press i to insert text, Esc to return, :w to save and :q to quit (:wq does both, :q! quits without saving). gedit is a simple graphical editor.

On a robot you often edit files over SSH with no desktop, so learn at least nano well and enough vim to escape from it. Use sudo (sudoedit) to edit system files in /etc, and keep a backup copy before changing a configuration.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the text editors: nano, vim and gedit commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

nano ~/notes.txt
# Open a file in nano; type, then Ctrl+O to save and Ctrl+X to exit.
vim ~/notes.txt
# Open it in vim (normal mode).
# In vim: i (insert), type text, Esc, :wq then Enter
# Insert, leave insert mode, save and quit.
gedit ~/notes.txt &
# Open the graphical editor without blocking the terminal.
sudo cp /etc/hosts /etc/hosts.bak
# Back up a system file before editing it.
sudoedit /etc/hosts
# Edit a system file safely with your preferred editor.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Build and run

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

sourcecompilerprogramBUILD AND RUN

Demo for Text editors: nano, vim and gedit. Expected result: The file notes.txt contains three lines: robot, lab, ubuntu

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: open ~/notes.txt in nano, type the three lines robot, lab and ubuntu, save with Ctrl+O and exit with Ctrl+X
cat ~/notes.txt

Expected output

The file notes.txt contains three lines: robot, lab, ubuntu
Stuck? Show the full solution (try it yourself first)
nano ~/notes.txt
cat ~/notes.txt

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Create notes.txt in nano with three lines, open it in vim, add a fourth line and save, then quit vim without saving after a deliberate change (:q!). Confirm the file has four lines with wc -l.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.