Installing and setting up emacs

Installing emacs

Emacs is installed by default on most linux machines and on Macs, but it may not be the most recent version. To get the most out of emacs, you should be running an up-to-date version, certainly at least version 24.4.

If you use a Mac, you should install the latest emacs from Emacs for OSX

To install your own version of emacs on linux, use your package manager, which should provide a new version. Failing that, you can build from source to get the bleeding-edge version using

git clone git://git.savannah.gnu.org/emacs.git
cd emacs
./autogen.sh
./configure --prefix=/my/install/path
make
make check
make install

If you use Windows, I’m afraid I don’t have any good recommendations. It is perfectly possible to get a nice emacs setup in Windows, but I don’t know how!

Setting up emacs

Emacs is fully useable and powerful out of the box, but is infinitely configurable and several comprehensive packaged configurations exist to get you off to a flying start. My recommendation is prelude which you install using the following steps in a terminal wondow:

  1. Back up any previous emacs configuration
    mv ~/.emacs ~/.emacs_backup
    mv ~/.emacs.d ~/.emacs.d_backup
    rm ~/.emacs.desktop
    
  2. Now we can use curl to install prelude:
    curl -L http://git.io/epre | sh
    
  3. This step is optional, and adjusts some of the modules installed by prelude to match my tastes – you can change this later. In a text editor (not emacs – we don’t want to start emacs yet) edit the file ~/.emacs.d/prelude-modules.el – I will use nano
    nano ~/.emacs.d/prelude-modules.el
    

    and comment out the line containing prelude-company and uncomment the lines containing prelude-helm and prelude-latex

  4. Now start emacs and wait a minute or two as it sets itself up for the first time. Once it has completed, quit emacs.
  5. Next we’ll create a personal emacs configuration file where you can add your own options as you become more familiar with emacs. use nano or any other text editor to create a file called ~/.emacs.d/personal/emacs_config.el and paste in the following and then save
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; basic emacs configuration to be used in conjunction with prelude       ;;
;; pragmaticemacs.com/installing-and-setting-up-emacs/                    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;Add MELPA repository for packages
(add-to-list 'package-archives
             '("melpa" . "http://melpa.org/packages/") t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; prelude options                                                        ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; install additional packages - add anyto this list that you want to
;; be installed automatically
(prelude-require-packages '(multiple-cursors ess))

;;enable arrow keys
(setq prelude-guru nil)

;;smooth scrolling
(setq prelude-use-smooth-scrolling t)

;;uncomment this to use default theme
;;(disable-theme 'zenburn)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; display options                                                        ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;enable tool and menu bars - good for beginners
(tool-bar-mode 1)
(menu-bar-mode 1)

These are my personal preferences and you can ignore these if you like. You can edit this file as you get more familiar with emacs to make further config changes.

  1. Now start emacs again and wait as it finishes setting itself up.

Optional Prelude Tweaks

For the following changes you will need to edit your ~/.emacs.d/personal/emacs_config.el, save the changes and restart emacs

  • Prelude uses the dark zenburn colour scheme. You can choose lots of others, but if you just want to use the light, default colour scheme, edit your ~/.emacs.d/personal/emacs_config.el file and uncomment the line mentioning zenburn, so it reads
    ;;uncomment this to use default theme
    (disable-theme 'zenburn)
    

    and then save the file and restart emacs

  • I find that the default highlight colour in the dark zenburn theme can be hard to see, so add the following to your config file to change the colour
    ;;change highlight colour
    (set-face-attribute 'region t :background "#164040")
    
  • Prelude highlights the ends of lines longer than 80 characters to encourage good code formatting. To prevent this, add the following to your emacs_config.el
    ;;don't highlight the end of long lines
    (setq whitespace-line-column 99999)
    
  • Prelude automatically saves your file frequently while you are editing. Instead, you can make emacs keep backups of your files frequently, but not overwrite the file. In this code, we tell it to make the backups in /tmp/emacs-backup, so look there if you ever loose or mess up a file!
    ;;turn off aggressive auto save
    (setq prelude-auto-save nil)
    (setq
     backup-by-copying t      ;; don't clobber symlinks
     backup-directory-alist
     '(("." . "/tmp/emacs-backups"))    ;; don't litter my fs tree
     delete-old-versions t
     kept-new-versions 6
     kept-old-versions 2
     version-control t)
    

    N.B. I copied this code from somewhere, but don’t remember where, so can’t give credit – sorry!

Starting to use emacs

The best thing to do is to just start using emacs for any text editing needs you have. Make use of the menus at first using the mouse, but as you do, note the keyboard shortcuts for common tasks and try to start using them. You will get the most out of emacs by using the keyboard shortcuts.

There are also lots of good emacs tutorials, including the built-in one (found under the help menu), this tutorial, and the new book at Mastering Emacs.

You can also start digging into my tips. The beginner category is a good place to start!

8 comments

  1. Prelude appears to have changed since you wrote these instructions.

    1. there is no ~/.emacs.d/prelude-modules.el

    2. curl http://www.star.bris.ac.uk/bjm/emacs_config.el -o ~/.emacs.d/personal/emacs_config.el fails, due to your security:

    Here’s the file that command gets

    403 Forbidden

    Forbidden

    You don’t have permission to access /bjm/emacs_config.el

    on this server.

    Apache/2.2.15 (Scientific Linux) Server at http://www.star.bris.ac.uk Port 80

    Like

    1. Sorry about the web permissions – I’ve updated the page above to just contain the code to paste into the file as it is not large.

      I tried a clean install of prelude and I have the ~/.emacs.d/prelude-modules.el as expected

      Note that as set up above, prelude will install org and auctex for you. I added ess to the list of extra packages to install as an example – I use it for my research too!

      Like

  2. Building emacs is nowhere near as trivial as you seem to expect. I don’t know how you get the canonical list of dependencies or the correct flags for ./configure, but I find the hand-waving here to be rather infuriating.

    For an example of what I’m talking about – I have no idea how this guy got his list of flags and dependencies – but without them, a lot of things don’t work as advertised: http://askubuntu.com/a/484472/247661

    Like

    1. It is a shame you found my advice to be hand-waving and infuriating, my intention is to be helpful. The fact is that when I last installed Emacs from source (on Scientific Linux a few years ago) then the commands above worked for me.

      In general, if you are able to install Emacs from a package manager it will make your life much less infuriating!

      Like

      1. Well that’s just giving up, and people are coming to this post looking to build emacs, not install it from a package manager. If this post is going to punt, at least link to some canonical location for instructions for building emacs.

        Like

  3. HI, and thanks for the article. I want to use Prelude. I read that it will backup my .emacs.d folder. So my two questions:

    1) if I want to revert to my previous Emacs configuration, shall I just unzip the backed-up zip emacs.d to replace the new one and my old Emacs will be back?
    2) If I want to stick with Prelude, how can I transfer all my configuration to the new “Emacs”? At the moment I have an init.org file with all the settings which is prompted by Babel: (org-babel-load-file “~/Documents/Dropbox/emacs/init/init.org”)
    (org-babel-load-file
    (expand-file-name “init.org”
    “~/Documents/Dropbox/emacs/init/”))
    So how to reconcile this with Prelude, can I manage to use this method to host my personal init file? If so, how?

    Thanks!
    Emmanu

    Like

Leave a comment