Starting out
C tutorial
Source code
| 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 wordprocessors, such as Microsoft Word or WordPerfect, in that they do not addadditional formatting information to documents. You might write a paper in Word, because it contains tools tochange fonts, margins, and layout, but Word by default puts that formatting andlayout information directly into the file, which will confuse the compiler. If you open a .doc file in a texteditor, you will notice that most of the file is formatting codes. Text editors, however, do not addformatting codes, which makes it easier to compile your code.
Why should I use a texteditor? Text editors have a feature set different from that of atraditional word processing program. For example, most won't let you include pictures, or include tables, ordouble-space your writing. Thefeatures of text editors vary from implementation to implementation, but thereare several kinds of features that most editors have. Below are listed some of the most common and usefulfeatures.
Syntax highlighting
Syntax highlighting is a very useful feature. It means that the editor will highlightcertain 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 keywordsappear green. This makes it mucheasier to follow the flow of your program. As another example, the editor might have all quoted textshow up as light blue. This way,if you forget to include an opening or closing quotation mark, you will quicklyrealize it because of the color of the text on your screen. A text editor might also indicatemismatched parentheses or brackets by turning them red; if you have a closingbrace with no corresponding opening one, the color will tell you that you madea 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))));} VersatilityHow does the editor know which words to highlight? Good question. The editor knows what language you areprogramming in. It does this byeither having you tell it, or, like Vim, detecting the suffix of the file. If you are working on a file namedcode.cc, it will see the .cc and know to use C++ rules, but if you are workingon one called code.html, it will apply HTML rules instead. Some editors know hundreds oflanguages, ranging from the commonplace (C, Java, Perl) to the truly obscure(TADS, ABAQUS). This means thatyou can use the same editor to program in practically any language you canthink of and still enjoy the same feature and command set that you've becomeaccustomed to.
Automatic indenting
automatic indenting is probably the most useful feature of atext editor. would you rather dealwith 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 ofthe 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. Atext editor will spare you the trouble of having to put in all the tabsyourself by adding them automatically. This has the benefit of letting you follow the control flow throughindentation, so that you can make sure you are in the right block of code asyou write.
Quick navigation features
If your program is anything above trivial, you'll want to beable to move through it quickly to find certain functions, instances of certainvariables, or particular lines. Text editors typically have more sophisticated movement capability thanword processors. For example, sayyou're compiling a program and find that you have a syntax error on line312. In Vim, all you have to do istype 312G, and the cursor will move to line 312. (How does Vim know you don't want to type the characters312G into the document? More onthat in the links at the end of the article.)
Which text editor should I use? What's the difference betweenthem? How do I get one? How much do they cost?
There are many, many different editors available, withVim andEmacs being two of themost popular, portable, and powerful. Another popular editor isNotepad++, a vastlyimproved version of Notepad. Most editors (Vim and Emacs included) are free, but some areshareware. I use Vim, but each editor has its adherents. For a good listing of some of the best editors available foryour platform,check out thisFAQ on text editors. (It's aimed at STATA users, but all the editors listedare just fine for writing C++ code.) |