IT干货网

init 文件中的 Emacs 包安装脚本

wayfarer 2026年07月31日 编程设计 102 0

我重组了我的 Emacs init 文件以将它们置于版本控制之下。文件系统布局如下所示:

/home/axel/.user_config/emacs 
├── development.el 
├── general.el 
├── init.el 
├── packages.el 
└── writing.el 
packages.el包含 Emacs 应在启动时安装的软件包列表(如果尚未安装)。在我做 rm -rf ~/.emacs.d/* 后,这没有问题.当我之后启动 Emacs 时, packages.el 中列出的所有软件包已安装。但是,当我手动将包添加到(例如 markdown-mode+ )到列表时 pfl-packagespackages.el重新启动 Emacs 时未安装新软件包。删除 ~/.emacs.d/的内容再次解决了这个问题,但我希望能够将包名称添加到列表中,然后 Emacs 在启动时自动安装。我是否遗漏了有关 Emacs 初始化过程的一些关键信息?

请参阅以下相关文件的内容。

文件 ~/.emacs包含以下内容:
(add-to-list 'load-path "~/.user_config/emacs/") 
(load-library "init") 

文件 ~/.user_config/emacs/init.el包含:
;;;; loads the different libraries to set up Emacs accordingly 
 
;; load the library that sets up package repositories and syncing 
(load-library "packages") 
 
;; load the library that sets up the general behavior of Emacs 
(load-library "general") 
(load-library "writing") 
(load-library "development") 

文件 packages.el包含以下内容:
;;;; this library sets up package repositories and allows for syncing    packages 
;;;; between different machines 
;;;; to add packages to the syncing, add their repository names to the   list `pfl-packages' 
 
;;; set up package repositories from which additional packages can be installed 
 
;; set up ELPA and MELPA repositories 
(package-initialize) 
(add-to-list 'package-archives 
                     '("melpa" . "http://melpa.org/packages/")) 
(add-to-list 'package-archives 
                     '("org" . "http://orgmode.org/elpa/")) 
 
 
;;; set up package syncing to allow for syncing between different machines 
 
;; list of packages to sync 
(setq pfl-packages 
        '( 
            color-theme-sanityinc-solarized 
            company 
            company-auctex 
            company-c-headers 
            company-irony 
            company-quickhelp 
            elpy 
            ess 
            flycheck-irony 
            irony 
            magit 
            markdown-mode 
            markdown-mode+ 
            rainbow-delimiters 
            smart-tabs-mode 
            yasnippet 
            )) 
 
;; refresh package list if it is not already available 
(when (not package-archive-contents) (package-refresh-contents)) 
 
;; install packages from the list that are not yet installed 
(dolist (pkg pfl-packages) 
    (when (and (not (package-installed-p pkg)) (assoc pkg package-archive-contents)) 
        (package-install pkg))) 

请您参考如下方法:

在我的 ~/.emacs.d目录,我在 init.el 中有下列:

(load "~/.emacs.d/init-packages") 

init-packages.el我已经完成了您想要做的事情:启动时,emacs 检查是否已安装列出的每个包,如果没有,则已安装。当您想要安装新软件包时,只需将其添加到列表中即可。

值得注意的是 package-initialize必须在添加相关包存档后调用,以便实际获取并安装它们。

以下是 init-packages.el的内容:
(require 'package) 
 
(add-to-list 'package-archives 
             '("elpy" . "http://jorgenschaefer.github.io/packages/")) 
 
(add-to-list 'package-archives 
             '("marmalade" . "http://marmalade-repo.org/packages/")) 
 
(add-to-list 'package-archives 
             '("melpa-stable" . "http://melpa-stable.milkbox.net/packages/") t) 
 
(add-to-list 'load-path "~/.emacs.d/site-lisp/") 
 
 
; list the packages you want 
(setq package-list 
    '(python-environment deferred epc  
        flycheck ctable jedi concurrent company cyberpunk-theme elpy  
        yasnippet pyvenv highlight-indentation find-file-in-project  
        sql-indent sql exec-path-from-shell iedit 
        auto-complete popup let-alist magit git-rebase-mode  
        git-commit-mode minimap popup)) 
 
 
; activate all the packages 
(package-initialize) 
 
; fetch the list of packages available  
(unless package-archive-contents 
  (package-refresh-contents)) 
 
; install the missing packages 
(dolist (package package-list) 
  (unless (package-installed-p package) 
    (package-install package))) 

希望这就是你要找的!


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!