一、参考
emacs系列文章目录——更新ing
use-package
Configuring Emacs from Scratch — use-package
二、为什么须要use-package
?
当配置好init.el
后,常常会呈现上面的两个问题
2.1 可移植性
当须要在其余机器中,配置emacs
时候,如果不能保障须要的packages
都装置,可能导致init.el
中的许多配置不能失效
2.2 代码构造太扁平
因为在init.el
中,有多个packages
都须要配置,然而init.el
是所有包的配置文件,随着packages
的增加,可能会呈现配置之间的抵触
2.3 解决packages
的装置问题
(defvar my-packages '(spacemacs-theme company))(dolist (p my-packages) (when (not (package-installed-p p)) (package-install p)))
下面的形式,能够解决 2.1 可移植性
,然而不能很好的解决 2.2 代码构造
引入新的package
——use-package
能够很好的解决下面的两个问题
三、根本应用
3.1 装置
M-x package-install <RET> use-package <RET>
3.2 根本语法
(use-package <package-name> :init <code to be executed before loading the package> <加载包之前,执行的代码> :config <code to be executed after loading the package> <加载包之后,执行的代码> :bind <key bindings for this package>)
3.3 示例
(1) 没有应用use-package
(global-company-mode t)(define-key company-active-map (kbd "C-n") 'company-select-next)(define-key company-active-map (kbd "C-p") 'company-select-previous)(setq company-idle-delay 0.0)
(2) 应用use-package
(use-package company :bind (:map company-active-map ("C-n" . company-select-next) ("C-p" . company-select-previous)) :config (setq company-idle-delay 0.3) (global-company-mode t))
3.4 保障应用的package
都存在
(use-package magit :ensure t :bind ("C-x g" . magit-status))
语句:ensure t
保障了所有本地不存在的package
都会被装置后在应用
3.5 保障use-package
装置
(when (not (package-installed-p 'use-package)) (package-refresh-contents) (package-install 'use-package))