在新电脑上设置emacs,包括init.el和安装软件包

3
像许多Emacs用户一样,我有自己的Emacs配置文件~/.emacs.d/init.el来配置我喜欢的Emacs。所以当我开始使用新机器时,我会将我的Emacs配置文件复制到它上面。现在,问题是我的Emacs配置文件依赖于我通过Emacs包管理器安装的一些软件包,但由于缺少这些软件包,我无法成功安装软件包。
当然,我可以启动没有我的配置文件的Emacs (emacs -q),但问题是只有默认仓库可用,所以我实际上不能安装我需要安装的软件包,以便成功地使用我的配置文件启动Emacs。
因此,我通常做的是暂时注释掉我的Emacs配置文件中的内容,以便我能够成功安装软件包,然后我可以取消注释并重新启动带有完整配置的Emacs。但这很麻烦,通常需要尝试几次才能注释掉所有必要的内容。肯定有我错过的更好的方法吧?

将您的整个配置放入版本控制中。然后设置Emacs只需克隆该存储库,就可以开始使用了。这还确保您获得每个软件包的已知工作版本,而不是最新版本(或者,如果运气不好,发现在那个时候无法下载某些软件包)。 - undefined
2个回答

6
你可以声明你使用的包。然后添加一些代码,每次打开Emacs时运行。它会检查该列表中的每个包是否已安装。如果没有安装,则会安装它。
以下是我的配置文件中的一个快速示例:
;; 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)))

而且你很棒。


3
您可以将安装所需包的初始化elisp放置在单独的文件中,然后启动emacs -q,再加载和评估包的elisp文件。
我使用以下代码,在其自己的文件中处理软件包。此代码还定义了我正在使用的软件包,并允许动态添加和加载软件包。
如果您首先从init.el加载此文件,那么您可能只需像往常一样启动Emacs,缺少的必需包将自动安装。
更新
我对使用defvar定义软件包列表变量的方式有些困扰。我做了一些阅读,并修复了下面的代码,现在它会defvar一个名为my-packages-package-list的变量,然后把它设置为要安装的软件包列表。据我所知,这是定义和使用变量的更惯用方式。结果,此代码现在已经编译没有任何警告。
对于那些感兴趣的人,可以在这里Emacs手册中找到有关使用defvarsetq的一些信息。
(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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接