Change line ending character on Mac - macos

I got a new Mac and hammered out some code in Vim. When I viewed the git diff the code I changed had a "^M" after each line. It seems like my new mac is using windows line ending character for some reason. This is not an issue with my text editor because this occurs in both Sublime Text and Vim.
I cannot find any way to edit this in my system preferences and I am not sure where to go from here. It seems like every thread on Stack Overflow addressing this issue either simply offers an explanation of how line endings work or suggest using some third-party service to convert the line endings in a file. I want to know how to change the line endings my Mac inserts.

Vim attempts to use the existing line endings for files. To set the line endings to Unix style explicitly, use
:set fileformat=unix
:w
See :help fileformat for further information. (Note that the Vim help may refer to "mac" style line endings, which refers to Classic Mac OS before 10.x. Mac OS X has always used Unix style line endings.)

Related

newline in mac command file created in windows

I am creating a command file in windows using JavaScript activexobject.
This file will run on Mac after double click.
I am writing the file as
script.write("#!/bin/bash\r");
script.write('cd "$(dirname "$0")"\r');
The additional \r is for carriage return line ending in Mac.
But this doesn't work. The command doesn't execute just starts and over. No cd happens.
I am sure that this problem is related to line endings because when I edit the line ending (by deleting the newline and again add newline using return key) after opening the command file in Mac with TextEdit, it works.
How can I solve this in write method?
You should try writing '\n' instead of '\r'. Carriage returns were used in pre OSX machines only.
If you wanna visually see the line endings you could open up vim on the Mac and type :set list. All new lines will appear like $ and carriage returns as ^M. You should only be seeing $ if this is an OSX machine.

Perl - edit in Windows but run directly on Unix shell?

I write my Perl code in Textpad (which I believe is only avai in Windows). I run it on Linux cmd prompt by calling the Perl interpreter explicitly, e.g. "perl script.pl". I was wondering if it's possible to run it simply as in "./script.pl". When I add the shebang in Windows, the Linux prompt complains "command not found", but it works fine if I call it with Perl, and also works fine after I dos2unix the script, so the issue seems to be the shebang not being parsed correctly. Any suggestions? Why does the rest of the Windows-formatted code work but not the shebang?
Your problem is that Windows prefers a different line ending convention (CRLF, or \r\n) than other operating systems (LF, or \n). Your editor is creating files with \r\n line endings by default.
The shebang is parsed by the operating system, which is not as forgiving as Perl about the stray \r at the end of the command. It tries to run /usr/bin/perl\r, which doesn't exist.
Your text editor should be able to save the script with Unix line-endings. This won't cause problems with using it on Windows, though a few Windows text editors (including Notepad) won't recognize the line endings properly. This will make it work properly on Linux.
The first line of your file is
#!/usr/bin/perl<CR><LF>
<LF> is the line terminator, so the OS tries to launch /usr/bin/perl<CR>. There is no such program. dos2unix changes the first line to
#!/usr/bin/perl<LF>
<LF> is the line terminator, so the OS tries to launch /usr/bin/perl and succeeds.

Emacs ^m end of line encoding

I was wodnering why my emacs started putting ^m at the end of every line, i know how to "fix it" but i was wondering what caused it to happen. I have been using emacs in windows for some time now and it started more recently. Does anyone know why this starts?
Most likely the reason is that the buffer is using '...-unix' coding system when the file contains Windows carriage-return/linefeed line ends. The mode line will show you the coding system in use. See 'Coding Systems' in the Emacs Manual.
Ctrl-h C
will display the coding system currently in use.
I see this happening when the file contains both \r\n and \n\r line endings. That is, someone has written broken Windows line endings to the file (\n\r).
Go through your file and look for lines that start with ^M. This implies that the carriage return was after the newline character on the previous line, which is incorrect.
If that is the case, go back to the program that generated the file and make sure that it's writing correct Windows line endings.

Changing Line Endings on Dreamweaver for the MAC (so that files work on linux)

I am trying to change the line endings in dreamweaver for the mac so that the line endings match linux standards.
Go to Edit | Preferences | Code Format and change the line break type to LF for Unix-style line breaks.

Vim: Mac vs. Linux ^M Problem

I use Vim and GIT to develop my project on a Linux/Ubuntu machine. In the past, I used to use Windows, which means line ends were DOS format. Now after moving to Linux, I always see ^M confusing/annoying characters at the end of the line. Some people here:
How to convert the ^M linebreak to 'normal' linebreak in a file opened in vim?
suggested that I should make a find/replace or use tools like dos2unix. These are good suggestions, but with a version-controlled project it means I have to add unnecessary commits for the files after change, and it is a big task anyway that is not worth spending time on it. So:
Is there anyway to make Vim tolerant for this? That is, if the file is DOS line-ended, it keeps it like this, and use its formatting for new newlines, and so on.
I have added this line to my .vimrc file.
set fileformats=unix,mac,dos
If you don't know what a vimrc file is, it's simply the file that configure (and allows you to customize) your vim. By default vim will be looking for a file called .vimrc (notice the dot at the beginning) in the user's home folder. If it cannot find it, it will source it from /etc/vimrc.
Yes, you can set the following in vim to say it's a dos format file:
:set ff=dos
And, likewise:
:set ff=unix
The best answer I found is #Keith answer in the comments:
"With subversion you can set a property svn:eol-style=native that will cause it to convert line endings to.. native type for all platforms automatically. I don't know if git has that feature, but you might look into that first. It probably does."
And these links might be useful link is how to set:
http://help.github.com/dealing-with-lineendings/
http://git-scm.com/docs/gitattributes

Resources