Text Editors for Programmeres - Programming Tools

 

What is a text editor?

A text editor is used to edit plain text files.  Text editors differ from word processors, such as Microsoft Word or WordPerfect, in that they do not add additional formatting information to documents.  You might write a paper in Word, because it contains tools to change fonts, margins, and layout, but Word by default puts that formatting and layout information directly into the file, which will confuse the compiler.  If you open a .doc file in a text editor, you will notice that most of the file is formatting codes.  Text editors, however, do not add formatting codes, which makes it easier to compile your code.

 

Why should I use a text editor?

Text editors have a feature set different from that of a traditional word processing program.  For example, most won't let you include pictures, or include tables, or double-space your writing.  The features of text editors vary from implementation to implementation, but there are several kinds of features that most editors have.  Below are listed some of the most common and useful features.

 

Syntax highlighting

 

Syntax highlighting is a very useful feature.  It means that the editor will highlight certain words or types or syntax specific to a language.  For example, if you have C++ highlighting turned on, the editor might make all C++ control flow keywords appear green.  This makes it much easier to follow the flow of your program.  As another example, the editor might have all quoted text show up as light blue.  This way, if you forget to include an opening or closing quotation mark, you will quickly realize it because of the color of the text on your screen.  A text editor might also indicate mismatched parentheses or brackets by turning them red; if you have a closing brace with no corresponding opening one, the color will tell you that you made a syntax error somewhere.

//Here is an example of what text might look like in your editor.
//This text is colored because it is a comment.
if (x > 5)
{
    //The closing parenthesis is red because it is unmatched.

x = 5 - ((3 + y) * (8 + (z / 24)))); }

Versatility

How does the editor know which words to highlight?  Good question.  The editor knows what language you are programming in.  It does this by either having you tell it, or, like Vim, detecting the suffix of the file.  If you are working on a file named code.cc, it will see the .cc and know to use C++ rules, but if you are working on one called code.html, it will apply HTML rules instead.  Some editors know hundreds of languages, ranging from the commonplace (C, Java, Perl) to the truly obscure (TADS, ABAQUS).  This means that you can use the same editor to program in practically any language you can think of and still enjoy the same feature and command set that you've become accustomed to.

 

Automatic indenting

 

automatic indenting is probably the most useful feature of a text editor. would you rather deal with code that looks like this (taken from a fifteen-puzzle):

int get_col (int tile_id)
{
    /*Cycle through...*/
    int i = 0, j = 0;

    while (i < Dim)
    {
        if (board[i][j] == tile_id) 
        {
            return i;
        }
        /*If you've hit the end of the row, move to the beginning of the
         * next.*/
        if (i == Dim-1)
        {
            j++;
            i = 0;
            /*Otherwise move to the next space in the row.*/
        } 
        else 
        {
            i++;
        }
    }
    /*This is only to get rid of the warning.*/
    return i;
}

or code that looks like this?:

 

int get_col ( int tile_id)

{

/*Cycle through...*/

int i = 0, j = 0;

while (i < Dim) {

if (board[i][j] == tile_id) {

return i;

}

/*If you've hit the end of the row, move to the beginning of the next*/

if (i == Dim-1) {

j++;

i = 0;

/*Otherwise move to the next space in the row.*/

} else {

i++;

/*This is only to get rid of the warning.*/

return i;

 

I thought so.  A text editor will spare you the trouble of having to put in all the tabs yourself by adding them automatically.  This has the benefit of letting you follow the control flow through indentation, so that you can make sure you are in the right block of code as you write.

 

Quick navigation features

 

If your program is anything above trivial, you'll want to be able to move through it quickly to find certain functions, instances of certain variables, or particular lines.  Text editors typically have more sophisticated movement capability than word processors.  For example, say you're compiling a program and find that you have a syntax error on line 312.  In Vim, all you have to do is type 312G, and the cursor will move to line 312.  (How does Vim know you don't want to type the characters 312G into the document?  More on that in the links at the end of the article.) 

 

 

Which text editor should I use?  What's the difference between them?  How do I get one?  How much do they cost?

 

There are many, many different editors available, with Vim and Emacs being two of the most popular, portable, and powerful. Another popular editor is Notepad++, a vastly improved version of Notepad.  Most editors (Vim and Emacs included) are free, but some are shareware.  I use Vim, but each editor has its adherents.  For a good listing of some of the best editors available for your platform, check out this FAQ on text editors. (It's aimed at STATA users, but all the editors listed are just fine for writing C++ code.)