Coding+with+emacs

I'm a big fan of the emacs text editor. emacs can seem like an awful lot to learn, and to be honest, I use less than 1% of its capabilities. Still, it works well for me and makes some tasks easier.

Terminology:
C-x means hold the control key and press x. M-s means hold the meta key, which is usually alt (esc usually also works), and press s. M-x command-name means hold meta, hit x. Release meta, and type "command name" the new buffer that will appear. You can hit tab to complete partial command names.

Editing C code:
emacs understands C pretty well. Open or create a new C file (from the command line, "emacs newfile.c"), and it will be properly highlighted.

Saving and exiting:
For new emacs users, these are the two biggest hurdles.
 * To save the current file (called a "buffer" in emacs lingo), press C-x C-s. That means hold control, press x. Release, hold control again and press s.
 * To exit, use C-x C-c. Same pattern.

Open a new file:
If the file doesn't exist, emacs will create it. Use C-x C-f. Emacs will prompt for a file name in a tiny buffer at the bottom of the screen. You can use tab completion here.

Switch between files:
In emacs lingo, this is called switching buffers. If you open a new file, you still have the other open. Use C-x b (hold control, press x; release, then press b) to switch to another buffer. By default, it will be the last buffer you were in, but you can type the name of another buffer. Again, tab completion is very nice here.

Close a second window:
Sometimes running a command will open a second window that you don't want, dividing your screen and getting in the way. If your cursor is in the window you want to keep, use "C-x 1" to close the other window. You can use "C-x 0" to close the other window instead.

Compile:
This is one of the primary reasons I use emacs, so that I don't have to return to the command line to compile my code. Use "M-x compile" (hit M-x, then type compile). It will ask how you want to make your code, and default to the command "make -k". If you have a makefile, go ahead and use it. Otherwise, enter whatever command you want, e.g., "gcc -o prog -Wall -ansi main.c otherfile.c". If you haven't saved, it will ask if you want to (hit "y"), and it will run the command.

Now you have two buffers open. If you have errors, this is kind of nice, because you can see the command output. However, this is kind of annoying if the compile completed normally. If you want the window to automatically close when your compilation succeeds, add this: code (defun compilation-exit-autoclose (status code msg) ;; If M-x compile exists with a 0  (when (and (eq status 'exit) (zerop code)) ;; then bury the *compilation* buffer, so that C-x b doesn't go there (bury-buffer) ;; and delete the *compilation* window (delete-window (get-buffer-window (get-buffer "*compilation*")))) ;; Always return the anticipated result of compilation-exit-message-function  (cons msg code)) (setq compilation-exit-message-function 'compilation-exit-autoclose) code to your .emacs file.
 * Helper for compilation. Close the compilation window if there were no errors.
 * Specify my function (maybe I should have done a lambda function)

Next, you want to be able to recompile quickly. I have a single-button hotkey, since it I do it so often. Also add this to your .emacs file. code (global-set-key [f12] 'recompile) code Now you can hit F12 to recompile your code.

Run code (or other shell commands):
There wouldn't be much point to compiling in emacs if we couldn't also run code from there. Use "M-!" (hold alt, shift, and press 1), and emacs will ask you what shell command you want to run. If you compiled using the above gcc command, you would say "./prog", or "./prog input1 input2" for instance. This is keeps command line history, so you can use the up arrow to get previous command.

Combining both of these, I usually write some code, hit F12 to recompile (and save at the same time), and then use M-! up arrow enter to run it again. 7 key presses (5 if you count M-! as a single key press), and it saves, compiles, and runs my code. This could all be turned into a single key press if you felt like hacking a little more on the .emacs file.

Run gdb:
There are a lot of ways in which you can use gdb inside emacs to speed up debugging. My use is pretty basic:

code M-x gdb code

Asks how you want to run gdb; it will have a default that uses a binary in the local directory, so usually you can just hit enter.

This will open a new interactive gdb shell inside emacs. For instance,

code (gdb) b main (gdb) r (gdb) p argc code

Should set a break point at 'main', run your code, and then print the value of argc once you hit your break point.

For more advanced usage, [] is a pretty good reference.

Use git:
Also for later. [] is one source.