The everyday tools of software development: linux, the command line,
docker, git, c++, tmux, and neovim.
Linux
Linux keeps everything in one tree of folders: programs, settings, logs,
and hardware. A command is a program in one of those folders, and the
shell finds it by looking through the folders listed in PATH. PATH is
an environment variable: a named value that a terminal holds and gives
to every program it starts.
Two more errors are common. pip install fails with
externally-managed-environment on a new Ubuntu, because the system's own
programs use those Python packages. Install into a virtual environment
(python3 -m venv). Files made by a command run with sudo belong to
root, and sudo chown -R $USER <folder> gives them back.
The command line
Each command does one small job. A pipe, written |, sends what one
command prints into the next.
A running program is a process. Ctrl-C ends the one that holds the
terminal. Ctrl-Z freezes it, and it is still there.
Tab finishes a name, the up arrow brings back the last command, and
Ctrl-R searches the earlier ones. A name with a space in it has to be
quoted (cd "my folder"). A page of text that will not go away is open
in less, and q closes it.
Docker runs a second Linux computer inside ours. An image is a
saved copy of a system's files, with its software installed. A
container is a running copy of an image. A team shares one image, so
everyone has the same Ubuntu and the same ROS 2 on any laptop.
Two more errors are common. "Cannot connect to the Docker daemon" means
Docker is not running, and starting Docker Desktop fixes it. Images also
fill the disk: docker system df shows how much they take, and docker system prune deletes what no container uses.
Git
Git keeps the history of a project as a chain of commits. A commit
is a saved copy of every file, with a pointer to the commit before it. A
branch is a name that points at one commit, and making a commit moves
the name of the branch we are on.
Two branches are joined by a merge. When they changed different lines,
git joins them by itself. When they changed the same line it stops and
asks, which is a merge conflict.
Our example is a project whose file start_robot.py holds a robot's
settings. We change one on a branch, and then merge it.
A project that is also on a server (GitHub) has a second name for each
branch. origin/main is our computer's note of where the server's main
was. git fetch updates the note, git pull fetches and then merges,
and git push sends our commits.
Two more messages are common. "Your local changes would be overwritten"
means we have edits that are not in a commit. git stash sets them aside
and git stash pop brings them back. "Detached HEAD" means we asked for
a commit by its number and are on no branch. git switch main goes back.
To go deeperPro Git, chapter 3,
draws every step of branching and merging.
Learn Git Branching has the same
material as exercises in a browser.
C++
Robot software that has to be fast is mostly written in C++. It
differs from Python in three ways. It is built into a program before it
runs. A function is declared in one file and written in another. And a
function gets a copy of what it is given, unless we ask for the original.
Two more errors are common. A missing ; is reported on the line after
it, and one mistake can print a page of errors, so fix the first error
and build again. "Segmentation fault" means the program used memory that
is not its own, usually through a pointer that points at nothing.
tmux and Neovim
Everything above is standard. This part is my opinion: VS Code is for
noobs and people who love Microsoft bloatware. I write code in a terminal,
with Neovim as the editor and tmux splitting the screen.
It's faster. Any file or any line in a project is a few keys away, an
edit is a few keys, and my hands never go to the mouse. It's also the
easiest way I know to see how a codebase is laid out: the code, the
running program, and git sit side by side on one screen, and I can grep
the whole project as fast as I can type.
It's also the better setup for running AI agents. Each agent gets its own
pane, so I can watch several work at once, read what each one changed,
and step in from the same keyboard.
The setup in the figure is mine,
BaseNvim, and its README has
the install steps.
BaseNvim has no autocomplete and no debugger. I leave that work to
agents, which is easy when a codebase is set up with the right harnesses.
What I want from an editor is fast reading, so I can swap between
thoughts quickly.
To start, run nvim +Tutor in a terminal. It's a 30-minute lesson inside
the editor. Expect to be slow for a week.
Practice
Work each problem on paper before opening its solution.
Problem 1
It works in my terminal
A robot's computer has ROS 2 in /opt/ros/jazzy, and the last line of
~/.bashrc is source /opt/ros/jazzy/setup.bash. The file start.sh
starts the robot. It is a script: a file of commands that the shell
runs from top to bottom.
#!/bin/bash
ros2 launch robot.launch.py
The script is started in five ways. Two of them use commands that the
lesson does not: sudo runs a command as root, the user that is allowed
to do anything, and ssh robot connects to the robot's computer from
another one.
A. In a terminal on the robot: ./start.sh
B. In the same terminal: sudo ./start.sh
C. From a laptop, in one line: ssh robot ./start.sh
D. From a laptop: ssh robot, and then ./start.sh in the terminal
that opens
E. By the system, each time the robot is turned on
Say which of the five find ros2.
For each that does not, say why.
Change start.sh so that all five work.
Solution
Step 1: Ask who started it
A program is handed a copy of the environment variables of the program
that started it. ros2 is found only when PATH has ROS 2's folder in
it, and that folder is added by .bashrc. So for each way we ask what
started the script, and whether .bashrc had run there.
Way
Started by
.bashrc ran
Finds ros2
A
A terminal
Yes
Yes
B
sudo
Yes, but sudo does not pass PATH on
No
C
ssh, with no terminal to type in
It stops at its first lines
No
D
A terminal, opened by ssh
Yes
Yes
E
The system
No
No
Step 2: Explain the three that fail
B.sudo starts the program as root, and for safety it replaces
PATH with a fixed list of the system's folders.
C. With a command on its line, ssh runs that command and opens no
terminal. Ubuntu's .bashrc begins with a check for this, and stops
there:
# If not running interactively, don't do anythingcase $- in
*i*) ;;
*) return;;
esac
$- holds the shell's settings, and it has an i in it only when a
person is typing commands. Here nobody is, so .bashrc returns. The
source line is at the bottom, so it is never reached.
E. The system starts the script with no terminal at all, and
.bashrc belongs to terminals.
A script that needs something in its environment sets it up itself. Then
it does not matter what starts it.
Step 4: Check our own variables
The same rule covers a variable of our own. A variable is handed on only
once it is exported.
$ ROBOT=rover
$ ./start.sh # the script sees no ROBOT
$ export ROBOT
$ ./start.sh # now it does
Step 5: Run it
In a terminal where ros2 works, write the first start.sh with
ros2 topic list as its second line, so that it needs no launch file.
env -i starts a program with an empty environment, which is what the
system does in E.
chmod +x start.sh
./start.sh # A: prints the topics
sudo ./start.sh # B: ros2: command not found
env -i ./start.sh # as in E: ros2: command not found
Answer:
Two of the five find ros2. The script has to source the setup file itself
Problem 2
The script that deleted the wrong folder
A robot keeps its recordings on a second disk, which shows up as the
folder /data. The file clean.sh is a script: a file of commands
that the shell runs from top to bottom. It empties the folder of old
recordings. Its third line, rm -rf *, deletes everything in the folder
we are in, without asking.
One evening the second disk is not plugged in, so /data/old does not
exist. We are in ~/project, and we run bash clean.sh.
Write what the script prints, and say what it deletes.
A second version has no cd. It sets the folder at the top, with a
spelling mistake:
RECORDNGS=/data/old
rm -rf "$RECORDINGS"/*
Write out the command that the shell runs.
Add one line to the script that stops both.
Solution
Step 1: Run the script a line at a time
The cd fails and prints an error. A script does not stop at a line that
fails. It goes on to the next line, and we are still in ~/project.
clean.sh: line 2: cd: /data/old: No such file or directory
Old recordings deleted.
rm -rf * deleted everything in ~/project, and the last line reports
success.
Step 2: Put the variable's value in
RECORDINGS was never set, because the line above it set RECORDNGS. A
variable that was never set has an empty value and gives no error. With
the empty value put in, the command is
rm -rf /*
which deletes every folder on the computer that we are allowed to delete.
This has happened to a released program. In January 2015 the script that
starts Steam on Linux ran a line of this form with an empty variable, and
deleted the files of people who had moved one folder
(the report).
Step 3: Stop at the first failure
#!/bin/bashset -eu
-e stops the script at the first command that fails, and -u makes a
variable that was never set an error. With this line the first script
stops at the cd, and the second at the variable.
clean.sh: line 3: cd: /data/old: No such file or directory
clean.sh: line 4: RECORDINGS: unbound variable
Step 4: Compare with typing the commands
Going on after an error is right for a person at a terminal, who reads
each error before typing the next command. Nobody reads a script's
errors while it runs, so a script has to be told to stop.
Step 5: Run it
Only inside a container that can be thrown away:
docker run --rm -it ubuntu:24.04 bash
mkdir -p /demo/project && cd /demo/project && touch robot.py notes.txt
echo 'cd /data/old' > /demo/clean.sh
echo 'rm -rf *' >> /demo/clean.sh
bash /demo/clean.sh
ls # nothing is left
Answer:
Everything in the folder it was started from. One line, set -eu, stops it
Problem 3
Which file is the container running
start_robot.py prints version 1. On Monday we build an image named
my-robot from this Dockerfile.
FROM ros:jazzy
RUN git clone https://github.com/team/tools /tools
COPY start_robot.py /app/start_robot.py
RUN runs a command while the image is built, and here it downloads a
repository of tools. COPY copies a file from our folder into the image.
On Tuesday we change start_robot.py on our laptop so that it prints
version 2, and a teammate pushes a fix to the tools repository.
Say what is printed by each of these, in this order.
docker run my-robot python3 /app/start_robot.py
docker run -v ~/project:/app my-robot python3 /app/start_robot.py
docker build -t my-robot ., and then the command of 1 again.
Then say whether the container of 3 has the teammate's fix in /tools.
Solution
Step 1: Run the image as it was built
COPY copied Monday's file into the image. Editing the file on the
laptop changes nothing in the image, so 1 prints version 1.
Step 2: Share the folder
With -v, the container's /app is our ~/project, in place of what
the image has there. So 2 prints version 2. Every later save on the
laptop is seen in the container at once.
Step 3: Build again
A build does not start over. For each step it asks whether anything has
changed, and reuses Monday's result when nothing has.
For COPY, Docker compares the file, which changed, so that step runs
again and 3 prints version 2. For RUN, Docker compares only the text
of the line, which is the same as on Monday. The clone is not run again,
and /tools does not have the fix.
docker build --no-cache runs every step again.
Step 4: Choose between copying and sharing
Share the folder while writing the code, because every save is seen at
once. Copy the code into the image when the image has to run somewhere
our folder is not (the robot), and build it again for each change.
Step 5: Run it
RUN date stands in for the clone, because its result changes every
time it is really run. Put this in a Dockerfile in a new folder:
FROM ubuntu:24.04
RUN date > /built-at
COPY start_robot.py /app/start_robot.py
Two people make a branch from this commit on the same morning.
A teammate, on meters, changes the wheels to take meters a second.
Line 1 becomes MAX_SPEED = 0.5 # meters a second, and line 5 prints
"m/s".
We, on patrol, add a slow patrol at the end of the file.
defpatrol():
send_to_wheels(30)
patrol()
Each branch runs correctly by itself. meters is merged into main
first, and then we merge patrol.
Say whether git reports a merge conflict, and why.
Write what python3 robot.py prints after the merge.
Say what would have caught this before the robot moved.
Solution
Step 1: Compare the lines each branch changed
The teammate changed lines 1 and 5. We added lines after line 12. No line
was changed by both, so git joins the two and reports no conflict.
Step 2: Read the merged file
The merged file has the teammate's lines 1 and 5 and our patrol. When we
wrote 30 it was centimeters a second. The wheels now read it as meters a
second, which is 60 times the top speed.
Step 3: Say what git checked
Git compares lines of text. A conflict means that two branches changed
the same lines, and no conflict means only that they did not. The two
changes here disagree about what a number means, and that is not written
on any line that both of them touched.
Step 4: Catch it
Run the program after every merge and before pushing. One run shows this
fault.
The unit can also be put where the two changes collide. If the function
had been named send_cm_per_second, the teammate would have renamed it,
and our call to the old name would have stopped the program with an error
on its first run.
Step 5: Run it
mkdir merge-demo && cd merge-demo && git init -b main
# write robot.py, the first file above
git add robot.py && git commit -m "Drive at the top speed"
git switch -c meters
# change lines 1 and 5
git commit -am "Give the wheels meters a second"
git switch main && git switch -c patrol
# add the patrol at the end of robot.py
git commit -am "Add a slow patrol"
git switch main && git merge meters && git merge patrol
python3 robot.py
Answer:
No conflict, and the wheels are sent 30 meters a second
Problem 5
What is gone for good
Six accidents, each in a project kept in git.
We edit start_robot.py for an hour without committing, then run
git restore start_robot.py, which puts the file back as it was in
the last commit.
We commit the hour's work, then run git reset --hard HEAD~1, which
moves the branch back one commit and makes the files match it.
We commit on a branch faster, never merge it, and run
git branch -D faster, which deletes the branch.
We write a new file notes.txt, never run git add, and run
git clean -fd, which deletes every file that git does not track.
We commit every day for a week without pushing, then delete the
project's folder.
We run an experiment in a container, which writes results.csv in the
container's home folder, then run docker rm on the container.
For each accident:
Say whether the work can be got back.
Say where the work was kept when the accident happened.
For the ones that come back, give the commands.
Solution
Step 1: Name the places work can be
Work is in one of three places, and each is harder to lose than the one
before.
Place
What removes it
Only in a file
Any command that overwrites or deletes the file
In a commit on this computer
Deleting the project's folder, which holds the history in .git
In a commit on the server
Almost nothing we do on our computer
Step 2: Sort the six
Accident
The work was
Comes back
1. git restore
Only in a file
No
2. git reset --hard
In a commit on this computer
Yes
3. git branch -D
In a commit on this computer
Yes
4. git clean -fd
Only in a file, and git never knew of it
No
5. The folder deleted
In commits on this computer, and the folder held them
No
6. docker rm
Only in a file, inside the container
No
Step 3: Bring back the two commits
Deleting a branch, or moving it back, removes a name. The commits stay,
and git keeps a list of every commit we have been on, for at least 30
days. git reflog prints it.
$ git reflog
007c14f HEAD@{0}: reset: moving to HEAD~1
2843e95 HEAD@{1}: commit: two
007c14f HEAD@{2}: commit (initial): one
Each line is a commit's number and what we did to get there. For accident
2, git reset --hard 2843e95 puts the branch back on the lost commit.
For accident 3, the list shows the commit that faster pointed at, and
git switch -c faster 1bdda8b gives it a name again.
Step 4: Say the rule
A commit is hard to lose and a file is easy to lose. So we commit before
any command that has --hard, -D, -f, or rm in it, we push at the
end of each day, and in a container we write to the shared folder.
Step 5: Run it
mkdir lost-demo && cd lost-demo && git init -b main
echo one > f && git add f && git commit -m one
echo two >> f && git commit -am two
git reset --hard HEAD~1 # accident 2: f has one line
git reflog
git reset --hard HEAD@{1} # f has two lines again
Answer:
Four of the six are gone. The two that were committed, and are still on this disk, come back
Problem 6
Four lines that give the wrong number
This program compiles with no error and no warning.
Three things here are not in the lesson. A struct is a group of named
values. A std::vector is a list, and scan is an empty one. And
for (auto wheel : wheels) goes through a list, as for wheel in wheels
does in Python.
Its author expects it to print 0.2, 0, -1, and 0.
Write what it prints.
Fix each of the four.
Solution
Step 1: a. Two whole numbers divide to a whole number
1 and 2 are whole numbers, so 1 / 2 is worked out as whole numbers
and is 0. This happens before speed is looked at, and 0 times anything
is 0. The fix is 0.5 * speed, or speed / 2.
Step 2: b. The loop changes copies
auto wheel is a copy of each wheel, by the rule of the lesson: C++
copies unless we ask for the original. The loop sets the speed of two
copies to 0, and the wheels still hold 0.4. The fix is one character:
auto& wheel.
Step 3: c. A size cannot be negative
scan.size() has a type that holds only numbers from 0 up. The scan is
empty, and 0 minus 1 in that type wraps around to the largest number it
holds, 18446744073709551615. A loop that runs up to scan.size() - 1
would read far past the end of an empty scan. The fix is to compare with
the size and not with the size minus 1: i < scan.size().
Step 4: d. A variable starts with whatever was there
double total; gives total a place in memory and puts nothing in it.
The scan is empty, so nothing is added, and the program prints what that
memory last held. On our computer that was 6.71929e-322. Another
computer, or another build, prints another number. The fix is
double total = 0;.
Step 5: Ask the compiler
g++ says nothing about these unless asked. With -Wall -Wextra it
warns about b (variable 'wheel' set but not used), and with -O2
added, about d ('total' is used uninitialized). It says nothing about a
and c with either. Those two we have to know.