Basic emacs setup

Emacs is a great editor. But it is really hard to set it up. So I decided to write a document for my current emacs setup. In this post, I will explain how to install emacs with a basic setup.

I will cover how to:

  • Install emacs
  • Enable IDO mode
  • Add autocomplete mode
  • Add yasnippet mode
  • Set window title to full file name
  • Install color themes

I will cover setups for specific programming languages in other posts

Install emacs

There is two way to install emacs. Easy way is to use your package manager. If you are using debian derived system, just type

sudo apt-get install emacs23

I recommend to compile it yourself from source. It is really easy to do. Here is instructions to do it.

Go to emacs page and download latest version. By the time I wrote this it was emacs 23.3. Google for emacs and find project page. In there, you can find a link to download emacs. In my case I found the file emacs-23.3.tar.gz and untared it. The way I installed it, was the following.

First, I installed the dependencies for emacs. Easy way to do this is to use apt-get build-dep command:

$sudo apt-get build-dep emacs23

Please note that this command will not install emacs23 instead it will only install dependencies to build it.

After that, you can Install emacs. Installation instructions was in emacs directory that I just untarred. (In the file named INSTALL). Here is the commands I followed:

(run in emacs directory you just untared)
$./configure
$make
$sudo make install


UPDATE: When I try to install emacs on ubuntu 11.10 I faced the following error while configuring.

 configure: error: crt*.o not found.  Use --with-crt-dir to specify the location.

So as error suggested I search for a crt*o file with command

 sudo find / -name "crt*.o"

That way I found my crt directory in my case it was

 /usr/lib/x86_64-linux-gnu

So I run configure command as

 ./configure --with-crt-dir=/usr/lib/x86_64-linux-gnu

After that every thing work as expected.


Since we finish installing emacs, we can start the hard part: setting it up. Create .emacs file and .emacs.d directory in ~/ . Also create plugins directory under .emacs.d

Now you should have:

~/.emacs (File)
~/.emacs.d/ (Directory)
~/.emacs.d/plugins/ (Directory)

After that, add .emacs.d and plugins directory to emacs load path:

To do this add following elisp script to your .emacs file (initialization file which will be run in initialization of emacs)

(add-to-list 'load-path “~/.emacs.d/”)
(add-to-list 'load-path “~/.emacs.d/plugins/”)

Now you have .emacs.d and plugins directories on your load path. That means, any script you put in those directories will be executed in initialization of your editor.

Enable ido mode

IDO is a great emacs plugin that makes finding files and buffers very easy.

To enable it, Add the following script to your .emacs file

(require 'ido)
(ido-mode t)
(setq ido-enable-flex-matching t) ;; enable fuzzy matching

You didn't need to install it because it is already included in emacs 2.3.

Add autocomplete mode

For autocomplete mode, I went to this page and downloaded autocomplete 1.3.1. Page also has a link to Autocomplete mode user manual. You can get installation information from there.

I installed it in following way:

  1. Download and untar autocomplete file
  2. create an installation directory (~/.emacs.d/plugins/autocomplete)
  3. open emacs and type M-x load-file RET downloaded_autocomplete_dir/etc/install.el RET
  4. Installation script will ask you an installation path. give it your newly created directory (~/.emacs.d/plugins/autocomplete)
  5. After this step, script will print an elisp script to add in .emacs file. Add the script to your .emacs file.

The script I got was the following:

(add-to-list 'load-path "~/.emacs.d/plugins/autocomplete/")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/plugins/autocomplete/ac-dict")
(ac-config-default)

Install yasnippet

  1. First go to yasnippet project page and install ya snipet-bundle.el and put it under ~/emacs.d/plugins directory (or anywhere on your load path)
  2. Add following script to your .emacs file

    (require 'yasnippet-bundle)

More info about installing yasnippet can be found here

Set window title to full file name

File name is actually written in notification area but you cannot see where the file is located. So I prefer to add it to window title. To do this add the following script to your .emacs file. Try to change the format to fit your taste. After all that is all emacs is all about.

(setq frame-title-format '("Emacs @ " system-name ": %b %+%+ %f"))

Install color themes

If you want to change the colors in emacs, you can use preset themes for that. To install color themes go to color themes home page and download color-theme download it. Uncompress downloaded file (In my case, I downloaded color-theme.el.gz) and copy color-theme.el to somwhere in your load path(~/emacs.d/plugins/color-theme.el). After that, open el file and read the instructions which explains that in order to enable color themes you should copy following code to your .emacs file:

(require 'color-theme)
(color-theme-gnome2)


UPDATE: If you got an initialization error on color theme try this instead

(require 'color-theme)
(color-theme-initialize)
(color-theme-gnome2)

you can change color-theme-gnome2 method with another theme to change the default theme. available themes can be seen from Tools->Color Themes menu item. You can also color theme test project to choose a theme.

If you are also using emacs in terminal, you might want to use different theme for terminal. In order to achive that use following code instead of the one above:

(require 'color-theme)
(if window-system
    (color-theme-subtle-hacker)
    (color-theme-hober))

In this code first theme is used if you open emacs in regular way. Second one is used if you open it in terminal (emacs -nw) (source: emacs-wiki)

You might also want to cover emacs python setup and emacs web setup post with this one

blog comments powered by Disqus
- Follow yilmaz_huseyin on Twitter