Temporarly disable adding of newlines in Emacs

孤街浪徒 提交于 2019-12-11 02:49:33

问题


I've enabled require-final-newline, because I usually want Emacs to add newlines to my files where it's missing. But there are some cases, where I want Emacs to remove the newline (like when editing a yasnippet that should not produce a newline, see emacs + latex + yasnippet: Why are newlines inserted after a snippet?).

Is there a way to achieve this temporarly (like enabling a mode or something), without having to change .emacs and restarting Emacs?


回答1:


In addition to what @eldrich mentioned, you can set its value locally in a given buffer. Put something like this on a mode hook, to turn it off for a given mode:

(defun foo () (set (make-local-variable 'require-final-newline) nil))
(add-hook 'some-mode-hook 'foo)



回答2:


Anything that is set in the .emacs file can be changed on the fly. For variables such as require-final-newline, this just involves changing the variable. For example, you could type the following code, then use C-x e to evaluate it.

(setq require-final-newline (not require-final-newline))

This could then be bound as a keyboard shortcut, if you so desire.

(defun toggle-final-newline
    (interactive)
    (setq require-final-newline (not require-final-newline)))
(global-set-key (kbd "C-c f") 'toggle-final-newline)



回答3:


I think It should be:

(defun toggle-final-newline ()
    (interactive)
    (setq require-final-newline (not require-final-newline)))
(global-set-key (kbd "C-c f") 'toggle-final-newline)


来源:https://stackoverflow.com/questions/23738897/temporarly-disable-adding-of-newlines-in-emacs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!