Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do configure sublime to always convert to unix line endings on save?

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? Simply setting "default_line_ending": "unix" is not enough, because that doesn't convert Windows files as I mentioned. How do I do that?

like image 276
DrStrangepork Avatar asked Sep 24 '16 20:09

DrStrangepork


People also ask

How do you change line endings in Sublime Text?

You can use dos2unix command in linux (package dos2unix) or change line endings in Sublime Text under menu item View / Line Endings.

How do I change Unix line endings in Windows?

Converting using Notepad++ To write your file in this way, while you have the file open, go to the Edit menu, select the "EOL Conversion" submenu, and from the options that come up select "UNIX/OSX Format". The next time you save the file, its line endings will, all going well, be saved with UNIX-style line endings.

What line endings does Linux use?

Back to line endings The reasons don't matter: Windows chose the CR/LF model, while Linux uses the \n model. So, when you create a file on one system and use it on the other, hilarity ensues.


2 Answers

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.

like image 169
MattDMo Avatar answered Sep 29 '22 04:09

MattDMo


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.

like image 43
Steven K7FAQ Avatar answered Sep 29 '22 04:09

Steven K7FAQ