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

送分小仙女□ 提交于 2019-12-04 16:21:19

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!