Learning Emacs


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.


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.


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.

1. Autopair (https://raw.github.com/capitaomorte/autopair/master/autopair.el)
2. Rxvt (http://www.emacswiki.org/emacs/download/rxvt.el)

Next, you need to install autocomplete. Follow these commands to install it.

$ cd /tmp
$ wget http://cx4a.org/pub/auto-complete/auto-complete-1.3.1.tar.bz2
$ tar xjf auto-complete-1.3.1.tar.bz2
$ cd auto-complete-1.3.1
$ make
$ make install
Install to: ~/.emacs.d

Finally, you need emacs configuration file (i.e  ~/.emacs). You can download mine (https://raw.githubusercontent.com/xpressrazor/themes/master/settings/.emacs) and make necessary changes to it. I am trying out different things, therefore it is not very clean, but will get the job done.

To open emacs in terminal use “emacs -nw



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.

A-6 = Switch to previous file
A-7 = Switch to next file
C-u / A - 8 = Save
C-q = Save and quit
C-x-C-f / A-5 = Open file
C-x-C-w = Save as
A-9 = Select All
A-k = Close this file (not emacs).

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).

E. Movement

These are my most used keys.

C-f / C-b = Move one character forward/backward.
A-f / A-b = Move one word forward/backward
C-n / C-p = Move one line forward/backward
A-a / A-e = Move one line forward/backward in beginning or end of line
C-a / C-e = Go to beginning/end of a line
C-Space = Start selecting text (you can cancel selection with C-g). Use above keys to move
C-w / A-w = Cut / Copy
C-y = Paste
C-o / C-j = Move current line down but stay here / go with the line
C-c-C-j = Copy current line (this is from our mapping)
A-d / A-delete = Delete word forward /backward
C-d / Delete = Delete character forward / backward


I hope you found this tutorial useful.


Leave a comment

Filed under Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s