{"id":730,"date":"2023-06-12T12:25:18","date_gmt":"2023-06-12T06:55:18","guid":{"rendered":"https:\/\/www.lguruprasad.in\/blog\/?p=730"},"modified":"2023-06-12T12:25:24","modified_gmt":"2023-06-12T06:55:24","slug":"automatically-delete-the-trailing-whitespace-on-save-in-emacs-while-excluding-the-trailing-newlines","status":"publish","type":"post","link":"https:\/\/www.lguruprasad.in\/blog\/2023\/06\/12\/automatically-delete-the-trailing-whitespace-on-save-in-emacs-while-excluding-the-trailing-newlines\/","title":{"rendered":"Automatically delete the trailing whitespace on save in emacs while excluding the trailing newlines"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I use <a rel=\"noreferrer noopener\" href=\"https:\/\/www.gnu.org\/software\/emacs\/\" target=\"_blank\">emacs<\/a> as my editor and have it configured to delete all trailing whitespace in a file, including any trailing newlines at the end, before saving it. The configuration snippet that I have in my emacs configuration file to do this is<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(add-hook 'before-save-hook 'delete-trailing-whitespace)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">While this is very convenient and works as expected, it becomes a hindrance in specific cases &#8211; the <a rel=\"noreferrer noopener\" href=\"https:\/\/jinja.palletsprojects.com\" target=\"_blank\">Jinja2 templating language<\/a> deletes a single trailing newline, thereby leaving the rendered templates without a newline at the end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One way to work around this behaviour is to add 2 trailing newlines in the <code>jinja2<\/code> template files. But unfortunately, due to my emacs configuration that deletes all trailing whitespace, this doesn&#8217;t work. So I started reading the documentation for the <code>delete-trailing-whitespace<\/code> function and found out about the <code>delete-trailing-newlines<\/code> variable (default: <code>t<\/code>). This variable controls whether the trailing newline characters at the end of a file are deleted or not. So I wanted to try overriding the <code>delete-trailing-newlines<\/code> variable to be false in <code>jinja2-mode<\/code>, that I use for editing Jinja2 templates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With some help from the excellent folks in the <code>#emacs<\/code> IRC channel on <a rel=\"noreferrer noopener\" href=\"https:\/\/libera.chat\/\" target=\"_blank\">Libera Chat<\/a>, I was able to come up with the following configuration, that works as expected.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(use-package jinja2-mode                                                                     \n  :pin nongnu                                                                                \n  :hook                                                                                      \n  (jinja2-mode . (lambda ()                                                                  \n                   (setq-local delete-trailing-lines nil)))                                  \n  :mode \"\\\\.j2\\\\'\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that I use the excellent <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/jwiegley\/use-package\" target=\"_blank\">use-package macro<\/a> to install the <code>jinja2-mode<\/code> and configure it appropriately. If you don&#8217;t use <code>use-package<\/code>, this can be done using the <a href=\"https:\/\/www.gnu.org\/software\/emacs\/manual\/html_node\/elisp\/Setting-Hooks.html#index-add_002dhook\" target=\"_blank\" rel=\"noreferrer noopener\">add-hook<\/a> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use emacs as my editor and have it configured to delete all trailing whitespace in a file, including any trailing newlines at the end, before saving it. The configuration snippet that I have in my emacs configuration file to do this is While this is very convenient and works as expected, it becomes a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[12],"tags":[350,351,353,352,354],"class_list":["post-730","post","type-post","status-publish","format-standard","hentry","category-foss","tag-emacs","tag-jinja2","tag-trailing-newlines","tag-trailing-whitespace","tag-use-package"],"_links":{"self":[{"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/posts\/730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/comments?post=730"}],"version-history":[{"count":4,"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/posts\/730\/revisions"}],"predecessor-version":[{"id":734,"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/posts\/730\/revisions\/734"}],"wp:attachment":[{"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/media?parent=730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/categories?post=730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lguruprasad.in\/blog\/wp-json\/wp\/v2\/tags?post=730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}