如何让Emacs重新加载我在.emacs中更新的所有定义而不重新启动Emacs?


下面就应该做到了…

M-x load-file

如果你在当前活动缓冲区中打开了.emacs文件:

M-x eval-buffer

其他人已经回答了你的问题,但我发现我通常想执行我刚刚写的行。

为此,Elisp部分的Ctrl + Alt + X工作得很好。

我建议你一开始不要这样做。相反,启动一个新的Emacs会话并测试您所做的任何更改,以查看它们是否正常工作。这样做的原因是为了避免让您处于无法操作.emacs文件的状态,该文件无法加载或无法干净地加载。如果在原始会话中进行所有的编辑,在新会话中进行所有的测试,那么总有一些可靠的东西可以注释掉有问题的代码。

当您最终对更改感到满意时,继续使用其他答案之一重新加载。我个人倾向于只计算您添加/更改的部分,要做到这一点,只需突出显示添加/更改代码的区域,并调用M-x eval-region。luapyad指出,这样做可以最大限度地减少所评估的代码,最大限度地减少任何无意的副作用。

您可以使用命令load-file (M-x load-file,然后按Return两次以接受默认文件名,即当前正在编辑的文件)。

你也可以把这个点移动到任何性别p的末尾,然后按C-x, C-e来执行那个性别p。如果您只是更改一两行,通常不需要重新加载整个文件。

M-x load-file
~/.emacs

你通常可以重新计算改变的区域。标记~/区域。然后使用M-x eval-region RET。这通常比重新评估整个文件更安全,因为很容易编写一个.emacs文件,在加载两次后就不能正常工作。

尽管M-x eval-buffer可以工作,但是在切换和其他类似的事情上可能会遇到问题。更好的方法可能是“标记”或突出显示.emacs文件中的新内容(如果只是瞎折腾,甚至可以标记缓冲区),然后是M-x eval-region。

在init文件中定义它,并通过M-x reload-user-init-file调用

(defun reload-user-init-file()
  (interactive)
  (load-file user-init-file))
C-x C-e ;; current line
M-x eval-region ;; region
M-x eval-buffer ;; whole buffer
M-x load-file ~/.emacs.d/init.el

有很方便

m x eval-buffer

它立即计算缓冲区中的所有代码。如果你的。emacs文件是幂等的,这是最快的方法。

我目前使用的是Ubuntu 15.04 (Vivid Vervet);我喜欢为它定义一个键。

[M-insert]在我的键盘上翻译成Alt + Ins。

把这个放在你的emacs文件中:

(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))

这里有一个快速和简单的方法来快速测试您的配置。您还可以在特定的lisp末尾使用C-x C-e来单独执行某些函数。

C-x C-e runs the command eval-last-sexp (found in global-map), which is an interactive compiled Lisp function. It is bound to C-x C-e. (eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL) Evaluate sexp before point; print value in the echo area. Interactively, with prefix argument, print output into current buffer. Normally, this function truncates long output according to the value of the variables ‘eval-expression-print-length’ and ‘eval-expression-print-level’. With a prefix argument of zero, however, there is no such truncation. Such a prefix argument also causes integers to be printed in several additional formats (octal, hexadecimal, and character). If ‘eval-expression-debug-on-error’ is non-nil, which is the default, this command arranges for all errors to enter the debugger.

键盘快捷键:

(defun reload-init-file ()
  (interactive)
  (load-file user-init-file))

(global-set-key (kbd "C-c C-l") 'reload-init-file)    ; Reload .emacs file

如果你碰巧在Emacs内部打开了一个shell,你还可以这样做:

. ~/.emacs

这样可以节省一些按键。

你可以像这样为Emacs设置键绑定:

;; Reload Emacs configuration
(defun reload-init-file ()
  (interactive)
  (load-file "~/.emacs"))

(global-set-key (kbd "C-c r") 'reload-init-file)

除了M-x eval-buffer或M-x load-file这样的命令,您还可以从命令行重新启动一个新的Emacs实例:

emacs -q --load "init.el"

使用示例:GNU Emacs中的公司后端

解决方案

M-(加载user-init-file)


笔记

你在Eval: prompt中输入它(包括括号) User-init-file是一个保存~/的变量。Emacs值(指向配置文件路径) (load)是(load-file)的较短、较旧且非交互式的版本;它不是一个Emacs命令(要在M-x中输入),而仅仅是一个Elisp函数


结论

M-: > M-x