无法在 emacs 中安装一些 melpa 包

Can't install some melpa packages in emacs

我正在试用 emacs(来自 vim)。

$  emacs --version
GNU Emacs 27.2
Copyright (C) 2021 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

$ cat ~/.emacs.d/init.el
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
;; Comment/uncomment this line to enable MELPA Stable if desired.  See `package-archive-priorities`
;; and `package-pinned-packages`. Most users will not need or want to do this.
;;(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t)
(package-initialize)
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

我已 运行 M-x package-refresh-contents 多次,但仍然没有列出许多我想安装的软件包。例如,go-mode

知道我哪里搞砸了吗?谢谢!

编辑

我刚刚试用了 spacemacs,我想要的包出现在列表中...

根据您的描述,MELPA 包似乎根本没有加载。
因此,首先,检查 C-h v package-archives 输出的内容。返回列表中是否输出'melpa'?

您也可以 运行 M-x package-list-packages 看看在列出的结果中是否有任何 melpa 存档包。

在我的配置中,我以这种方式明确设置包存档:

(require 'package)

(setq package-archives
      '(("gnu" . "https://elpa.gnu.org/packages/")
        ("gnu-devel" . "https://elpa.gnu.org/devel/")
        ("nongnu" . "https://elpa.nongnu.org/nongnu/")
        ("melpa" . "https://melpa.org/packages/")))

此外,由于 Emacs 27.1 "it is no longer necessary to call 'package-initialize' in your init file"

因此,您应该可以安全地删除 (package-initialize) 调用或有条件地使用它,例如:

(when (< emacs-major-version 27)
  (package-initialize))

如评论中所述,您得到的行为只是 package.el 的一个功能(不是错误 :)

也就是说,在使用 M-x package-install RET … 安装了一个 MELPA 包后,不再建议安装此包。

要检查软件包是否确实已安装,您可以:

  • 键入 M-x package-list-packages RETRET = Return 键),
    并搜索您的包裹名称:C-s go-mode C-s C-s … (C-s = Ctrl+S).
  • 或者 运行 ls ~/.emacs.d/elpa/ 看看是否有 go-mode-YYYYMMDD.* 目录。

旁注

不时升级所有已安装的 MELPA 包可能会有用,方法是键入:

  • M-x package-list-packages RET
  • r(刷新包列表)
  • U(标记可升级包)
  • x(执行安装和删除)

另外,除了内置的 Emacs 包管理器,您可能对 use-package 工具感兴趣:

  • 以声明的方式帮助组织 ~/.emacs 文件,
  • 并在自定义软件包时自动安装软件包(即无需手动输入 M-x package-install);

→作为使用的例子,你可以看看this Gist of mine to help installing Magit.