关于emacs:emacs插件4包的配置usepackage

一、参考

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))

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理