使用 init.el 和软件包安装在新机器上设置 emacs

Setting up emacs on new machine with init.el and package installation

可能像许多 emacs 用户一样,我有自己的 emacs 配置文件 ~/.emacs.d/init.el 以我喜欢的方式配置 emacs。所以当我开始使用一台新机器时,我将我的 emacs 配置文件复制到它。现在,问题是我的 emacs 配置文件依赖于我通过 emacs 包管理器安装的几个包,但由于缺少包,我无法成功安装包。

我当然可以在没有我的配置文件的情况下启动 emacs (emacs -q),但是问题是只有默认的 repo 可用,所以我实际上无法安装我需要安装的包使用我的配置文件成功启动 emacs。

所以我通常做的是暂时注释掉我的 emacs 配置文件中的内容,以便我能够成功安装软件包,然后我可以取消注释并使用我的完整配置重新启动 emacs。但这很麻烦,在我注释掉所有需要的东西之前通常需要尝试几次。肯定有我想念的更好的方法吗?

您可以将安装所需软件包的初始化 elisp 放在单独的文件中,然后启动 emacs -q,然后加载并评估软件包 elisp 文件。

我在自己的文件中使用以下代码来处理包。此代码还定义了我正在使用的包,并允许动态添加和加载包。

如果您首先从 init.el 加载此文件,那么您可能可以像往常一样启动 Emacs,并且会自动安装缺少的必需包。

更新

我对使用 defvar 定义包列表变量的方式感到有些困扰。我已经阅读并修复了下面的代码 — 现在它 defvar 一个变量 my-packages-package-list 然后 setq 它到要安装的包列表。据我所知,这是一种更惯用的定义和使用变量的方式。因此,这段代码现在是字节编译的,没有任何警告。

有兴趣的朋友可以参考here and in the Emacs` manualsetq的一些使用信息here and in the Emacs` manual

(require 'package)

(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
                         ;; ("marmalade" . "https://marmalade-repo.org/packages/")
                         ("melpa" . "https://melpa.org/packages/")
             ("org" . "https://orgmode.org/elpa/")))


(setq package-archive-priorities '(("melpa" . 10)
                   ("gnu" . 5)
                   ("org" . 2)
                   ;; ("marmalade" . 0)
                   ))

(package-initialize)

(when (not package-archive-contents)
  (package-refresh-contents))

;; the following code will install packages listed in myPackages if
;; they are not already installed
;; https://realpython.com/emacs-the-best-python-editor/

(defvar my-packages-package-list "List of custom packages to install.")

;;; this allows for dynamically update and install packages while
;;; Emacs is running, by modifying this list, and then evaluating it
;;; and tha mapc expression below it
(setq my-packages-package-list
      '(;; add the ein package (Emacs ipython notebook)
    ein

    ;; python development environment
    elpy

    ;; beutify python code
    py-autopep8

    ;; git emacs interface
    magit

    ;; debuggers front end
    realgud

    ;; multiple major mode for web editing
    ;; multi-web-mode

    ;; major mode for editing web templates
    web-mode

    ;; docker modes
    docker-compose-mode
    dockerfile-mode

    ;; list library for emacs
    dash
    ;; collection of useful combinators for emacs lisp
    dash-functional

    ;; major modes for yaml
    yaml-mode

    ;; major modes for markdown
    markdown-mode

    ;; major modes for lua
    lua-mode

    ;; major modes for fvwm config files
    fvwm-mode

    ;; treat undo history as a tree
    undo-tree

    ;; flychek
    ;; flychek-clojure
    ;; flychek-pycheckers

    ;; Clojure for the brave and true - below; amit - some packages
    ;; commented out by me until I'll be sure they are needed

    ;; makes handling lisp expressions much, much easier
    ;; Cheatsheet: http://www.emacswiki.org/emacs/PareditCheatsheet
    paredit

    ;; key bindings and code colorization for Clojure
    ;; https://github.com/clojure-emacs/clojure-mode
    clojure-mode

    ;; extra syntax highlighting for clojure
    clojure-mode-extra-font-locking

    ;; integration with a Clojure REPL
    ;; https://github.com/clojure-emacs/cider
    cider

    ;; allow ido usage in as many contexts as possible. see
    ;; customizations/navigation.el line 23 for a description
    ;; of ido
    ;; ido-ubiquitous

    ;; Enhances M-x to allow easier execution of commands. Provides
    ;; a filterable list of possible commands in the minibuffer
    ;; http://www.emacswiki.org/emacs/Smex
    ;; smex

    ;; project navigation
    ;; projectile

    ;; colorful parenthesis matching
    rainbow-delimiters

    ;; solarized theme
    solarized-theme

    ;; edit html tags like sexps
    ;; tagedit

    ;; help finding keys
    which-key

    ;; xkcd
    xkcd

    ;; Clojure exercises
    4clojure
))

(mapc #'(lambda (package)
    (unless (package-installed-p package)
      (package-install package)))
      my-packages-package-list)

您可以做的是声明您使用的包。然后添加一些每次打开 Emacs 时运行的代码。它会检查该列表中的每个包是否已安装。如果不是,它会安装它。

来自我的 config file 的简单示例:

;; first, declare repositories
(setq package-archives
      '(("gnu" . "http://elpa.gnu.org/packages/")
        ("marmalade" . "http://marmalade-repo.org/packages/")
        ("melpa" . "http://melpa.org/packages/")))

;; Init the package facility
(require 'package)
(package-initialize)
;; (package-refresh-contents) ;; this line is commented 
;; since refreshing packages is time-consuming and should be done on demand

;; Declare packages
(setq my-packages
      '(cider
        projectile
        clojure-mode
        expand-region
        helm
        jinja2-mode
        magit
        markdown-mode
        paredit
        wrap-region
        yaml-mode
        json-mode))

;; Iterate on packages and install missing ones
(dolist (pkg my-packages)
  (unless (package-installed-p pkg)
    (package-install pkg)))

而且你很好。