Before we can dive into the subject of Linux game coding, we’ll need a way to write our source code. Although simple editors such as pico and joe are sufficient for some simple tasks, they are inadequate for programming. It would be
preferable to use a text editor with support for syntax highlighting, brace matching, and other features. Let’s take a look at several of the more popular code editors. If you have never written code with a UNIX-like system, it would be a good idea to try out these editors to see which one best suits your
programming style. This chapter is not meant to be a reference manual or tutorial for these editors; rather, it is starting point for those who have never written code on a UNIX-like platform.
vi
vi (pronounced “vee-eye” or “vie”) is a rather old text editor with a strong following. It is difficult to master, but once you have learned its keystrokes and its quirks, it is hard to use anything else. vi works well on just about any Linux configuration; it requires almost no processor power and very little memory. It also has the nice advantage of being present on nearly every UNIX-like system you’ll encounter, including most Linux systems. vi is a standard component of every major Linux distribution.
Although vi is an old editor from the days when everyone worked over slow text terminals, it has been improved substantially by its users, and some modern versions (such as vim) are capable of syntax highlighting and other niceties. Several versions of this editor are also available for the X Window System, featuring pull-down menus and convenient shortcuts. However, these versions defeat one of the greatest features of vi: that it can be used over nearly any type of terminal. vi can be used efficiently over low-speed telnet connections, within local terminals, and even from Palm Pilots and other unusual devices. Its minimalistic interface requires very little functionality from the terminal.
Graphical versions of vi throw this feature away and so can hardly be considered substitutes for the original vi editor.
vi is actually a full-screen interface to the command-based ex editing engine. ex can also be used outside of the vi editor as a command-line tool, and it can be used to add editing capabilities to shell scripts. For instance, a shell script might invoke ex to edit a configuration file automatically. ex commands can be
specified within the vi editor, although a tutorial on the nuts and bolts of ex is beyond the scope of this chapter.
vi is a mode-based editor, and this is a major source of confusion. vi has two main modes: command mode and insertion mode. Command mode is strictly for issuing commands to vi. For instance, one might use this mode to move to a certain line in the document and delete a word. Command mode maynot be used for typing text into the document. Anything you type in command mode will be interpreted as a vi command (and indeed there are so many possible vi commands that nearly anything you type in command mode will do something). Insertion mode, on the other hand, is strictly for typing text into the document. Most commands are not recognized in this mode, and anything you type will be inserted into the document.
vi initially starts up into command mode. To enter insertion mode, pressi. To switch back into command mode, press Escape. This mode switching may seem like quite a hassle, but it becomes second nature after a bit of practice.
Emacs
GNU Emacs is uncontested as the behemoth of text editors (indeed, some think of it as an operating system in itself). It is based on its own variant of the Lisp programming language; almost all of the editor’s functionality is implemented in customizable Emacs Lisp. Emacs has a loyal following among programmers, partly because absolutely every aspect of this editor can be changed by the user. I started writing this book with NEdit, but I eventually switched over to Emacs because it works well on the Linux console and doesn’t require a mouse. (I’m also a bit of a Lisp enthusiast, and in that regard Emacs is a perfect match.) Emacs is not as difficult as vi to learn initially; there is simply a lot more to learn. Its basic commands and keystrokes are not hard to get used to, but becoming fluent with Emacs is a major undertaking. Emacs includes a mail and news client, editing modes for nearly every language you’d ever want to use, several types of documentation readers, and even optional IRC clients and web browsers. Many of these features define their own sets of command keys, leaving much for the would-be user to learn. In return for this hassle, Emacs provides an enormous amount of power; it’s quite literally possible to set your login shell to Emacs and never leave its environment.
To get started with Emacs, run the editor (the command is usually emacs), press
Ctrl-h, and thent. The Emacs tutorial will open, and you can use it to learn the basic keys and editing modes.
In addition to the “real” GNU Emacs, there are several other editors that are very similar in capabilities and usage. XEmacs1 is a code fork from GNU Emacs with a number of added features and an improved interface. JED2 is a
programmer’s editor that closely resembles Emacs but has fewer features and a smaller memory footprint.
1 http://www.xemacs.org 2
Emacs is an ideal editor for people who appreciate a large (perhaps
overwhelming) amount of functionality and don’t mind a bit of a learning curve. It is excellent for those who would like to use a scripting language to add custom abilities to their editor (entire applications have in fact been written in Emacs Lisp). Emacs is available as part of nearly every Linux distribution, and it can also be obtained directly from the GNU project’s FTP server3 or one of its mirrors.
NEdit
NEdit, the “Nirvana Editor,” is a very slick code editor from Fermilab.4 It is neither as absurdly customizable as Emacs nor as ubiquitous as vi, but it is much easier to learn (since its keystrokes are similar to those of many popular word processors) and powerful enough for serious work. NEdit’s main downside is that it requires the X Window System to run. It is a good idea to have at least a working knowledge of another editor if you choose to use NEdit for your daily work. This book was written partly with NEdit (though I later switched to Emacs). Although previous versions of NEdit were encumbered by a license that was not palatable to most Linux distributors, the NEdit license was changed to the GNU General Public License with the 5.1 release. The editor is now truly free software, and it is currently under active development by a team of volunteers.