Emacs: the Free Software IDE
Being in a hurry, we neglected our semicolon and pressed Return instead of Ctrl-J. Try a compile. Do M-X and the up arrow keys until you have “compile” in the minibuffer, then press Return. The command line is good, so press Return again.
Oops! We have an error. To locate the error in the source, we press Ctrl-X `. This puts the first error message at the top of the compile output buffer and the cursor at the offending line. The error is before the brace, so we look at the end of the previous line. How terrible; we've committed the classic newbie programmer's error and left out the semicolon. So we press the left arrow key and add the semicolon. Then we save our work with Ctrl-X Ctrl-S.
Emacs' compilation mode is set up to detect error messages from a great many compilers (Microsoft Visual C++, for example) and not only C compilers. Emacs also recognizes the output from weblint, an HTML checker.
Notice that the electric semicolon did two things: it indented our line for us, which was nice of it, but it also inserted a line between the “return” statement and the closing brace, positioning the cursor ready to add another line. Well, that isn't what we wanted. So we undo it with M-Shift--. Now we override the electric semicolon and insert a semicolon with Ctrl-Q ;.
It would be nice to have the program all properly indented, so we use Emacs' indentation tools. First we select a region to indent. To select the entire program, Ctrl-X H. Then we indent with Ctrl-M-\ (that's Control meta-backslash). To look at the results, we hide the compilation buffer with Ctrl-X 1 and admire our handiwork (Figure 6). That done, we compile once more (M-X compile Return Return). This time we are successful.

Figure 6. Pretty Printing with Emacs
Of course, any programmer knows to comment their code copiously. First, we need a general comment to tell what the program does. So we enter that in the source file right after the timestamp (Figure 7). Start with Emacs' command to start a comment, M-;. Notice as you type that the text simply wraps around (as indicated by the arrows in the left and right margins). That's ugly, so we press M-Q to “fill” the paragraph. Notice that the C++ comment delimiters are inserted at the beginning of each line (Figure 8).

Figure 7. Entering a Comment (or Other Text) into Emacs

Figure 8. The Same Comment, Now “Filled”
We can arrange to fill text automatically with M-X auto-fill-mode, but this will fill our C source as well, which is not what we want. If you like, you can turn auto-fill mode on for comments and off again for source.
You can also comment a line of code. Put the cursor anywhere on the line, press M-; and continue. Emacs will put in the comment delimiter at the end of the line and move the cursor to the appropriate place to enter the comment. That will be the end of the line for C++-style comments, but between the delimiters for C-style comments, /* ... */.
And, don't forget to spell check your comments with M-X ispell-comments-and-strings.
Emacs has a front end for CVS, RCS or SCCS, called VC. The first two are free-software version control systems and come with many Linux distributions. VC is smart enough to figure out which one you are using. Most VC functions are handled with the key sequence Ctrl-X Ctrl-Q or Ctrl-X V V. Depending on the present state of the file, VC will check it in or out. If the file has never been added to the version control system, Emacs will determine that and check it in for you. When you check in a change, Emacs will make a temporary buffer for the change comment, so you have no excuse for ignoring that good programming habit.
This front end works with any document under version control. For example, the Linux Documentation Project uses remote CVS to control their linuxdoc and docbook source files. Even though I contribute over a 56KBps dial-up line, I can use Emacs to make the CVS process transparent.
Tags are a database of function, and optionally, typedef names are created with the program etags and stored in the file TAGS. They are stored together with their locations, making it easy to examine the definition of a function. To create a tags file for C programs, run etags -t *.[ch] from any shell. You can use tags for many programming languages; run etags --help from a shell for a list.
Our minimal program has only one file and only one function. Other developers should be so lucky; many C projects spread out across multiple files in multiple directories. Tags will cover complex situations like that as well. In fact, the more complex the project, the better tags shine.
To examine the source for a function, use M-. (that's meta-period). Start typing the name of the function and use tab completion. If you haven't already specified a tags file to use, Emacs will ask. Usually the default is exactly what you want (Figure 9).

Figure 9. Using the Tags Facility to Locate a Function
Notice that the default function name is the word next to the cursor in the source window. Tags mode lets you edit one file, place the cursor over the name of a function to examine and go edit that function with minimal keystrokes.
You can also display the source for the function in another window, with Ctrl-X 4 .. This lets you examine multiple functions simultaneously, as many functions as you can fit on the screen. If you want to see every reference to a function, use M-X tags-search. To continue the search, use M-,.
Also, tags are very useful for searching and replacing function names within projects. For example, if I have a function named Frodo and I want to rename it Gollum, I not only need to change the function declaration, but I have to get every prototype and every reference. So M-X tags-query-replace and away I go. Both of these search functions use regular expressions, making them very powerful. You can browse C++ classes with Ebrowse.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- UX Designer
- Technical Support Rep
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
10 hours 25 min ago - Nice article, thanks for the
21 hours 5 min ago - I once had a better way I
1 day 2 hours ago - Not only you I too assumed
1 day 3 hours ago - another very interesting
1 day 5 hours ago - Reply to comment | Linux Journal
1 day 6 hours ago - Reply to comment | Linux Journal
1 day 13 hours ago - Reply to comment | Linux Journal
1 day 14 hours ago - Favorite (and easily brute-forced) pw's
1 day 15 hours ago - Have you tried Boxen? It's a
1 day 21 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Emacs 22 reference card
An Emacs 22 reference card - PDF generated from the refcard.tex TeX source file that comes in the Emacs distribution.
A GUD (GDB) tip
You forgot to mention in regards to GUD (GDB) that C-x SPC will set a breakpoint on a given line.