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!

  • Bill Raynor

    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

    • Ben Maughan

      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!

      • Bill Raynor

        Did another clean install and this one worked. Thanks for the help.

  • Antonio Juan

    Hi thanks for this great site, I follow some tips of you, I just would point that for installing the latest emacs in mac just use Homebrew,

    brew install emacs –HEAD –use-git-head –with-cocoa –with-gnutls –with-rsvg –with-imagemagick

    For windows I think that the best way is following the tutorial from the emacs github mirror

    https://github.com/emacs-mirror/emacs/blob/master/nt/INSTALL.W64

  • aaronchall

    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

    • 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!