-
Notifications
You must be signed in to change notification settings - Fork 261
/
use-package.texi
1903 lines (1530 loc) · 63 KB
/
use-package.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\input texinfo @c -*- texinfo -*-
@c %**start of header
@setfilename ../../use-package.info
@settitle use-package User Manual
@include docstyle.texi
@syncodeindex vr cp
@syncodeindex fn cp
@c %**end of header
@copying
This manual is for use-package, a configuration macro for simplifying
your init file.
Copyright @copyright{} 2022 Free Software Foundation, Inc.
@quotation
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with the Front-Cover Texts being ``A GNU Manual'',
and with the Back-Cover Texts as in (a) below. A copy of the license
is included in the section entitled ``GNU Free Documentation License''.
(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
modify this GNU manual.''
@end quotation
@end copying
@dircategory Emacs misc features
@direntry
* use-package: (use-package). Declarative package configuration for Emacs.
@end direntry
@finalout
@titlepage
@title use-package User Manual
@subtitle for version 2.4.5
@author John Wiegley & Stefan Kangas
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@contents
@ifnottex
@node Top
@top use-package User Manual
The @code{use-package} macro allows you to isolate package
customization in your init file in a declarative way. It takes care
of a lot of things for you that would otherwise require a lot of
repetitive boilerplate code. It can help with common customization,
such as binding keys, setting up hooks, customizing user options and
faces, autoloading, and more. It also helps you keep Emacs startup
fast, even when you use many (even hundreds) of packages.
Note that use-package is not a package manager. Although use-package
does have the useful capability to interface with the Emacs package
manager, its primary purpose is for the configuration and loading of
packages.
@insertcopying
@menu
* Basic Concepts:: Basic concepts of use-package.
* Getting Started:: A gentle introduction to use-package.
* Loading Packages:: How and when packages are loaded.
* Configuring Packages:: Package configuration keywords.
* Installing packages:: Ensuring packages are available.
* Byte-compiling:: Byte-compiling your init file.
* Troubleshooting:: What to do when there's trouble.
Appendices
* Keyword extensions:: Adding new use-package keywords.
* History:: History and acknowledgments.
* GNU Free Documentation License:: The license for this manual.
* Index::
@end menu
@end ifnottex
@c ----------------------------------------------------------------------------
@node Basic Concepts
@chapter Basic Concepts
use-package provides the @code{use-package} macro, that simplifies the
customization and use of packages in Emacs. It was created for a few
basic reasons, each of which drove the design. Understanding these
reasons may help make some of those decisions clearer:
@enumerate
@item
To gather all configuration details of a package into one place,
making it easier to copy, disable, or move it elsewhere in the init
file.
@item
To reduce duplication and boilerplate, capturing several common
practices as mere keywords both easy and intuitive to use.
@item
To make startup time of Emacs as quick as possible, without
sacrificing the quantity of add-on packages used.
@item
To make it so errors encountered during startup disable only the
package raising the error, and as little else as possible, leaving as
close to a functional Emacs as possible.
@item
To allow byte-compilation of one's init file so that any warnings or
errors seen are meaningful. In this way, even if byte-compilation is
not used for speed (reason 3), it can still be used as a sanity check.
@end enumerate
It is worth noting that use-package is not intended to replace the
standard @w{@code{M-x customize}}. On the contrary, it is designed to
work together with it, for things that customize cannot do.
@c ----------------------------------------------------------------------------
@node Getting Started
@chapter Getting Started
This chapter provides instructions and examples for quickly getting
started with use-package. The first thing you need to do is make sure
that @samp{use-package} itself is loaded. To do that, put this at the
top of your init file:
@lisp
(require 'use-package)
@end lisp
The above makes the @code{use-macro} for in the rest of your init
file. In this manual, we call each call to @code{use-macro} a
@dfn{declaration}, to highlight the declarative nature of its
semantic.
To unconditionally load a package named @samp{foo}, add the following
declaration to your init file:
@lisp
(use-package foo)
@end lisp
@noindent
This declaration is equivalent to using @code{require}, with some
use-package specific error handling added in. Just like require, it
needs the package @samp{foo} to be installed and available in your
@code{load-path} (@pxref{Installing packages}).
To evaluate Lisp code @emph{before} the @samp{foo} package is loaded,
use the @code{:init} keyword:
@lisp
(use-package foo
:init
(setq foo-variable t))
@end lisp
Similarly, @code{:config} can be used to execute code @emph{after} a
package is loaded. In cases where loading is done lazily
(@pxref{Loading Packages}), this execution is deferred until after the
autoload occurs. As you might expect, you can use @code{:init} and
@code{:config} together:
@lisp
(use-package foo
:init
(setq foo-variable t)
:config
(foo-mode 1))
@end lisp
The above declarations will all load the @samp{foo} package
immediately. In most cases, this is not necessary or desirable, as
that will slow down Emacs startup. Instead, you should try to set
things up so that packages are only loaded when they are actually
needed (autoloading). If you have installed a package from
@acronym{GNU ELPA} that provides it's own autoloads, it is often
enough to say:
@lisp
(use-package foo
:defer t)
@end lisp
@noindent
This will avoid loading the package. Now, when you run any autoloaded
command, the package @samp{foo} is loaded automatically. Package
authors will make their own decisions about which commands are marked
to autoload by default.
In some cases, you might need or want to provide your own autoloads.
The below more complex example autoloads the commands
@code{isearch-moccur} and @code{isearch-all} from
@file{color-moccur.el}, and binds keys both globally and in
@code{isearch-mode-map}. When one of these commands are used, the
package is loaded. At that point, @code{moccur-edit} is also loaded,
to allow editing of the @code{moccur} buffer.
@lisp
(use-package color-moccur
:commands (isearch-moccur isearch-all)
:bind (("M-s O" . moccur)
:map isearch-mode-map
("M-o" . isearch-moccur)
("M-O" . isearch-moccur-all))
:init
(setq isearch-lazy-highlight t)
:config
(use-package moccur-edit))
@end lisp
Some packages will suggest ready-made @code{use-package} declarations
that you can use. Where possible, it is a good idea to copy them, and
use that as a starting point.
That should be enough to get you started!
@c ----------------------------------------------------------------------------
@node Loading Packages
@chapter Loading Packages
@cindex loading packages
Before use-package can load an Emacs Lisp package, it must be
available in a directory on your @code{load-path}. When you install
packages using the built-in @code{install-package} command, it will do
this automatically for you. Packages shipped with Emacs (built-in
packages) are always available.
If you install packages manually, you must make sure they are
available on your @code{load-path}. @xref{Lisp Libraries,,, emacs,
GNU Emacs Manual} for details.
Some packages have more than one library. In those cases, you might
need more than one @code{use-package} declaration to make sure it is
properly loaded. For complex configurations, you might also need more
than one declaration for a package with the same name.
use-package can interface with @samp{package.el} to install packages
on Emacs start. @xref{Installing packages} for details.
@menu
* Loading basics:: How and when packages are loaded.
* Deferring loading:: Loading packages later.
* Forcing loading:: Loading packages immediately.
* Conditional loading:: Loading packages conditionally.
* Loading sequentially:: Loading packages in sequence.
* Load dependencies:: Don't load without dependencies.
* Load path:: Using a custom @code{load-path}.
* Manual autoloads:: Setting up autoloads manually.
@end menu
@node Loading basics
@section How and when use-package loads packages
The @code{use-package} macro either will either load a package
immediately, or when they are first used (autoloading). In the
simplest case, a @code{use-package} declaration loads a package when
it is evaluated.@footnote{This happens both at run-time and at
compile-time. @xref{Byte-compiling}.} If the declaration is in your
init file, this happens automatically each time Emacs is started.
For example, the below declaration immediately loads the library
@code{foo}, just like @code{require} would. If the library @samp{foo}
is not available in your @code{load-path}, it logs a warning to the
@samp{*Messages*} buffer:
@lisp
(use-package foo)
@end lisp
Note that a ``package'' is different from an Emacs Lisp ``library''.
The above declaration tells use-package to load the @emph{library}
@file{foo.el}, which the overwhelming majority of cases also resides
in a @emph{package} named @code{foo}. But the @code{foo} package
might also contain a library named @file{foo-extra.el}. If that
library is not loaded automatically, you will need a separate
@code{use-package} declaration to make sure that it is. This manual
will often use these terms interchangeably, as this distinction does
not usually matter, but you should keep it in mind for the cases when
it does.
The details of how and when you should load a package might differ
from one package to another. When in doubt, refer to the package
documentation for details.
@node Deferring loading
@section Deferring package loading
@cindex autoloading packages
@cindex loading lazily
In the examples we have seen so far, use-package loads packages every
time you start Emacs, even if that package is never used. That will
make starting Emacs slower. use-package therefore tries to set things
up in such a way that it only loads packages when a command is first
used (either with @kbd{M-x} or some key binding). This is based on
autoloading, a full description of which is outside the scope of this
manual. @xref{Autoload,,, elisp, GNU Emacs Lisp Reference Manual} for
the full story.
@cindex triggers, for loading packages
Some @code{use-package} keywords provide autoload @dfn{triggers} that
cause a package to be loaded when certain events occur. For example,
the @code{:hook} keyword sets up a trigger that fires when the
specified hook is run, and then loads the package automatically. The
other trigger keywords, all of which are described later in this
manual, are @code{:commands}, @code{:bind}, @code{:bind*},
@code{:bind-keymap}, @code{:bind-keymap*}, @code{:mode}, and
@code{:interpreter}.
@subheading The @code{:defer} keyword
@findex :defer
If you did not specify any autoloading keyword, use-package will fall
back to loading the package immediately (typically when Emacs is
starting up). This can be overridden using the @code{:defer} keyword.
It takes one boolean argument: a non-@code{nil} value means to stop
this package from being immediately loaded. Here is an example of
using @code{:defer} to postpone loading the package @samp{foo}:
@lisp
(use-package foo
:defer t)
@end lisp
Using @code{:defer t} by itself like this is rarely useful.
Typically, you would only use it together with a keyword like
@code{:config} (@pxref{Lisp Configuration}), or @code{:ensure}
(@pxref{Installing packages}).
@subheading Defer loading until idle for N seconds
You can also give a numeric argument @var{N} to @w{@code{:defer}} to
specify that a package should be loaded (if it hasn't already) after
Emacs has been idle for @var{N} seconds. For example, use this to
make use-package load @samp{foo} after 30 seconds of idle time:
@lisp
(use-package foo
:defer 30)
@end lisp
@subheading When to use @code{:defer}
When using autoloading keywords, there is no need to also use
@code{:defer}. It doesn't hurt anything to add it in this case,
perhaps for extra clarity, but it is redundant.
You should use @code{:defer} to force deferred loading, in cases when
use-package isn't creating any autoloads for you. For example, you
might know that some other package will already do something to cause
your package to load at the appropriate time. This is usually the
case when you install a package using @code{package-install}, as
packages installed in this way normally always have their own
autoloads already set up.
@subheading Making @w{@code{:defer t}} the default
@vindex use-package-always-defer
If you customize the user option @code{use-package-always-defer} to
non-@code{nil}, the @code{use-package} macro will behave as if
@w{@code{:defer t}} is always specified. This can be overridden for
individual declarations using either @w{@code{:defer nil}} or
@w{@code{:demand t}} (@pxref{Forcing loading}).
@node Forcing loading
@section Forcing package to load immediately
@findex :demand
The presence of autoloading trigger keywords can be overridden using
@code{:demand t}, which forces the package to load immediately. Thus,
even if you use an autoloading keyword such as @code{:bind}
(@pxref{Key bindings}), adding @code{:demand} will force loading to
occur immediately. It will also avoid creating an autoload for the
bound key, as it would be redundant.
If you specify both @w{@code{:demand t}} and @w{@code{:defer t}}, the
@code{:defer} keyword will take precedence.
@node Conditional loading
@section Loading packages conditionally
@findex :if
@findex :when
@findex :unless
The @code{:if}, @code{:when}, and @code{:unless} keywords predicates
the loading and initialization of packages. They all accept one
argument, an Emacs Lisp form that is evaluated at run-time.
If the argument of the @code{:if} keyword evaluates to non-@code{nil},
the package will be loaded and initialized. The @code{:when} keyword
is provided as an alias for @code{:if}. Finally, the @code{:unless}
keyword is the inverse of @code{:if}, such that @w{@code{:unless foo}}
means the same thing as @w{@code{:if (not foo)}}.
For example, if you only want to load @samp{foo} in graphical Emacs
sessions, you could use the following:
@lisp
(use-package foo
:if (display-graphic-p))
@end lisp
@subheading Some common use cases
Here are some common cases for conditional loading, and how to achieve
them.
@itemize
@item Operating system
This example loads a package only on GNU/Linux. See the
@code{system-type} docstring for other valid values.
@lisp
:if (eq system-type 'gnu/linux)
@end lisp
@item Window system
This example loads a package only on macOS and X. See the
@code{window-system} docstring for valid values.
@lisp
:if (memq window-system '(ns x))
@end lisp
@item Installed package
This example loads a package only when the @samp{foo} package is
installed.
@lisp
:if (package-installed-p 'foo)
@end lisp
@item Libraries in @code{load-path}
This example loads a package only when @file{foo.el} is available in
your @code{load-path} (for example, if you installed that file
manually):
@lisp
:if (locate-library "foo.el")
@end lisp
@end itemize
@subheading Making conditional loading affect @code{:preface} and @code{:ensure}
@cindex conditional loading before @code{:preface} or @code{:ensure}
If you need to conditionalize a use-package form so that the condition
occurs before even @code{:ensure} or @code{:preface}, use @code{when}
around the use-package form itself. For example:
@lisp
(when (memq window-system '(mac ns))
(use-package foo
:ensure t))
@end lisp
@node Loading sequentially
@section Loading packages in sequence
@findex :after
Sometimes it only makes sense to configure a package after another one
has been loaded, because certain variables or functions are not in
scope until that time. This can achieved with the @code{:after}
keyword, which allows a fairly rich description of the exact
conditions when loading should occur. It takes either a symbol
indicating the package name, a list of such symbols, or a list of
selectors (see below).
Here is an example of using the @acronym{GNU ELPA} packages hydra,
ivy, and ivy-hydra. Note that ivy-hydra will always be loaded last:
@lisp
(use-package hydra)
(use-package ivy)
(use-package ivy-hydra
:after (ivy hydra))
@end lisp
In this case, because the declarations are evaluated in the order they
occur, the use of @code{:after} is not strictly necessary. However,
if @samp{hydra} and @samp{ivy} were to be autoloaded, using
@code{:after} guarantees that @samp{ivy-hydra} is not loaded until it
is actually needed. By using @code{:after}, the above code will also
work even if the order of the declaration changes. This means that
moving things around in your init file is less likely to break things.
@subheading Using @code{:after} selectors
@findex :all (with :after)
@findex :any (with :after)
The @code{:after} keyword also accepts a list of selectors. By
default, @code{:after (foo bar)} is the same as @w{@code{:after (:all
foo bar)}}, meaning that loading of the given package will not happen
until both @code{foo} and @code{bar} have been loaded. Here are some
of the other possibilities:
@verbatim
:after (foo bar)
:after (:all foo bar)
:after (:any foo bar)
:after (:all (:any foo bar) (:any baz quux))
:after (:any (:all foo bar) (:all baz quux))
@end verbatim
When you nest selectors, such as @code{(:any (:all foo bar) (:all baz
quux))}, it means that the package will be loaded when either both
@code{foo} and @code{bar} have been loaded, or when both @code{baz}
and @code{quux} have been loaded.
Pay attention when setting @code{use-package-always-defer} to a
non-@code{nil} value, and also using the @code{:after} keyword. In
this case, you will need to specify how the declared package is to be
loaded: for example, by some @code{:bind}. If you are not using one
of the keywords that registers autoloads, such as @code{:bind} or
@code{:hook}, and your package manager does not provide autoloads, it
is possible that your package will never be loaded if you do not add
@code{:demand t} to those declarations.
@node Load dependencies
@section Prevent loading if dependencies are missing
@findex :requires
While the @code{:after} keyword delays loading until the dependencies
are loaded, the somewhat simpler @code{:requires} keyword @emph{never}
loads the package if the dependencies are not available when the
@code{use-package} declaration is evaluated. In this context,
``available'' means that @code{foo} is available if @w{@code{(featurep
'foo)}} evaluates to a non-@code{nil} value. For example:
@lisp
(use-package abbrev
:requires foo)
@end lisp
This is the same as:
@lisp
(use-package abbrev
:if (featurep 'foo))
@end lisp
As a convenience, a list of such packages may be specified:
@lisp
(use-package abbrev
:requires (foo bar baz))
@end lisp
For more complex logic, such as that supported by @code{:after},
simply use @code{:if} and the appropriate Lisp expression.
@node Load path
@section Setting a custom @code{load-path}
@findex :load-path
If a package resides in some directory that is not in your
@code{load-path}, use the @code{:load-path} keyword to add it. It
takes a symbol, a function, a string or a list of strings. If the
path is relative, it is expanded within @code{user-emacs-directory}.
For example:
@lisp
(use-package ess-site
:load-path "site-lisp/ess/lisp/"
:commands R)
@end lisp
Note that when using a symbol or a function to provide a dynamically
generated list of paths, you must inform the byte-compiler of this
definition so that the value is available at byte-compilation time.
This is done by using the special form @code{eval-and-compile} (as
opposed to @code{eval-when-compile}). Further, this value is fixed at
whatever was determined during compilation, to avoid looking up the
same information again on each startup. For example:
@lisp
(eval-and-compile
(defun ess-site-load-path ()
(shell-command "find ~ -path ess/lisp")))
(use-package ess-site
:load-path (lambda () (list (ess-site-load-path)))
:commands R)
@end lisp
@node Manual autoloads
@section Setting up autoloads manually
@findex :commands
@findex :autoload
To autoload an interactive command, use the @code{:commands} keyword.
When you use the @code{:commands} keyword, it creates autoloads for
those commands (which defers loading of the module until they are
used). The @code{:commands} keyword takes either a symbol or a list
of symbols.
The @code{:autoload} keyword works like @code{:commands}, but is used
to autoload non-interactive functions. Here is an example:
@lisp
(use-package org-crypt
:autoload org-crypt-use-before-save-magic)
@end lisp
@c ----------------------------------------------------------------------------
@node Configuring Packages
@chapter Configuring Packages
This chapter describes the various keywords provided by
@code{use-package} that helps you configure packages.
@menu
* Lisp Configuration:: Using Lisp to configure packages.
* Key bindings:: Making your own keybindings.
* Hooks:: Adding functions to hooks.
* Modes and interpreters:: Enabling modes automatically.
* Magic handlers:: Using regexps to enable modes.
* User options:: Setting user options.
* Faces:: Customizing faces.
* Hiding minor modes:: Tidying up the mode line.
@end menu
@node Lisp Configuration
@section Using Lisp code for configuring packages
The most general way to add customizations are the @code{:preface},
@code{:init}, and @code{:config} keywords. They all accept one or
more Emacs Lisp forms, up to the next keyword, that are evaluated in
order. This lets you add arbitrary Lisp code to your
@code{use-package} declarations.
The only difference between these keywords is when they are evaluated.
@menu
* Preface keyword:: Evaluate code before anything else.
* Init keyword:: Evaluate code before loading package.
* Config keyword:: Evaluate code after loading package.
* Best practices:: When to use @code{:config}, @code{:init}, and @code{:preface}.
@end menu
@node Preface keyword
@subsection @code{:preface} is evaluated first
@findex :preface
The @code{:preface} section is evaluated before anything else, except
@code{:disabled} and @code{:ensure}. It can be used to establish
function and variable definitions that will:
@enumerate
@item
Make the byte-compiler happy. It will not complain about functions
whose definitions are unknown because you have them within a guard
block.
@item
Define code that can be used in an @code{:if} test.
@end enumerate
Note that whatever is specified within @code{:preface} is evaluated
both at load time and at byte-compilation time, in order to ensure
that definitions are seen by both the Lisp evaluator and the
byte-compiler. Therefore, you should avoid having any side-effects in
your preface, and restrict it to symbol declarations and definitions.
@node Init keyword
@subsection @code{:init} is evaluated before loading package
@findex :init
The @code{:init} section is evaluated just before the package is
loaded. Note that the @code{:init} form is run unconditionally --
even if the @code{foo} package happens to not exist on your system.
You must therefore remember to restrict @code{:init} code to only what
would succeed either way. @code{:init} also always happens before
package load, whether @code{:config} has been deferred or not.
@node Config keyword
@subsection @code{:config} is evaluated after loading package
@findex :config
The @code{:config} section is evaluated after the package has been
loaded. If the package is loaded immediately, this happens
immediately after that, but if loading is done lazily (@pxref{Loading
Packages}), this is deferred until after the package has been loaded.
In general, you should keep @code{:init} forms as simple and quick as
possible, and put as much as you can get away with into the
@code{:config} section. That way, deferred loading can help your
Emacs start as quickly as possible.
@node Best practices
@subheading When to use @code{:preface}, @code{:config} and @code{:init}?
Where possible, it is better to avoid @code{:preface}, @code{:config}
and @code{:init}. Instead, prefer autoloading keywords such as
@code{:bind}, @code{:hook}, and @code{:mode}, as they will take care
of setting up autoloads for you without any need for boilerplate code.
For example, consider the following declaration:
@lisp
(use-package foo
:init
(add-hook 'some-hook 'foo-mode))
@end lisp
This has two problems. First, it will unconditionally load the
package @samp{foo} on startup, which will make things slower. You can
fix this by adding @code{:defer t}:
@lisp
(use-package foo
:defer t
:init
(add-hook 'some-hook 'foo-mode))
@end lisp
This is better, as @samp{foo} is now only loaded when it is actually
needed (that is, when the hook @samp{some-hook} is run).
The second problem is that there is a lot of boilerplate that you have
to write. In this case, it might not be so bad, but avoiding that was
what use-package was made to avoid. The better option in this case is
therefore to use @code{:hook} (@xref{Hooks}), which also implies
@w{@code{:defer t}}. The above is thereby reduced down to:
@lisp
(use-package foo
:hook some-hook)
@end lisp
use-package will set up autoloading for you, and your Emacs startup
time will not suffer one bit.
@node Key bindings
@section Key bindings
@cindex :bind
@cindex binding keys
@cindex key bindings
One common thing to do when loading a package is to bind a key to
commands within that module. Without use-package, this would be done
using a combination of @code{keymap-local-set},
@code{keymap-global-set} and various autoloads. With use-package, you
can simplify this using the @code{:bind} keyword.
@menu
* Global keybindings:: Bindings you can use anywhere.
* Binding in keymaps:: Bindings for particular modes.
* Binding to a keymap:: Binding a key to a keymap.
* Binding to repeat-maps:: Binding repeating keys.
* Displaying keybindings:: Displaying personal key bindings.
@end menu
@node Global keybindings
@subsection Global keybindings
@findex :bind
To bind keys globally, the @code{:bind} keyword takes as its argument
either a single cons or a list of conses. Each cons has the form
@w{@code{(@var{key} . @var{definition})}}, where @var{key} is a string
indicating the key to bind, and @var{definition} is the name of a
command (a symbol). Alternatively, @var{definition} may be a cons
@w{@code{(@var{desc} . @var{command})}}, where @var{desc} is a string
describing @var{command}, which is the name of a command to bind
@var{key} to. The syntax for the keys is similar to the syntax used
by the @code{kbd} function (see @ref{Init Rebinding,,, emacs, GNU
Emacs Manual}, for more information).
@subheading Using @code{:bind} with a single cons
Here is an example of using a single cons:
@lisp
(use-package ace-jump-mode
:bind ("C-." . ace-jump-mode))
@end lisp
This does two things: first, it creates an autoload for the
@code{ace-jump-mode} command and defers loading of the
@code{ace-jump-mode} package until you actually use it. Second, it
binds the key @code{C-.} to that command globally.
@subheading Using @code{:bind} with a list of conses
Here is an example of using @code{:bind} with a list of conses:
@lisp
(use-package hi-lock
:bind (("M-o l" . highlight-lines-matching-regexp)
("M-o r" . highlight-regexp)
("M-o w" . highlight-phrase)))
@end lisp
@subheading Using special keys
Inside key strings, special keys like @kbd{TAB} or @kbd{F1}--@kbd{F12}
have to be written inside angle brackets, e.g. @code{"C-<up>"}.
Standalone special keys (and some combinations) can be written in
square brackets, e.g.@ @code{[tab]} instead of @code{"<tab>"}.
Examples:
@lisp
(use-package helm
:bind (("M-x" . helm-M-x)
("M-<f5>" . helm-find-files)
([f10] . helm-buffers-list)
([S-f10] . helm-recentf)))
@end lisp
@subheading Providing custom descriptions of commands
When binding keys to commands with @code{:bind}, custom descriptions
of the commands may optionally be provided.
Examples:
@lisp
@group
(use-package avy
:bind ("C-:" ("Jump to char" . avy-goto-char)
"M-g f" ("Jump to line" . avy-goto-line)))
@end group
@end lisp
@noindent
These descriptions can be used by other code that deals with key
bindings. For example, the @acronym{GNU} @acronym{ELPA} package
@file{which-key} displays them when showing key bindings, instead of
the plain command names.
@subheading Remapping commands
Remapping commands with @code{:bind} and @code{bind-key} works as
expected, because when the binding is a vector, it is passed straight
to @code{define-key}. @xref{Remapping Commands,,, elisp, GNU Emacs
Lisp Reference Manual}) for more information about command remapping.
For example, the following declaration will rebind
@code{fill-paragraph} (bound to @kbd{M-q} by default) to
@code{unfill-toggle}:
@lisp
(use-package unfill
:bind ([remap fill-paragraph] . unfill-toggle))
@end lisp
@subheading What @code{:bind} does behind the scenes
To understand what @code{:bind} does behind the scenes, it might be
useful to consider an example:
@lisp
(use-package ace-jump-mode
:bind ("C-." . ace-jump-mode))
@end lisp
This could be expressed in a much more verbose way with the
@code{:commands} and @code{:init} keywords.
@lisp
(use-package ace-jump-mode
:commands ace-jump-mode
:init
(bind-key "C-." 'ace-jump-mode))
@end lisp
Without using even the @code{:commands} keyword, we could also write
the above like so:
@lisp
(use-package ace-jump-mode
:defer t
:init
(autoload 'ace-jump-mode "ace-jump-mode" nil t)
(bind-key "C-." 'ace-jump-mode))
@end lisp
Although these three forms are all equivalent, the first form is
usually the best, as it will save some typing.
@node Binding in keymaps
@subsection Key bindings in local keymaps
@findex :map, inside :bind
Slightly different from binding a key to a keymap, is binding a key
@emph{within} a local keymap that only exists after the package is
loaded. @code{use-package} supports this with a @code{:map} modifier,
taking the local keymap to bind to:
@lisp
(use-package helm
:bind (:map helm-command-map
("C-c h" . helm-execute-persistent-action)))
@end lisp
The effect of this statement is to wait until @code{helm} has loaded,
and then to bind the key @code{C-c h} to
@code{helm-execute-persistent-action} within Helm's local keymap,
@code{helm-command-map}.
Multiple uses of @code{:map} may be specified. Any binding occurring
before the first use of @code{:map} are applied to the global keymap:
@lisp
(use-package term
:bind (("C-c t" . term)
:map term-mode-map
("M-p" . term-send-up)
("M-n" . term-send-down)
:map term-raw-map
("M-o" . other-window)
("M-p" . term-send-up)
("M-n" . term-send-down)))
@end lisp
@node Binding to a keymap
@subsection Binding to keymaps
@findex :bind-keymap, inside :bind
Normally @code{:bind} expects that commands are functions that will be
autoloaded from the given package. However, this does not work if one of
those commands is actually a keymap, since keymaps are not functions,
and cannot be autoloaded using the built-in @code{autoload} function.
To handle this case, @code{use-package} offers a special, limited
variant of @code{:bind} called @code{:bind-keymap}. The only difference
is that the ``commands'' bound to by @code{:bind-keymap} must be keymaps
defined in the package, rather than command functions. This is handled
behind the scenes by generating custom code that loads the package
containing the keymap, and then re-executes your keypress after the
first load, to reinterpret that keypress as a prefix key.
For example:
@lisp
(use-package foo
:bind-keymap ("C-c p" . foo-command-map))
@end lisp
@node Binding to repeat-maps
@subsection Binding to repeat-maps
@findex :repeat-map, inside :bind
@cindex repeat-mode and use-package, using
A special case of binding within a local keymap is when that keymap is
used by @code{repeat-mode} @pxref{Repeating,,, emacs, GNU Emacs
Manual}. These keymaps are usually defined specifically for
this. Using the @code{:repeat-map} keyword, and passing it a name for
the map it defines, will bind all following keys inside that map, and
(by default) set the @code{repeat-map} property of each bound command
to that map.
The following example creates a keymap called
@code{git-gutter+-repeat-map}, makes four bindings in it as above,
then sets the @code{repeat-map} property of each bound command
(@code{git-gutter+-next-hunk} @code{git-gutter+-previous-hunk},
@code{git-gutter+-stage-hunks} and @code{git-gutter+-revert-hunk}) to
that keymap.
@lisp
(use-package git-gutter+
:bind
(:repeat-map git-gutter+-repeat-map
("n" . git-gutter+-next-hunk)
("p" . git-gutter+-previous-hunk)
("s" . git-gutter+-stage-hunks)
("r" . git-gutter+-revert-hunk)))
@end lisp
@findex :exit, inside :repeat-map and :bind
Specifying @code{:exit} inside the scope of @code{:repeat-map} will
prevent the @code{repeat-map} property being set, so that the command
can be used from within the repeat map, but after it using it the repeat
map will no longer be available. This is useful for commands often used
at the end of a series of repeated commands:
@lisp
(use-package git-gutter+
:bind
(:repeat-map my/git-gutter+-repeat-map
("n" . git-gutter+-next-hunk)
("p" . git-gutter+-previous-hunk)
("s" . git-gutter+-stage-hunks)
("r" . git-gutter+-revert-hunk)
:exit
("c" . magit-commit-create)
("C" . magit-commit)
("b" . magit-blame)))
@end lisp
@findex :continue, inside :repeat-map and :bind
Specifying @code{:continue} @emph{forces} setting the