Sunday, 2 May 2010

Intro to emacs as C\C++ source code editor



Hi guys.....
Well whatever I'm writing in this blog can be found in help,info,man pages or any book as its the starting details of emacs.
I actually presented this presentation as a talk in OSScamp Pantnagar,May 2010.
Its just for review or Introduction.
(Intro to Programming in Linux can be found at http://gnu-linux34915.blogspot.com/2010/05/starting-programming-in-gnulinux.html)
Well, An editor is the program that you use to edit source code. Lots of different editors are available for Linux, but the most popular and full-featured editor is probably GNU Emacs.
Emacs is the free, customizable, extensible, platform independent editor(means its available for different platforms).
Emacs is much more than an editor. It is an incredibly powerful program, so much so that at CodeSourcery, it is affectionately known as the One True Program, or just the OTP for short. You can read and send email from within Emacs, and you can customize and extend Emacs in ways far too numerous. You can even browse the Web from within Emacs!

Emacs is a very powerful tool written by richard M. stallman and has more than 1000 of useful commands. Some of those commands which I think are good for start using emacs and helped me to start programming C\C++ in emacs are:

General Commands
C-x-f : To open a file
C-x-s: To save a file
C-x-c: To close Emacs(All buffers)
C-x 2: To split buffer horizontally
C-x 3: To split buffer vertically
C-x 1: To close all the buffers except current
C-x 0: To close the current buffer
M-x : To enter a command
M-x compile: To compile
M-x gdb: GNU Debugger
Meta Key
Under Linux: the Esc key
Under Windows: the Alt key
Under MacOs it should be the Command key.

Here one thing should be noted 'C' is used for Ctrl key and c-x-f is different from c-x 0.
C-x-f means to press all the keys Ctrl, x and f simultaneously and C-x 0 means first press Ctrl an x together then leave both the keys , the command interpretor will wait for the next instruction or command input , press 0 and the command will work.

Note: I'm making blog as explainable and easy to understand as possible without technical words , if there is any contradiction or mistake please feel free to comment and correct me I will surely take it under consideration.

Now why is emacs good for programming when many other powerful editors like vi and vim or easy GUI editor like gedit or kwrite (or notepad++ in M$) are available.
Well here are some features of emacs that make it handy for C\C++ programming:

Advantages of emacs for programming:
Editing , Compiling , Debugging all in one editor.
Multiple buffers
Auto Indentation (Automatic Formatting)
Syntax Highlighting
Different Modes
Command + GUI both interface

To make it work out more simpler let me give brief info about emacs with the help of some screenshots.
Well emacs is not by default available in GNU-Linux distros.
You can install it by typing #apt-get install emacs22 or just use Synaptic package manager, for windows download the exe file.
I will be particularly talking about the use of emacs in linux(Ubuntu).
to start emacs its in Applications->Accessories or type emacs in the terminal (with file specified optional).
To open a file type Ctrl+x+f command.



Now go to the file which you want to edit , here I would i like to add a thing if you will open C or C++ file it will identify the mode and adds an extra C\C++ menu in emacs.













As in above pics you can see Different color coding are used for keywords, identifiers and all i.e., Syntax highlighting.
Now let us compile the C or C++ code.
We will be using gcc\g++ to compile the codes.
First press M-x (M is meta character , Esc in ubuntu Linux) , you will see that the command interpreter will wait for the next instruction , input compile and press enter.
It will show make -k i.e., the default command to compile in Linux. Clear it and type gcc -c(arguments optional) and press enter.
Your code will successfully compile inside emacs.
You can see how in the next screenshots.










You not only compile you can even run GNU debugger (gdb) inside emacs using M-x gdb command.
At last close emacs using C-x-c command.
There is a lot to learn. Keep learning keep blogging and do comment.
Here are some videos too (No audio).

video


video

I hope this Intro will help you guys start using emacs.

Aduait Pokhriyal
Student
B.Tech 3rd Year

2 comments:

  1. great work man!!!
    this is a nice intro to emacs editor.....keep up the good work!!! :P :D :)

    ReplyDelete
  2. If any one would like to learn more about emacs there is no need to search for a book initially......there is lot of tutorials available in the emacs help menu and at the welcome screen..... just try them first.......

    ReplyDelete