Introduction

In this tutorial, I am going to talk about setting up emacs so that it will be easier for new users to get to their programming or editing tasks without much barrier.

Basics

If you don’t know emacs’s basic keys (at least the movement and editing keys), you need to keep a note of some of the most frequently used ones. You can copy the commands using some websites containing the basics (e.g http://mally.stanford.edu/~sr/computing/emacs.html) or use the list from “Movement” section below.

Installation

Create a directory called .emacs.d in your home directory (if not present already) and copy autopair.el file. If you use rxvt terminal, make sure also to download rxvt.el into your ~/.emacs.d directory. You can download these two files using following links.

Configuration

Let me walk you through some of the interesting stuffs, I have added to my ~/.emacs file.

At the top, plugins are loaded. Then some compilation magic is added.

To compile a c source file you can press “C-x C-m C-m“. i.e Hold Ctrl key and press x, m and m sequentially. It will compile the file (or execute Makefile if found one). If there are no errors, the message window will close. You can also press “C-x C-a C-a” to recompile (however you only need one. you can stick with C-x C-m C-m).

There are some unused commands in my configuration file. E.g I have yet to decide a shortcut key for open-line-above and open-line-below. However for time being, I am pretty much satisfied with “C-o” and “C-j“.

From around line 162, I have defined some handy shortcuts. Edit this section to match your favorite keys, so that they feel natural to the way you work.

Note: Here C=Ctrl key, A = Alt key

C-c C-j = Copy current line (just like y-y in vim)
F6 or C-x C-b = Open a list of currently opened file. I have remapped C-x C-b to open ibuffer instead of buffer. I prefer C-x C-b over F6 for this.
TAB = Don't get frustrated, if Tab don't seem to work immediately. In emacs rely on space, rather than Tab.
A-1 = Make this window take all the space
A-2 = Split vertically
A-3 = Split horizontally
A-0 = Delete this window
A-o / A-4 = Move to other window

Note: Windows and buffers are different things. With windows you can simulate tilling window behavior, and buffers are open files. Deleting a window does not delete a buffer.

Use emacs

A. Parenthesis

With our autopair plugins, parenthesis, quotes, brackets will be inserted. To move past the closing quotes, parenthesis you can keep on typing those characters. I.e If you type “ at the closing parenthesis, cursor will move after the quotes.

B. Auto-complete

When you start typing function names and variables previously typed in your source code, emacs will suggest the name. You can press TAB to complete it. If there are other options emacs will open a popup to let you choose.

C. Recording

Recording and playing a macro is a common task in emacs (just like in vim). Once you have figured out that the next serious of actions will be repeated multiple times, you can press “Ctrl + x + )” (you have to press shift+9 for “)” ) and let emacs record your actions. Once you have completed the series of tasks you wanted to repeat multiple times press “Ctrl + x + )“. After the macro have been recorded you can play (execute) it by pressing “Ctrl + x + e“. For subsequent repetition just press “e“.

D. Undo-redo

If you have to undo a series of actions, press C-/. If you wait for few seconds, C-/ will start to redo the commands. You can always reverse the action with C-r or use C-_ (undo command).