Keeping Track of Change

Have you ever wanted to turn back time after making a mistake and irrevocably damaging a file you were editing? You can do so with minimal effort after reading this article.

For years, software developers have been keeping track of all their changes to programs they are developing with what they call “version control” software. However, even competent developers have sometimes found it too troublesome to use these tools except where they find it absolutely necessary, and so it is not surprising that very few non-developers have used these tools at all.

The reason that they find these tools so difficult to use is that the rules for how to use them are written with the most complicated situations in mind. For most personal use of these tools, that is like learning to fly a jet aircraft in order to drive a car. There is an easier way.

The assumption that makes everyone do so much extra work is that more than one person will be trying to modify the file at the same time. For your own personal files, or for keeping track of changes to system files when you are the only system administrator, that is not an issue.

The standard program for doing version control under Linux (and most other versions of Unix) is GNU's RCS, which stands for Revision Control System. It has many options, but you hardly need to know about any of them to make good use of it. You can treat RCS as one of the simplest tools on your system.

In fact, you can keep track of all the changes that you ever make to a file with one command. Run the command ci -l filename each time you make a change to the file (“ci” stands for “check in”; you are “checking in your changes”). The first time you do this, you will be given the option to describe the file:

$ ci -l foo
foo,v  <--  foo
enter description, terminated with single '.'
  or end of file:
NOTE: This is NOT the log message!

You are not required to describe the file, but you can if you like. Then type a . character on its own line, or press ^D on an empty line.

When you have made a change to the file, you need type only one command to tell RCS to keep track of that change for you:

$ ci -l foo
foo,v  <--  foo
new revision: 1.2; previous revision: 1.1
enter log message, terminated with single '.'
  or end of file:

Here, you may wish to describe the change you have just made, especially if you think you will want to examine the change at some future time, but it is not required.

Remembering that single command is enough to prepare you for disaster. The rest of this short article will show you a few easy tricks that make RCS a little more comfortable to use, and help you learn how to recover from disaster.

Even Easier?

If you think running one command each time you finish editing a file is still too much work, we can make it even easier for you! (Laziness is a virtue if it causes you to work smarter instead of harder.)

We'll use a shell script. You can create a file in your own “bin” directory (if your account is on a computer shared with other people) or in /usr/local/bin (if you are using your own computer). Whichever directory you choose must be listed in your PATH. To make it easy to type, we'll call it et, short for edit text.

# et - Edit Text file, while keeping
#      track of changes with RCS
[ -z "$1" ] && {
  # No file specified on the command line
  echo "Edit what file?"
  exit 1
${EDITOR-vi} $1
# Only check in the file if it exists.
[ -f $1 ] && ci -l $1

Type that into a file named et, without making any typos. Then run the command chmod +x et to make the file executable. Now, you will be able to run et filename to edit and then automatically check in your changes.

The #!/bin/bash line tells Linux this script is a shell script that is interpreted by the bash shell, which comes as a standard part of every Linux distribution. All the other lines starting with a # character are comments, and are ignored by bash.

If you decide that you will never want to include a description of the changes you have made, you can change the last line of the script to

[ -f $1 ] && echo "." | ci -l $1

The reason for the [ -f $1 ] part is so that if you type et mistake, and then quit the editor without saving the file, you won't get error messages when ci finds that there is no file for it to check in.

The ${EDITOR-vi} part runs your favorite editor, or if you have not chosen a favorite editor, it defaults to the old standard vi editor. You can choose a default other than vi; pico, jed, joe, Emacs, and other editors are all possibilities. For example:

${EDITOR-jed} $1

will run jed, unless you have chosen some other favorite editor.

To choose your favorite editor, which will apply to all programs that want you to choose an editor, not just et, you will need to set the EDITOR environment variable to the name of the editor you want to use. If you are using a bourne shell, such as bash, zsh, pdksh, ksh, or sh, you will want to add a line like the following to the file .profile in your home directory:

EDITOR=jed ; export EDITOR

If you use the C shell (csh or tcsh), you will want to add a line like the following to the file .login in your home directory:

setenv EDITOR jed