ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Single​Trailing​New​Line

by mattst ST2/ST3

A Sublime Text package to make sure that there is exactly one trailing newline at the end of a file.

Details

Installs

  • Total 1K
  • Win 336
  • OS X 740
  • Linux 306
Jan 22 Jan 21 Jan 20 Jan 19 Jan 18 Jan 17 Jan 16 Jan 15 Jan 14 Jan 13 Jan 12 Jan 11 Jan 10 Jan 9 Jan 8 Jan 7 Jan 6 Jan 5 Jan 4 Jan 3 Jan 2 Jan 1 Dec 31 Dec 30 Dec 29 Dec 28 Dec 27 Dec 26 Dec 25 Dec 24 Dec 23 Dec 22 Dec 21 Dec 20 Dec 19 Dec 18 Dec 17 Dec 16 Dec 15 Dec 14 Dec 13 Dec 12 Dec 11 Dec 10 Dec 9 Dec 8
Windows 0 0 0 0 2 0 0 0 1 0 0 0 0 1 2 0 0 1 1 1 1 0 1 0 0 0 1 2 1 0 1 0 1 0 2 0 0 1 2 1 0 1 0 0 0 0
OS X 0 2 2 3 1 1 1 0 3 0 0 2 1 4 2 0 0 1 1 3 1 0 0 0 0 1 0 1 1 0 1 2 2 1 1 0 0 1 1 2 0 2 0 0 0 3
Linux 0 1 0 3 0 0 0 1 0 0 0 1 1 0 1 0 0 2 2 1 0 0 0 0 3 0 0 0 2 2 0 2 1 1 1 0 0 0 1 0 1 0 0 0 0 0

Readme

Source
raw.​githubusercontent.​com

Single Trailing Newline - Package for Sublime Text

This Sublime Text package makes sure that there is exactly one trailing newline at the end of a file.

All the whitespace and trailing newlines at the end of the file (if any) are removed and then a single newline is inserted.

Empty files, those with a file size of zero, are deliberately ignored and a newline will not be inserted into them.

Note: This is different from the ensure_newline_at_eof_on_save setting which makes sure that there is at least one trailing newline at the end of the file, rather than making sure that there is exactly one trailing newline at the end of the file.

Features

  • It 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.

  • It can also be run by using its command palette entry or by assigning it to a key binding. In both these cases the settings file is ignored and it will work with all files.

Requirements

  • Sublime Text v2 or v3
  • Tested with: v2 build 2221 and v3 builds 3114 and 3083

Installation Instructions

Package Control

  • Open the command palette and select: Package Control: Install Package
  • Wait for the package list to be updated and then select: SingleTrailingNewLine

Manual Installation

  • Download the package's zip file and extract it, or use git clone to get the package from its GitHub page.
  • Move the package's folder to your Sublime Text config Packages folder. Where is that?
  • Rename it from sublime-single-trailing-new-line-master to SingleTrailingNewLine.
  • You should end up with this folder: Packages/SingleTrailingNewLine/

Setup and Usage

The plugin was designed to be run automatically when a file is saved, rather than by manually running it from the command palette or with a key binding. However some users may prefer to run it manually on an as-needed basis, or to have that option available for syntaxes they have not added to the syntaxes list setting.

Command Palette

The package includes several command palette commands:

  • Single Trailing New Line: ensure there is exactly one trailing newline at the end of the file. When run from the command palette the plugin will work with all files, the settings file will be ignored.

  • Single Trailing New Line: Add Current Syntax: adds the full syntax name of the current file to the syntax list setting.

  • Single Trailing New Line: Remove Current Syntax: removes the full syntax name of the current file from the syntax list setting.

  • Single Trailing NewLine: Run For All Syntaxes: sets the enable for all syntaxes setting to true.

  • Single Trailing NewLine: Do Not Run For All Syntaxes: sets the enable for all syntaxes setting to false.

  • Single Trailing New Line: Copy Current Syntax: copies the full syntax name of the current file into the clipboard. [Legacy: No longer really needed now that syntaxes can be added/removed using the command palette.]

Key Bindings

When run from a key binding the plugin will work with all files, the settings file will be ignored.

Add the key binding of your choice to your user key bindings file (Menu --> Preferences --> Key Bindings - User):

{ "keys": ["ctrl+whatever"], "command": "single_trailing_new_line" }

There are now so many Sublime Text packages available that key conflicts are commonplace, it is therefore best for users to decide their own keys for small packages like this one.

Not sure? You could try: "ctrl+k", "ctrl+n" (which is not in use on my system).

For users who would like to use keys for all the features here are the available commands:

  • single_trailing_new_line: ensures there is exactly one trailing newline at the end of the file.
  • single_trailing_new_line_add_syntax: adds the current syntax to the syntaxes list setting.
  • single_trailing_new_line_remove_syntax: removes the current syntax from the syntaxes list setting.
  • single_trailing_new_line_copy_syntax: copies the current syntax into the clipboard.
  • single_trailing_new_line_enable_for_all_syntaxes_setting: sets the enable for all syntaxes setting to true or false.
    • Requires an arg called "value" to be set to true or false, e.g. "args": {"value": true}.

Settings / Running Automatically

The plugin can be run automatically every time a file is saved to ensure that the file is saved with exactly one trailing newline but this is disabled by default. It can be enabled for either all files or for only files of specific syntaxes by changing the default settings in the package's user settings file.

The path of the package's user settings file, in your Sublime Text config folders (where is that?), must be:

Packages/User/SingleTrailingNewLine.sublime-settings

The default and user settings files can be accessed from the Sublime Text menu:

Menu --> Preferences --> Package Settings --> SingleTrailingNewLine --> Settings - Default
Menu --> Preferences --> Package Settings --> SingleTrailingNewLine --> Settings - User

When changing the settings make sure you use the user settings file.

There are 2 settings:

  • enable_for_all_syntaxes: true / false. If this is set to true the plugin will be active for all files, regardless of their syntax, every time any file is saved the plugin will be run. Default: false. It is important to note that setting this to false does not disable the plugin completely, it just prevents it from running for all syntaxes. When it is set to false the plugin will still run for the syntaxes listed in the enable_for_syntaxes_list setting.

  • enable_for_syntaxes_list: a list of strings. If one or more syntax names are added to this list, the plugin will be run every time a file, whose syntax matches one of the syntax names, is saved. Default: [] - an empty list. This setting will be ignored if the enable_for_all_syntaxes setting is set to true. There is further information about this setting below the example settings file.

The .sublime-syntax syntax file format was introduced by Sublime Text v3 build 3084, from that version onwards all built-in syntax files use that format, with the exception of Packages/Text/Plain text.tmLanguage. Other .tmLanguage syntax files may be on your system if a syntax package that includes them has been installed, e.g. PythonImproved. Sublime Text v2 and v3 builds earlier than 3084 use only .tmLanguage syntax files.

Here is an example Packages/User/SingleTrailingNewLine.sublime-settings file:

{
    "enable_for_all_syntaxes": false,
    "enable_for_syntaxes_list":
    [
        "Packages/C++/C.sublime-syntax",
        "Packages/C++/C++.sublime-syntax",
        "Packages/Java/Java.sublime-syntax",
        "Packages/JavaScript/JSON.sublime-syntax",
        "Packages/Python/Python.sublime-syntax",
        "Packages/Python Improved/PythonImproved.tmLanguage",
        "Packages/Rails/",
        "Packages/XML/XML.sublime-syntax"
    ]
}

The following information about the enable_for_syntaxes_list entries should be noted:

  • They are case sensitive and neither regular expressions nor wildcards are accepted.

  • It is generally advisable to use full syntax names, as shown in the example file above. Partial syntax names will work, e.g. "XML.sublime-syntax", but only full syntax names will be added and removed when using the palette commands.

  • In fact any case sensitive substring match will work; e.g. the list ["C++", "Java", "Python", "Rails", "XML"] would match all of the syntaxes shown in the example file above, but it would also match a whole lot more (17 of the ST v3 default syntaxes).

  • "C.sublime-syntax" would match both "Packages/C++/C.sublime-syntax" and "Packages/Objective-C/Objective-C.sublime-syntax". Using just "C" on its own would be inadvisable because it would match any syntax containing an upper-case letter C (16 of the ST v3 default syntaxes).

  • All of the syntaxes in a (multi-syntax) package can be matched by using the syntax's path component on its own. e.g. "Packages/Rails/" would match all 5 syntaxes from the Rails package, i.e. "Packages/Rails/Ruby on Rails.sublime-syntax" as well as the other 4.

  • Using just "Java" would match all 7 syntaxes from the Java and JavaScript packages, as would "Packages/Java" but not "Packages/Java/" because the trailing slash would not match "Packages/JavaScript/".

For reference use only, 2 lists of Sublime Text built-in syntaxes are provided:

Those using Sublime Text v3 builds earlier than 3084 should refer to the v2 list.

License

This package is licensed under The MIT License (MIT). See the LICENSE file.