簡體   English   中英

使用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 -q )的情況下啟動emacs,但是問題是只有默認存儲庫可用,因此我無法實際安裝需要安裝的軟件包,才能成功通過配置文件啟動emacs 。

因此,我通常要做的是暫時將我的emacs配置文件中的內容注釋掉,這樣我就可以成功安裝軟件包,然后可以取消注釋並使用我的完整配置重新啟動emacs。 但這很麻煩,在我注釋掉所有需要的內容之前,通常需要嘗試幾次。 當然,一定有更好的方法讓我丟失嗎?

您可以將用於安裝所需軟件包的初始化elisp放在單獨的文件中,然后啟動emacs -q ,然后加載並評估軟件包elisp文件。

我在自己的文件中使用以下代碼來處理軟件包。 該代碼還定義了我正在使用的軟件包,並允許動態添加和加載軟件包。

如果首先從init.el加載此文件,則您可能可以照常啟動Emacs,並且缺少的必需軟件包將自動安裝。

更新資料

我對使用defvar定義軟件包列表變量的方式感到有些defvar 我已經閱讀並修復了以下代碼-現在將其defvar變量my-packages-package-list ,然后將其setq到要安裝的軟件包列表中。 據我了解,這是定義和使用變量的更慣用的方式。 結果,該代碼現在已字節編譯,沒有任何警告。

對於感興趣的人,可以在此處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)

您可以做的是聲明您使用的軟件包。 然后添加一些每次打開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)))

而且你很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM