30

When I do a git diff in my branch I see that line endings have the characters ^M:

^M Line Endings

I want to replace these with Unix line endings before committing my changes.

I am using Ubuntu 14.04, git and sublime text 3.

I am looking for a way to change line endings in sublime text 3 not using other utilities or commands.

0

5 Answers 5

26

This can be done without a plugin via the menu at the top or though the statusbar (but you need to enable the feature).

To change it through the menu at the top click View -> Line Endings -> Select desired line ending type.

To enable this through the status bar: Click Preferences -> Settings Then add "show_line_endings": true, somewhere between the {} brackets on the right-hand side. Finally, save your changes.

This enables a small section on the right of the status bar that shows up at the bottom. It tells you the current EOL encoding and by clicking the box and selecting a different type it changes the files EOL character for you.

Another helpful option is file encoding which can be enabled in the statusbar as well by following the same setups above but also adding "show_encoding": true, between the {} marks.

2
  • This works specially when you are transfering files from windows to a server because in the server is harder to edit the file. Thanks Aug 21, 2018 at 5:22
  • This should now be the accepted answer
    – lucas
    Apr 6, 2020 at 13:51
17

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

1
  • This answer remembered me of this tool, thank you, but unfortunately OP edited saying he want to modify line endings in sublime. Feb 8, 2021 at 17:57
8

^M is \r (Control-M, or carriage return).

This is likely because the user who committed the file was using windows, which uses \r\n for its line endings. Linux uses only \n, hence you see the ^M at the end of the line.

You probably want to do one of the following:

  • 'Fix' the file to have \n line endings (using a tool such as dos2unix) and commit. In which case you'll be fighting with the other user unless you can get them to do the below.
  • Configure both of your git installations to handle line endings correctly for each environment - e.g: https://stackoverflow.com/questions/10418975/how-to-change-line-ending-settings
  • Configure Sublime to open the file with 'Windows line endings'
6

To whom may not want to install additional plugin to remove ^M new lines,

  1. In sublime text setting, add "trim_trailing_white_space_on_save": true, to auto remove space when saving. (This step might be optional)

  2. On the file you are opening, click right bottom corner of sublime text. Change Windows to Unix and save.

  1. ^M is gone. Enjoy.
1
  • Be aware that the line endings are not show in the status bar by default - you need to set the preference "show_line_endings" to 'true'.
    – jmc
    Apr 13, 2021 at 8:31
3

I found a sublime text plugin that converts line endings in all open files to Unix/Windows/Mac OS 9.

Installation

  1. Open Sublime and go to Preferences > Browse Packages. This will open a window.
  2. Download or clone the contents of this repository to a folder named exactly as the package name into the Packages/ folder you opened in the previous step:

/home/YOUR_USERNAME/.config/sublime-text-3/Packages

Usage

  1. Go back to sublime and open all the files that you want to convert.
  2. Once all of the files are open hit CTRL + SHIFT + P, and search and select “Line Ending to all view: Unix”.
  3. Hit CTRL + ALT + S to save all of the open files.
1
  • The repository has been deleted?
    – AnnanFay
    Apr 11, 2019 at 0:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .