180

Here is my Settings - User config:

{
    "auto_indent": true,
    "color_scheme": "Packages/Color Scheme - Default/Twilight.tmTheme",
    "default_line_ending": "LF",
    "detect_indentation": true,
    "font_size": 10.0,
    "ignored_packages":
    [
        "Vintage"
    ],
    "indent_to_bracket": false,
    "smart_indent": true,
    "tab_size": 4,
    "translate_tabs_to_spaces": true,
    "trim_automatic_white_space": true,
    "use_tab_stops": true
}

Comment to default_line_ending option says: Comment to default_line_ending option

When I create a new file, I check line ending here:

Check line ending

As you can see it's still Windows... Any ideas?

mintobit
  • 2,363
  • 2
  • 15
  • 15

4 Answers4

201

The comment states

// 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).

You are setting

"default_line_ending": "LF",

You should set

"default_line_ending": "unix",

Leigh
  • 12,859
  • 3
  • 39
  • 60
  • 1
    I have the same problem but i'm correctly setting `"default_line_ending": "unix"`, i dot't know why the hell that doesn't work! Its driving me mad! I've research a little and apparently it has no useful fix. – acrespo Jan 10 '13 at 16:15
  • @acrespo Are you sure it's not being overridden by another preferences file? Sublime has lots of them :) - If you've specified one kind of line ending in your syntax specific prefs, it will override user prefs, which in turn override global prefs – Leigh Jan 10 '13 at 17:08
  • I've never changed any of the syntax prefs, but i'm currently using rails syntax prefs, perhaps it's that? How can i see those settings? – acrespo Jan 10 '13 at 19:19
  • @acrespo On OSX it's `Sublime Text 2 -> Preferences -> Settings - More -> Syntax Specific - User` - I imagine it's a similar path on windows/linux if you're using those. – Leigh Jan 11 '13 at 09:40
  • I've added the config to syntax prefs but still not working (the file with the settings was previously empty, so that couldn't be overriding anything). I seriously don't know what the heck is wrong with this. – acrespo Jan 11 '13 at 14:02
  • @acrespo I really doubt I can help, you can double check what it's actually set to by bringing up the python console (ctrl + backtick) and running `view.settings().get('default_line_ending')` - Maybe they're set correctly in Sublime and getting messed up elsewhere. – Leigh Jan 11 '13 at 15:31
  • 11
    setting "default_line_ending": "unix", works fine on new files.. but not on existing(older) files. Does anyone know how to change that behavior? – soothsayer Apr 18 '13 at 21:34
  • 11
    This can only be done manually, would be really helpful if this was automated. You can change this as seen in the image above View->Line Endings->Unix @soothsayer – A Star Sep 30 '13 at 09:48
  • 5
    @soothsayer, for existing files, select all the text, then `view` -> `line endings` -> `unix`. Then save the document. – AGS Dec 18 '13 at 12:20
  • is this the same settings for Sublime3? – Joe Platano Dec 02 '16 at 03:33
9

The EditorConfig project (Github link) is another very viable solution. Similar to sftp-config.json and .sublime-project/workspace sort of file, once you set up a .editorconfig file, either in project folder or in a parent folder, every time you save a file within that directory structure the plugin will automatically apply the settings in the dot file and automate a few different things for you. Some of which are saving Unix-style line endings, adding end-of-file newline, removing whitespace, and adjusting your indent tab/space settings.


QUICK EXAMPLE

Install the EditorConfig plugin in Sublime using Package Control; then place a file named .editorconfig in a parent directory (even your home or the root if you like), with the following content:

[*]
end_of_line = lf

That's it. This setting will automatically apply Unix-style line endings whenever you save a file within that directory structure. You can do more cool stuff, ex. trim unwanted trailing white-spaces or add a trailing newline at the end of each file. For more detail, refer to the example file at https://github.com/sindresorhus/editorconfig-sublime, that is:

# editorconfig.org
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

The root = true line means that EditorConfig won't look for other .editorconfig files in the upper levels of the directory structure.

coccoinomane
  • 858
  • 8
  • 24
Nathan C.
  • 319
  • 3
  • 8
  • Insufficiently aggressive. I need the plugin to identify the chain of people who lead to the file having crlf and punch each of them in the face. And then save it with lf. – ArtOfWarfare Jun 02 '17 at 20:48
  • Thanks, this was my problem, i had installed this plugin and it was taking the wrong config from the .config file – Luke Robertson May 17 '18 at 08:08
1

to chnage line endings from LF to CRLF:

open Sublime and follow the steps:-

1 press Ctrl+shift+p then install package name line unify endings

then again press Ctrl+shift+p

2 in the blank input box type "Line unify ending "

3 Hit enter twice

Sublime may freeze for sometimes and as a result will change the line endings from LF to CRLF

1

The simplest way to modify all files of a project at once (batch) is through Line Endings Unify package:

  1. Ctrl+Shift+P type inst + choose Install Package.
  2. Type line end + choose Line Endings Unify.
  3. Once installed, Ctrl+Shift+P + type end + choose Line Endings Unify.
  4. OR (instead of 3.) copy:

    {
     "keys": ["ctrl+alt+l"],
     "command": "line_endings_unify"
    },
    

    to the User array (right pane, after the opening [) in Preferences -> KeyBindings + press Ctrl+Alt+L.

As mentioned in another answer:

  • The Carriage Return (CR) character (0x0D, \r) [...] Early Macintosh operating systems (OS-9 and earlier).

  • The Line Feed (LF) character (0x0A, \n) [...] UNIX based systems (Linux, Mac OSX)

  • The End of Line (EOL) sequence (0x0D 0x0A, \r\n) [...] (non-Unix: Windows, Symbian OS).

If you have node_modules, build or other auto-generated folders, delete them before running the package.

When you run the package:

  1. you are asked at the bottom to choose which file extensions to search through a comma separated list (type the only ones you need to speed up the replacements, e.g. js,jsx).
  2. then you are asked which Input line ending to use, e.g. if you need LF type \n.
  3. press ENTER and wait until you see an alert window with LineEndingsUnify Complete.
CPHPython
  • 12,379
  • 5
  • 59
  • 71