8

How can you import a new chunk of code (an Emacs lisp library) into your .emacs file?

I do not want to put everything into one huge .emacs file.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

3 Answers3

14

Put the file google.el in a directory, say ~/lisp, and then in your .emacs:

(add-to-list 'load-path "~/lisp")
(require 'google)

If you want to add a directory and its sub-directories, you can check out the answers in this SO question.

And, as you add more and more 'require lines, you'll notice things slowing down on startup. At which point you'll want to find out how to make Emacs start up faster I of course like my answer best.

Community
  • 1
  • 1
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • Does this work for .el files nested in subdirectories under the load-path? I had to manually include a few .el files which were located lower than the ~/.emacs.d directory (where I store my .el files), though perhaps I did something wrong. – bedwyr Apr 26 '09 at 22:44
  • No, it doesn't work for nested directories. But I've added a link for how to do that. – Trey Jackson Apr 26 '09 at 23:01
1

elisp-load-dir can help, if you need to load many files at once. I use it to load per-topic setup files, which in turn only autoload the heavy stuff when actually needed:

.emacs
.emacs.d/
  lisp/
    elisp-load-dir.el
    ... other .el files that provide a feature
  rc/
    ... many small .el file that set variables, defaults, etc for me

So my .emacs is really minimal, it just adds ~/.emacs.d/lisp to the load path, so that I can install third-party extensions there. Then it requires elisp-load-dir and uses it to load whatever config files I have in ~/.emacs.d/rc:

(add-to-list 'load-path "~/.emacs.d/lisp")
(require 'elisp-load-dir)
(elisp-load-dir "~/.emacs.d/rc")
;; then comes all the custom-set-faces stuff that emacs puts there

The rc/*.el files are basically what you'd put in your .emacs, except it's modularized. For instance, I have one for each mode I regularly use, one for the startup, disabling the splashscreen, the toolbar, etc…

Damien Pollet
  • 6,488
  • 3
  • 27
  • 28
  • Could you give me an example how you use the code? For example, in moving from Work settings to freeTimeSettings. I am new in Lisp. – Léo Léopold Hertz 준영 Apr 26 '09 at 23:09
  • I added details. I don't think that answers your question on work/free time, but thet I'm not sure I understand what you mean. That could be a question on its own I guess. – Damien Pollet Apr 27 '09 at 10:53
1

You can also add an simple load statement in your .emacs file:

(load "/path/to/library.el")

Frankly, though, I like Trey's solution: it keeps all .el files in a single location.

Edit: removed the 'require' statement, as per Trey's statement.

bedwyr
  • 5,774
  • 4
  • 31
  • 49
  • What does the require statement means? I get the following error in Emacs http://dpaste.com/38167/ – Léo Léopold Hertz 준영 Apr 26 '09 at 23:03
  • The 'require in your solution is unnecessary as the 'require would result in a call to 'load anyway. – Trey Jackson Apr 26 '09 at 23:05
  • @Trey: How can you test that the Lisp files are included in Emacs? – Léo Léopold Hertz 준영 Apr 26 '09 at 23:11
  • The error message results because you have written (require 'google.el) but the library does (provide 'google). The symbol in require should not include the .el extension. You can also leave it out of the load statement, and Emacs will first look for a compiled .elc file, then for the source .el file. After the load the require is a no-op - require means "load the file unless it has been loaded already". – Jouni K. Seppänen Apr 27 '09 at 05:58