How can I remove the ^M from my file in sublime text 3?

17

6

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.

Thanks for your help.

EDIT:

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

Achraf JEDAY

Posted 2017-06-09T12:13:00.623

Reputation: 543

Answers

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.

Achraf JEDAY

Posted 2017-06-09T12:13:00.623

Reputation: 543

The repository has been deleted? – AnnanFay – 2019-04-11T00:18:38.723

11

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

Jan Čejka

Posted 2017-06-09T12:13:00.623

Reputation: 111

11

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.

K.B.

Posted 2017-06-09T12:13:00.623

Reputation: 211

This works specially when you are transfering files from windows to a server because in the server is harder to edit the file. Thanks – Luis Cabrera Benito – 2018-08-21T05:22:13.567

7

^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'

Attie

Posted 2017-06-09T12:13:00.623

Reputation: 14 841

3

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.

harrrrrrry

Posted 2017-06-09T12:13:00.623

Reputation: 131