Difference between revisions of "User's Initialization File"

From WikEmacs
Jump to navigation Jump to search
(speed up emacs start up: use-package)
Line 35: Line 35:
 
     (depends-on "dash")
 
     (depends-on "dash")
 
     (depends-on "evil")
 
     (depends-on "evil")
 +
 +
=== With use-package ===
 +
 +
'''Use-package''' (see below) is a macro that cleans emacs's configuration and speeds up its start up. We can use it also to manage our packages with [[package.el]]. See [https://github.com/jwiegley/use-package#for-packageel-users its documentation]. The relevant packages are downloaded automatically once declared in your .emacs. The '':ensure'' keyword causes the package(s) to be installed automatically if not already present on your system:
 +
 +
  (use-package magit
 +
  :ensure t)
 +
 +
We can also choose prefered package sources (between GNU Elpa, Melpa-stable or Melpa), and more.
  
 
=== With org-mode ===
 
=== With org-mode ===

Revision as of 11:04, 16 July 2015

User's Initialization File or dotemacs or init file is a file to store your configurations/customizations for Emacs written in Emacs Lisp, located at either ${HOME}/.emacs.d/init.el or (archaically) at ${HOME}/.emacs or at %HOME%\_emacs on MS Windows.

The most reliable way to find our the place of the initialization file on your system is to check the value of the user-init-file variable.

Configuration Example

For a good example of an init file check out some of the starter kits, like Prelude.

Debugging the Init file

$ emacs --debug-init

Running the above command will enable the Emacs Lisp debugger for errors in the init file.

A more advanced usage is to use elisp's bug hunter. It will find the source of the error for you. You can install it through ELPA and call it interactively:

 M-x bug-hunter-init-file RET e

How to package your config and packages

Will come a moment when you want to duplicate your emacs configuration into another computer. And you realize that you rely on a lot of third party packages that you don't want to re-install manually ! So how can we package our config, along with all our packages ? There are some options.

First you would obviously bundle everything into source control (except for a private file). Bitbucket and Gitlab.com offer private repos.

You then must read the page on package.el to see a way to list all the packages you use.

With Cask

You can use Cask to manage all the dependencies. A Cask file lists them all, like

   (depends-on "cask")
   (depends-on "dash")
   (depends-on "evil")

With use-package

Use-package (see below) is a macro that cleans emacs's configuration and speeds up its start up. We can use it also to manage our packages with package.el. See its documentation. The relevant packages are downloaded automatically once declared in your .emacs. The :ensure keyword causes the package(s) to be installed automatically if not already present on your system:

 (use-package magit
  :ensure t)

We can also choose prefered package sources (between GNU Elpa, Melpa-stable or Melpa), and more.

With org-mode

Some write their config in org-mode and load it with a call to org-babel, which is doable in one line in ~/.emacs.d/init.el:

   (require 'org)
   (require 'ob-tangle)
   (org-babel-load-file (expand-file-name "~/.emacs.d/myemacs.org"))

See an example config with Cask and org-mode.


some split it in multiple elisp files, some keep it in one big file.

Finally, to speed up the installation and ensure the same environment, you can also commit the pckages installed with package.el.

You can take inspiration from Starter Kits.

Speed up Emacs's start up

Use-package

Use-package is a macro that allows you to isolate package configuration in your .emacs file in a way that is both performance-oriented and, well, tidy. The author created it because he has over 80 packages that he uses in Emacs, and things were getting difficult to manage. Yet with this utility his total load time is around 2 seconds, with no loss of functionality. The key point is that it loads packages lazily. So your emacs starts up, you can start using it, and the longest loadings are done in the background.

The simplest use-package declaration looks like this:

 (use-package foo)

This loads in the package foo, but only if foo is available on your system.

Then we have keywords to add functionality. We can call code before and after a package is loaded (:init and :conf), :bind keybindings, do conditional loading, etc.


See Also

  • Prelude, a much more powerful and productive set of initial configuration than the one you get out of the box.
  • Emacs Starter Kit
  • package.el, the built-in package manager in Emacs 24.

External links