In CSC-151 you used an Integrated Development Environment (IDEs) called DrRacket to write and run (develop) your programs. Similar IDEs exist for C, but it is more traditional among C programmers to use separate programs for editing, compiling, and debugging C code. Because part of the focus of the course is to become familiar with Linux and the UNIX tradition, we will follow this approach.
Two of the most common text editors used by C programmers are emacs and ViM. This course will use emacs. Notice that this page uses emacs keystroke notations, which you can read about on the keystroke reference page.
emacs allows many editing operations to be performed via keystroke commands as well as via toolbar icons and menu selections.
emacs uses unconventional notation for naming keystrokes. For example the keystroke C-x C-f means: hold down the ctrl key and press x,then lift the x key and press f. The ctrl key should be down when you press f. It does not matter whether you do or do not lift it in between.
The keystroke M-w means: hold down Alt and press w.
More keystroke notations can be found on the keystroke reference page
.
To launch Emacs, call it by name and (optionally) add the name of a file that you want to open. Since you will probably find it convenient to launch Emacs as a background process, you should also append an ampersand to the end of the command. Either example below works.
$ emacs &
$ emacs filename &
If you open emacs without specifying a file to open, you will see an emacs splash screen, and if you click on it, a scratch buffer will appear. This can be disconcerting at first, but it is harmless. You should resist the temptation to delete the text in the scratch window and begin typing there. Rather, if your intent is to begin a new file, you should “open” the file as described below, using a new file name. This will present you with an empty buffer to type in.
To save a file, type C-x C-s.
To close a file, type C-x k or select Close (current buffer) from the file menu. You can also use C-x C-c to close all files and quit emacs.
By default, the emacs editor is configured so that you encounter a split screen each time you open the editor. If you find this feature annoying, you can change this default in either of two ways:
When you open the program, look at the bottom of the “Welcome to GNU Emacs” window. Click the box that says “Never show it [this startup screen] again”.
Or, within emacs click on the “Options” tab at the top of the window, select “Customize Emacs” and then “Specific Option”. The phrase “Customize variable:” will appear in the little windown at the bottom, type inhibit-startup-screen. A new window will appear which contains teh line `Inhibit Startup Screen: Toggle off (nil)”. Click the Toggle button, and then the apply and save button above.
To create a new file, simply launch emacs with the name of the file you’d like to create.
The following command creates a file called test.c (if it doesn’t already exist) in the current directory, and opens it in emacs.
$ emacs test.c &
The following command creates a file called test.c in the specified location (if it doesn’t already exist in that location) and opens it in emacs.
$ emacs /home/eikmeier/csc161/labs/test.c &
If emacs is already running, and you’d like to open a file (or create a new file) you can use keystrke C-x C-f. Then note the command buffer at the very bottom of the Emacs window. It should contain a prompt Find file: and a default directory (the directory from which you launched Emacs). Type the name of the file you want to open, and press return.
To cancel a partial command, type C-g.
When might you need to do this? Suppose you intend to open a file, so you press C-x C-f. Now the command buffer is waiting for input, but suppose you change you mind. To clear the command buffer, make sure your cursor is in the command buffer, and then type C-g.
If you open an exiting file and begin to edit it, the first time you save the file, emacs writes the new version with the file’s normal name, but it also retains the previous version and adds a tilde to the end of the name. For example, if you edit and save hello.c, you will have two files: hello.c and hello.c~.
As you continue to work, it does not matter how many times you save your changes, the backup version of the file will still be the version that existed when you opened it.
As you work, if you have unsaved changes, emacs will periodically create and update an autosave file. The name of the autosave file will be #filename#. Yes, indeed, the # characters are part of the file name.
When you save your changes, emacs will remove the autosave file. Thus, if you save your changes before exiting emacs, there will be no autosave file left behind. However, if you exit without saving your changes, or if emacs closes abnormally for some reason, the autosave file will be retained, and it can be used to recover your changes.
To recover your changes when a file was closed abnormally, open the file as you normally would (i.e., open the file you want to recover, not the autosave file). Then type M-x recover fileEnter. (Yes, you have to type both the keystrokes and the words.)
emacs will give a prompt in the command buffer, and you respond with the name of the file to be recovered (not the autosave file). Finally, emacs asks if you really mean it, and you answer yes. Hopefully, you will never need this feature, but it is nice to have when you need it.
emacs is smart about tabs. If you do not use correct indentation while programming (bad practice!) or make a mistake along the way, you can simply hit the tab anywhere in a line and emacs will automatically move the line to the correct indentation.
It may not be obvious just how useful this feature is. But when you have a large program, and you make a structural change like adding an if statement, it can be very nice to use a single keystroke per line to fix the indentation. Further, if you get in the habit of using this feature, you will find that emacs can help you identify errors in the placement of your braces. (It will indent based on your braces, and if the indentation is not what you expect, your braces are not correct!)
Having multiple files open within emacs is common and can be quite helpful. Each file is associated with an input buffer. You can see a list of your open buffers or switch between buffers by selecting the Buffers menu, at the top of the Emacs window. Try using the Buffers menu to switch between the two files you have open.
If you don’t feel like using the mouse, you can also switch buffers much more quickly by using C-x b to switch to the most recently used “other” buffer, or C-x C-b to type (or choose from a list) the name of the already-open buffer to switch to. Note that emacs will conveniently use tab completion here.
It is frequently nice when programming to have two source files open (and visible) at once. In emacs, this can be done by splitting your window. To try this, type C-x 2. Where you had one window before, you now have two.
At present both of the windows will be displaying the same file. This can actually be useful, since you can arrange to have the two windows display different portions of the file. If you choose to use it this way, you can edit text in either or both windows to update the file. Alternatively, you can open a second file in one of the windows.
The next thing you will want to know how to do is to move your cursor between the two windows. Certainly you can do this with the mouse. You can also do it with C-x o (where the last key is a lower-case letter o). What does that stand for? I don’t know, maybe “other window”?
When you want to revert to having only a single window open, type C-x 1 (i.e., “I want one window open now”).
It is good practice to refrain from increasing the width of your emacs window when writing programs. This is because, if you do so, you will start producing source code that has a non-standard width. Who cares? Anyone who wants to look at your code in an emacs window that has not been widened exactly as you widened yours, and anyone who wants to look at a printout of your source code. (If you generate source code that is very wide and then print it, or look at it in a standard size emacs window, your long lines will wrap around quite unpleasantly. The resulting code will look terrible. It really will. And your professors and future employers are likely to take serious offense at code that looks terrible.)
Increasing the vertical height of your emacs window is fine to do.