emacs启动时自动安装package

版权声明:本文为博主原创文章,未经博主允许不得转载。更好的样式阅读体验可点击文章底部<<查看原文>> https://blog.csdn.net/huuinn/article/details/80150771

简介

本文主要介绍如何在emacs中,自动安装/更新package

init.el中配置追加

(require 'package)
(setq package-archives
  '(("gnu" . "https://elpa.gnu.org/packages/")
     ("melpa" . "https://melpa.org/packages/")
     ("org" . "http://orgmode.org/elpa/")))
(package-initialize)

(defun require-package (package &optional min-version no-refresh)
  "Install given PACKAGE, optionally requiring MIN-VERSION.
If NO-REFRESH is non-nil, the available package lists will not be
re-downloaded in order to locate PACKAGE."
  (if (package-installed-p package min-version)
    t
    (if (or (assoc package package-archive-contents) no-refresh)
      (if (boundp 'package-selected-packages)
        ;; Record this as a package the user installed explicitly
        (package-install package nil)
        (package-install package))
      (progn
        (package-refresh-contents)
        (require-package package min-version t)))))

(defun maybe-require-package (package &optional min-version no-refresh)
  "Try to install PACKAGE, and return non-nil if successful.
In the event of failure, return nil and print a warning message.
Optionally require MIN-VERSION.  If NO-REFRESH is non-nil, the
available package lists will not be re-downloaded in order to
locate PACKAGE."
  (condition-case err
    (require-package package min-version no-refresh)
    (error
      (message "Couldn't install optional package `%s': %S" package err)
      nil)))

使用范例

  • 安装multiple-cursors
(require-package 'multiple-cursors)
如果emacs启动时发现package不存在时,会自动下载安装
  • 追加配置
(when (maybe-require-package 'multiple-cursors)
  (global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
  (global-set-key (kbd "C->") 'mc/mark-next-like-this))
只在当multiple-cursors安装成功时,才会调整global-set-key方法来设置快捷键

查看原文:https://www.huuinn.com/archives/615
更多技术干货:风匀坊
关注公众号:风匀坊

猜你喜欢

转载自blog.csdn.net/huuinn/article/details/80150771