Sublime Forum

Is there any way to convert file's line ending on saving?

#1

Couldn’t find any suitable plugin on package control.

0 Likes

#2

Have you tried looking in the View menu -> Line Endings -> …?

5 Likes

#3

Thank you for replying, I know I can manually convert line ending, but I have to do this manually for every each single file, I really hope I can do this automatically.
Also I know how to write script to convert all the files together, but that would lead a huge change list on our P4, will affect all the other colleagues, so I hope to do it quietly, single file each time I edited it.

0 Likes

#4

Save this as change_line_endings.py in your User package folder. I generally wouldn’t recommend this, but I hope you know what you’re doing with this.

import sublime_plugin

# Options are: "Windows", "Unix", "CR" (don't use CR)
PREFERRED_LINEENDINGS = "Unix"


class SilentlyChangeLineEndingsListener(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        if view.line_endings() != PREFERRED_LINEENDINGS:
            view.set_line_endings(PREFERRED_LINEENDINGS)
5 Likes

#5

Wow, awesome, write custom plugin, almost forget this :smile:
OK, I’ll go this way, good side is I can check and convert the encoding at the same time, appreciate!
I just wonder such simple task, sublime text doesn’t support and even package control doesnt have a suitable one, or it’s just a dumb task that no one ever need it😆

0 Likes

#6

As I mentioned, I generally wouldn’t recommend it because you either change line endings manually (on demand) or automatically, in which case you’d rather batch-edit many files using a commandline tool like dos2unix (or unix2dos). Other than that, you generally want to respect the original line endings used by a file so you don’t cause a mess in version control software or in other software that reads a file and expects certain line endings (Windows’s notepad.exe can’t handle UNIX line breaks, for example).

Add to the fact that this is a very trivial plugin and it’s not very surprising that a plugin like this does not exist. Or maybe it even does exist, but you didn’t find it (and I didn’t even bother to look).

For encodings, there certainly are more packages, so one of them might provide what you would be looking for.

1 Like

#7

Just to provide a less aggressive solution. You can also create a keybinding to change the line endings and save. Install Chain of Commands and add this to your keymap:

    {
        "keys": ["alt+s"],  // use "ctrl+s" to overwrite save
        "command": "chain",
        "args": {
            "commands": [
                ["set_line_ending", {"type": "unix"}], // also possible: "unix", "windows", "cr"
                ["save"]
            ]
        },
    },
4 Likes

#8

My situation here is quit tricky, we had our line ending & encoding standard, but we don’t have any tool for restriction of everybody, some of interns were using different line ending & encoding deal some of the source file in chaos, after we realize it, it like a hundred has been messed, it’s not that big deal to let everybody in the team to be aware this, so I rather to change them one by one once I touch the file. It’s just that simple, I am familiar the situation and all the tricky things behind this, very thank you for all the suggestion, I’ll make my custom plugin to do this:)

0 Likes

#9

More friendly indeed, big thx!!! :slightly_smiling:

0 Likes

#10

thanks this actually helped me! a slight improvement to the script: add regexp to configure more finely the files we want to convert:

class SilentlyChangeLineEndingsListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
if re.search(’.ksh|.sh|.cfg|.conf$’, view.file_name()):
if view.line_endings() != PREFERRED_LINEENDINGS:
view.set_line_endings(PREFERRED_LINEENDINGS)

0 Likes

#11

Install the EditorConfig Sublime package, and create an EditorConfig file https://editorconfig.org/
This will allow standardised configurations across multilple editors

0 Likes

#12

For anyone who got here from the Internet wondering how to set the default line endings used by Sublime without using the plugin above:

Preferences > Settings > add: “default_line_ending”: “unix”

Mind the JSON syntax.

1 Like

#13
"ensure_newline_at_eof_on_save": true
0 Likes

#14
// Settings in here override those in "Default/Preferences.sublime-settings",
// and are overridden in turn by syntax-specific settings.
{
        "default_line_ending": "unix",
}
1 Like

#15

Just select on menu: [“View” -> “Word Wrap”]

0 Likes