18

I'm currently on a mac and am trying to set Emacs to not add a newline at the end of a file by default, but pretty much everything I search for says to just add (setq require-final-newline nil) to the .emacs file in the home directory... This does not work. What should I be looking at next to change?

This is what the .emacs file looks like right now

(custom-set-variables
  ;; custom-set-variables 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.
 '(gud-gdb-command-name "gdb --annotate=1")
 '(large-file-warning-threshold nil)
 '(require-final-newline nil))
(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.
 )
Jayson
  • 940
  • 8
  • 14
  • 2
    The more pressing question is: "Why would you actually change this sane and useful default besides to shoot yourself in the foot?" – pmr Nov 26 '12 at 23:28
  • 1
    Because I am editing an input file for a program that expects an end of file character as the last thing in the file, but emacs is adding a newline after that character. – Jayson Nov 26 '12 at 23:34
  • 1
    What is the major mode of the file that is exhibiting this problem? – Trey Jackson Nov 26 '12 at 23:43
  • @pmr Yeah, a better way would be to set it locally to `nil`. – legends2k Sep 25 '17 at 22:35

1 Answers1

22

Have you tried:

(setq mode-require-final-newline nil)

It is possible the major mode for the file is using the value of this variable instead...

It's not immediately obvious how to find out what modes use this instead of the variable you tried, but presumably the writer of that mode knows better...

So, it's possible you might want to change the major mode for that file, which can be accomplished by the answer to this SQ question: How to tell emacs to open .h file in C++ mode? (obviously customzinging the answer to suit your need).

Community
  • 1
  • 1
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • I just tried it and this fixed the issue for me. I'm not sure what the "major mode" is though so I'm not sure I could give you a coherent answer (yet) – Jayson Nov 26 '12 at 23:49
  • 1
    To find out the major-mode for a particular file (buffer), type `C-h m' (or `M-x describe-mode`) and a new buffer will show up which describes the modes (including the major mode) for the current buffer. – Trey Jackson Nov 26 '12 at 23:54
  • 1
    @Jayson The "major mode" is the set of syntax highlighting, keybindings, and other behaviors associated with your current buffer. It should be listed in your modeline, in parentheses. You should see something like `U:--- Filename All L10 (Major Minor)` The "Major" part of that is the major mode (you may only have a major mode, or you may have a major and one or more minor modes). – Brian Campbell Nov 26 '12 at 23:58
  • For me (emacs 23.3.1) I had to use (setq require-final-newline nil) for this to work – HXCaine Mar 17 '14 at 12:17
  • @HXCaine - the question already is setting that, so yes, you do need that in some cases. In other cases, that variable is set from `mode-require-final-newline`. See the documentation for `require-final-newline` for more information. – Trey Jackson Mar 17 '14 at 14:46