13

I want all files that I ever save in Sublime Text to be in Unix line ending format, even when I open files that were originally saved in a different format that I later edited in Sublime Text?

Simply setting "default_line_ending": "unix" is not enough, because that doesn't convert Windows files as I mentioned. How do I do that?

informatik01
  • 16,038
  • 10
  • 74
  • 104
DrStrangepork
  • 2,955
  • 2
  • 27
  • 33
  • 4
    `Menu > View > Line Endings` or enable the setting `"show_line_endings": false` then on the bottom right of the status bar it will show what the current line ending is, and when you click it you can select which one to change it to. – Gerard Roche Sep 26 '16 at 22:48

2 Answers2

17

In SublimeText3 (possibly other versions) you can define this in your User Preferences.

On the menu bar open "Preferences -> Settings"

In the right window pane that opens you will find you are editing "Preferences.sublime-settings -- User". In that pane enter the following JSON formatted options:

{
    // Determines what character(s) are used to terminate each line in new files.
    // Valid values are 'system' (whatever the OS uses), 'windows' (CRLF) and
    // 'unix' (LF only).
    "default_line_ending": "unix",

    // Display file encoding in the status bar
    "show_encoding": true,

    // Display line endings in the status bar
    "show_line_endings": true
}

NOTE: I added a couple extra features here. One to tell you what format of file you are working with and that files encoding format. These will appear in the tool bar in the bottom of your ST3 application.

Steven K7FAQ
  • 798
  • 1
  • 9
  • 17
16

Here's a quick plugin to do the job:

import sublime_plugin

class SetUnixLineEndingsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.set_line_endings("unix")


class SetLineEndings(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        view.run_command("set_unix_line_endings")

In Sublime, select Tools → Developer → New Plugin…. In the window that opens, delete everything that's there and replace it with the program above. Hit Save, and the save file dialog should open in your Packages/User directory, whose location varies by OS and type of install:

  • Linux: ~/.config/sublime-text-3/Packages
  • OS X: ~/Library/Application Support/Sublime Text 3/Packages
  • Windows Regular Install: C:\Users\YourUserName\AppData\Roaming\Sublime Text 3\Packages
  • Windows Portable Install: InstallationFolder\Sublime Text 3\Data\Packages

Save the file as set_unix_line_endings.py and it will activate immediately.

The plugin will only change the line endings of a file if you edit the contents and then save it. Just opening a file to view won't change anything.

If you no longer want the plugin active, just enter your Packages/User directory and either delete the file or change its suffix to something other than .py - set_unix_line_endings.py.bak works well for me.

MattDMo
  • 100,794
  • 21
  • 241
  • 231