Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 3 - ensure _only_ one trailing newline at end of file

Tags:

I'm using Sublime Text 3 and want to ensure I get only a single new line at the end of a file on save. At the moment, 90% of the time my whitespace works perfectly, using:

"ensure_newline_at_eof_on_save": true

and

"trim_trailing_white_space_on_save": true

... however, every once in a while the file saves with two new lines at the end of the file.

This appears to occur when there is white space on the final newline prior to save, with the config settings adding a newline, then deleting the whitespace. Changing the order of these settings in config doesn't resolve this.

I haven't been able to find other reasons of this, so it may well be the only cause, though ideally I'd like to check if there's ever more than one newline on save.

The environment I'm working in fails its tests unless a file has exactly one new line at its end, so this is a bit of a pain. My question is whether there's a plugin / way to be stricter on save, ensuring one and only one trailing new line.

like image 448
SRack Avatar asked Jun 13 '16 16:06

SRack


People also ask

How do I insert a line break in sublime?

To insert a new line below the current line in your code, hit Cmd–Return (Mac) or Ctrl–Enter(Windows). You can also insert a new line above the current line by using Cmd–Shift–Return(Mac) or Ctrl–Shift–Enter (Windows).


Video Answer


1 Answers

EDIT:

I've expanded the plugin, that I posted below, a lot and it is now available to install using Package Control.

  • Single Trailing Newline is a Sublime Text package that makes sure that there is exactly one trailing newline at the end of a file. It works by deleting all the whitespace and newlines at the end of the file (if there are any) and then inserting a single newline.

  • The plugin can be set to run automatically every time a file is saved. This is disabled by default but by changing the settings it can be enabled either for all files or for only files of specific syntaxes.

  • Command palette entries are provided to change the package's settings; adding/removing the syntaxes that will trigger the plugin, and to allow or prevent the plugin from running with all syntaxes.

Original Answer:

Here's a plugin that will do it.

Save the following code in a file with a .py extension, e.g. EnsureExactlyOneTrailingNewLineAtEndOfFileOnSave.py and copy the file into your packages directory. When you save a file it will strip all trailing newlines and whitespace at the end of the file and then add a single trailing newline.

# # A Sublime Text plugin to ensure that exactly one trailing # newline is at the end of all files when files are saved. # # License: MIT License #  import sublime, sublime_plugin  class OneTrailingNewLineAtEndOfFileOnSaveListener(sublime_plugin.EventListener):      def on_pre_save(self, view):         # A sublime_plugin.TextCommand class is needed for an edit object.         view.run_command("one_trailing_new_line_at_end_of_file")         return None  class OneTrailingNewLineAtEndOfFileCommand(sublime_plugin.TextCommand):      def run(self, edit):         # Ignore empty files.         if self.view.size() == 0:             return          # Work backwards from the end of the file looking for the last         # significant char (one that is neither whitespace nor a newline).          pos = self.view.size() - 1         whitespace = ("\n", "\t", " ")          while pos >= 0 and self.view.substr(pos) in whitespace:             pos -= 1          # Delete from the last significant char to the end of         # the file and then add a single trailing newline.          del_region = sublime.Region(pos + 1, self.view.size())         self.view.erase(edit, del_region)         self.view.insert(edit, self.view.size(), "\n") 
like image 151
mattst Avatar answered Nov 18 '22 13:11

mattst