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

50,692

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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.
Share:
50,692

Related videos on Youtube

Achraf JEDAY
Author by

Achraf JEDAY

Senior web developer (Full stack) Languages: PHP, Javascript, HTML and CSS. Frameworks: Drupal, Symfony.

Updated on September 18, 2022

Comments

  • Achraf JEDAY
    Achraf JEDAY over 1 year

    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.

  • Luis Cabrera Benito
    Luis Cabrera Benito almost 6 years
    This works specially when you are transfering files from windows to a server because in the server is harder to edit the file. Thanks
  • AnnanFay
    AnnanFay about 5 years
    The repository has been deleted?
  • lucas
    lucas about 4 years
    This should now be the accepted answer
  • André M. Faria
    André M. Faria over 3 years
    This answer remembered me of this tool, thank you, but unfortunately OP edited saying he want to modify line endings in sublime.
  • jmc
    jmc about 3 years
    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'.