hupf
hupf

Reputation: 604

Temporarly disable adding of newlines in Emacs

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?

Upvotes: 2

Views: 730

Answers (3)

user5208749
user5208749

Reputation:

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)

Upvotes: 0

Drew
Drew

Reputation: 30701

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)

Upvotes: 2

Eldritch Cheese
Eldritch Cheese

Reputation: 1237

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)

Upvotes: 2

Related Questions