Programmers newly making their way into the Linux world especially those who acquired their previous experience by working on Windows,
seem to have a hard time figuring out how to start building applications for Linux. One question heard often here and there is
"What is the best IDE for Linux?". A rather more specific question
would be "Is there anything like MS Visual Studio for Linux?".
Well,there are actually a bunch of IDEs for Linux. Best known are KDevelop and Anjuta. However these IDEs tend to be rather intimidating. They are a work in progress in so many aspects and getting one of them to work the way you want might be daunting. On the other hand whoever
comes around to the Linux world would soon hear about emacs. One thing
always heard in the forums and on the message boards is that many
veteran Linux programmers use emacs for most if not all their development tasks. Most of these people seem so reluctant to use any
other alternative. Well there must be something about emacs that makes
it so popular through the many years it has been there.
This short tutorial doesn't promise to till you all you want, or even
all you need to know to start working with emacs. It however promises
to put you on the right path to becoming familiar with the wealth of
options that emacs can offer. It will try to take you along the
easiest road possible.
I will assume that you have one of these friendly Linux distributions available nowadays. A distro that comes with an X server packaged and installs it from day one and allows you to use a graphical user interface to login and to use your Linux system through it. All this is not needed for emacs to work: it will actually accept a black console rather happily and will still offer all its services. Still, for a beginner, it is rather easier to learn a new application under the friendliest conditions possible.
I yet need to hear of a well known Linux distribution that comes without emacs. Most of them will also install it by default. Most probably your desktop manager (KDE, or Gnome) will offer a menu for accessing applications. On my Mandriva 2006, emacs can be reached from the applications menu -> more applications -> editors -> emacs. In
your case it might be somewhere else. Look for it under the menu that
makes sense. If not able to locate it for some reason open a
console/terminal window and just type "emacs &" (No quotes), then
enter. There should pop-up a window with "emacs@localhost" or
something similar in its title bar. If nothing happens emacs is not
Note:There might be a weird reason why emacs can be installed and yet not reachable by simply invoking it on the command line as its binary is almost always installed to the /usr/bin directory which must be in your PATH environment variable.
To install emacs use any package installation facility that your
distribution offers to locate and install its packages. Choosing the
main package will by default select all needed packages for
installation so you don't have to look much around.
Starting to work with emacs
When you first start emacs you will be introduced to a rather simple
interface. The usual title bar, then a menu bar, an editing area, then
at the bottom two strips one usually in a different color than the
editing area and has some text while the other is in the same color as
the editing area and usually is empty initially. The editing area will
start filled with some welcome message and a few advices, after a
moment these will be replaced with three lines of text each starting
with two semi-colons, they tell you what Buffer you're looking at now.
Well, now what to do? Just ignore whatever seems mysterious for the
time being. Go to the "Tools" menu and then select "Display
Speedbar". Another window shows up, a rather narrow one. It shows you
the contents of your home directory. (if you started emacs from the
command line while you were in some other place in the file system it
will probably show you the contents of this particular directory).
In the first line of the speedbar there is the name of the current
directory. Usually "/home/yourname/". Underneath it come any
directories in you home directory. Each begins with a little image of
a folder with a plus sign in its center followed by the name of the
Try clicking on a directory's name using the middle button of your
mouse. (If your mouse has only two buttons press both of them at the same time - for mice with wheels press the wheel and don't roll it).
This should take you to the selected directory, so the speedbar is
displaying the contents of this directory. Move your mouse to the
first line.Notice that path components are highlighted individually. Middle click the tilde sign '~' (this is the short cut for your home directory), and notice that speedbar takes you back there. Go once again to any of the sub-directories names but this time middle-click the image with the plus sign. A sub-tree shows up with the contents of the selected directory indented. The plus sign also turns into a minus sign. Well this is a normal tree structure which most of us are used to.
Now, what if you middle-click a file? Go ahead select a directory and
middle-click some plain text file. The file should be opened in the
main emacs window. Well easy, isn't it?
We will see more of the speedbar later. For now keep it around and use
it to navigate within your file system.
Writing your first program with emacs
If there are still some files open from your speedbar tests, go to the file menu and select “Close (current buffer)”. This will close your file. (We will talk about what buffers are a little later). The emacs window should be showing the first three lines we mentioned before.
Go to the file menu and select “Open File”. We are actually going to create a new file, but for emacs it is always “Open File” that is used. Look now at the last strip down the window, it should say “Find File: ~/” an there should be a plinking caret waiting for you to write something. Well, you should type a full path to a file. By default and if no file is currently opened in emacs' window you will be provided with your home directory as a starting point. For now we will create our new file right there, so just type “hello.cpp” (No quotes- as a rule drop the quotes I provide in this text unless I ask you explicitly to type them). Follow that with hitting the enter key. The emacs window turns blank and the plinking caret waits now at the upper-left corner of it. The little strip at the bottom should now say “loading cc-mode ... done”. This is apparently some message. Just ignore it for now.
There is nothing unusual about typing in emacs, being such an ancient beast as it is keyboards with characters were around when it was first created :).
Well, wait a minute! Go to the “Options” menu and select “Syntacs Hilighting (Global Font Lock Mode)”. Your text now should show up with different colors. Wow, simple editors don't usually do that, but emacs does. Go ahead and type the following lines.
int main( int argc, char* argv)
std::cout << “Hello World!” << std::endl;
This is of course the old friend of all of us the usual Hello World program in C++.
Go to the “File” menu and select “Save (current buffer)” . Go now to the speedbar. Our file's name should be hilighted in red. Middle-click the plus sign to its left. There should appear a new line starting with a little icon (a label) next to it “main”. This is our function's name. It is the only function we have currently in our file. So the speedbar is not all about navigating the file system after all! Sure this feature will come quite handy for large files with many functions. It show us what functions are in the file. But is that all? Why don't you middle-click the function's name in the speedbar. Yeh the first line of the function in the main emacs window is hilighted and the cursor (though hollow as the focus is still in the speedbar window) moved to the first character of the function's name. Well this is even more like it. Not only we can know what functions are there but we can just jump to any of them by middle-clicking its name in the speedbar. Does it start to seem more like an IDE for you? Hang around there is more.
Compiling our Hello World
If the speedbar window still has the focus, click into the main emacs window to focus on it. Now go to the “Tools” menu and select “Compile ...”. look at the strip at the bottom it should say “make -k”. This is the default, but we are going to change that for now.
The caret is plinking right after the “k” so delete the “-k” and type “hello” so the line reads “make hello”. Yes I know that we didn't write any makefile for our little program. Just bear with me and hit the enter key. The window should be split in two halves horizontally with a new area underneath our file.
The window should have some text similar to the following.
g++ hello.cpp -o hello
Compilation finished at Wed May 17 07:18:26
It seems that “make” somehow has figured out how to build an executable out of our single source file. It used g++ (this is gcc front-end for C++ language) in the expected way.
Having some healthy doubts? Well, doubts or no doubts, go to the “Tools” menu again and this time select “Shell Command ...”. In the bottom strip there is “Shell Command: “ with the now usual plinking caret. Type “./hello”. There should comes the expected greeting right on the same bottom strip “Hello World!”.
Messing up the code
Our little program is so little that nobody is supposed to make any mistakes while writing it. For larger source files mistakes and typos are almost inevitable. Anyway let's introduce some mistakes for the fun of messing up our code. Go to this single statement in our function's body and just remove the terminating semi-colon. Now click in the lower window where the compilation results are. Notice that the menu bar has a new menu named “Compile”. Go ahead select it and then select “Recompile”.
A dialog box should pop-up telling you that “hello.cpp” has indeed been modified and asks if emacs should save the changes to the file. So emacs knows when a file is needed for a compilation and knows when it is modified and needs saving. Smart fellow isn't he? You will find many buttons in the dialog. There is yes, no, and there is also “save all buffers”. You should have figured out by now that emacs calls files opened in it “buffers”. Judging from the plural in the dialog button it seems that emacs can actually open several files at once and it can actually save all of them with one click of a mouse button when it is needed. Yes sir, all this is right. But for now let's just press this "yes" button.
The lower window get filled with new text (no more questions are asked this time as emacs knows that it is the last command to be used again). Now the text should be something like the following:
g++ hello.cpp -o hello
hello.cpp: In function 'int main(int, char**)':
hello.cpp:6: error: expected `;' before '}' token
make: *** [hello] Error 1
Compilation exited abnormally with code 2 at Wed May 17 07:43:59
It seems that g++ didn't like our code this time. Normal enough!
Notice that the name of our file in the third and fourth lines is hilighted in different colors. In the fourth line it is even displayed with bold font and light colors. This line specifies the line number of the source file where g++ found the error. There no magic in g++ outputting the errors with line numbers but it is sure a good thing that emacs hilights the important lines of g++ output for you. It seems that emacs actually understands g++ output rather than just display it as is. Go on middle-click this bold name of the file in the fourth line. It seems that it actually wants you to do so.
The focus goes to the upper window and the caret is situated right on the line mentioned in the g++ output. (it is not the line missing the semi-colon but the one after it for it is here where g++ notices that something is wrong when it meets the brace while it was expecting a semi-colon). Anyway what matters for us is the fact that emacs doesn't only highlight errors for your convenience, it also jumps to where they appear in the text so that helping you in locating and fixing them.
Retype our missing semi-colon. And recompile again. (I think you know how to do it now).
What have you done up to now? You navigated the file system, wrote a little source file, navigated its structure and jumped to any function in it, compiled it into a program, and you executed your program to see how it works. You also were notified with errors in your code and were conveniently taken to where they were to fix them. You didn't need to leave emacs for a moment to accomplish any of these tasks. You used emacs commands, conveniently reachable through menus. This is basically what you expect of an IDE isn't it? But still there is much, much more of emacs. In fact this old fellow has a number of features that a life-time is simply not enough to learn them or even to know about them all.
In next tutorial I hope to be able to show more of emacs' features and how to know more about emacs on your own without leaving emacs.