Programming Digital Media

Text editors

Word processing programs, like "TextEdit" in OS X (or "WordPad" in Windows), can show the text as it will be formatted when it is printed, arranged in a particular way on the page, using different fonts. This is often called "WYSIWYG" (pronounced "whizzy-wig"), which stands for "what you see is what you get." The documents produced by these programs including formatting instructions and other information besides what you actually typed.

However, when you are writing software, your source code usually needs to be in "plain text" format, without the various formatting commands inserted by programs like TextEdit. You need to make sure that you are saving your file in "plain text" format, which typically requires you to use the "Save as..." menu item.

Text editing programs designed for writing software, on the other hand, write out their files in plain text format by default. You can adjust TextEdit using the "Preferences" menu.

The first menu contains the "New Document" settings.



Change the "Format" to "Plain Text". You also probably do not want the spelling checker for your Python programs, so deselect it in the "Options" section at the bottom.

There are other useful options in the "Open and Save" menu.



You may not want to delete the automatic backup file. The extension to our Python programs will be "py" and not "txt", so you should deselect that item in the first section.

Beyond TextEdit

Many of the editing programs have been around a long time, and generations (in software time) of programmers have had arguments about their relative superiority.

Two text editing programs that are very popular with programmers are vi and Emacs. Both of these programs have been around for a long time; vi's chief claim to fame is that it is installed by default in almost every variant of Unix. Emacs has also been ported to lots of systems, so if you aren't a systems administrator, the argument for vi based on availability isn't as strong. Emacs also supports a variety of editing modes for different programming languages, so it can be a very helpful tool in programming.

The EmacsWiki contains a good description of Emacs commands for the beginner. There is also a very good Emacs tutorial that you can run in Emacs itself. First, enter "emacs" on the command line in the Terminal application. The window contents will be replaced with some introductory information about Emacs displayed in an Emacs buffer. Then follow these instructions to start the tutorial.

An Emacs trick

Here's an idiosyncratic way of using Emacs that you may find interesting. When you are in a shell in the Terminal, typing ctrl-z (holding down the "control key" (the key labeled "ctrl") and entering the "z" character) will suspend the current program that's running. This means that if you are editing in Emacs and you enter ctrl-z, you will "return" to the shell. You can then enter shell commands in the normal way, including running a Python script that you are currently editing in Emacs. The Emacs program (or more accurately, the "Emacs process") can be restarted again by entering the shell command fg (for "foreground"). The suspended program is then said to be "running in the foreground" again. Once you become used to entering ctrl-z and fg, you can easily move between editing a script and running it to see how its working, and then returning to Emacs to edit some more.

Emacs can also run a shell in one of its buffers. The edit/test cycle then becomes a matter of switching between your editing buffer and the buffer that is running a shell. You switch between buffers with the ctrl-x b command. If you are going back and forth between two buffers, the other buffer is the default for selection by ctrl-x b so you can just enter return. In some ways, this can be more useful than the ctrl-z method. You can, for example, use Emacs commands to cut and paste the output of a shell command and use that output in another buffer that you are editing.