Windows/Unix line ending issues? - vim

I have a couple files that were recently edited on windows and via Cpanel's file editor and now show up double spaced (as in an extra line CR/LF between each line). Vim is telling me (via :set ff?) the file format is unix (and I'm working on a Mac). If I show special characters via :set list all the lines just end in $. I tried setting the format via :e ++ff=mac which appears to remove all line breaks in the currently edited document and when I write the file and re-open it's back to being double spaced. I also tried searching and replacing ^M and various \r\n combinations. I know I'm missing something simple but can someone shed some light on what is going on? Is this even a line ending issue?

It appears to be a line ending issue.
The Vim wiki has this to say on the subject:
http://vim.wikia.com/wiki/File_format#Terminator_after_last_line
I, however, for expediency, when faced with a line ending problem, use BBEdit on my Mac to change them to Unix (I share, on the LAN, my eight Linux boxes with a Macbook Pro so I use a directory in Dropbox to transfer files across. scp will do the same job).
Unless you have a copy of BBEdit lying about, you can download Barebones's free Text Wrangler & it'll do the same job. Only works on a Mac obviously...

Related

Vim don't show last empty line

I have written some c code, and the end of my output is '\n', when I check the output text file in vim, I cannot find the last empty line, however, when I open it with another text viewer, I can find the last empty line. How can I configure my vim to show the empty line?
The way Vim shows \n / 0x0a at the end of the file is that it opens the file without complaining about [noeol] when :editing the file (in a kind of "reverse logic" from what you expect). Vim's (and Unix) philosophy is that the trailing newline should be there. This can be confusing when one is used to other editors or predominantly works on MS Windows.
There's a lot of discussion and questions about this (e.g. here); as this is unlikely to change, get used to it.

SublimeText: Change line endings of all files in a project

Sublime Text 3 has a very useful feature to change Windows to Unix line endings on a file per file basis under View > Line Endings. Is it possible to do this for a whole project and/or directory at once?
Thanks
The Line​Endings plugin / package is small, but works like a charm Visit it's GitHub repository to get a few details, because on its Package Control page there is no description.
It not only works, but also gives you an additional functionality -- you can change not only line endings, but also indentation method of all open files in one step.
After installation:
1-Open all files, in which you want to convert / change line endings and/or indentation method.
2-Press Ctrl+Shift+P, to bring Command Pallete and start typing lin to locate Line Ending to all views: Windows (for example).
3-Select it, hit Enter, and... you're done!
4-Repeat step 2, but type spaces to locate Indentation: Convert to all views to Spaces or tabs to locate Indentation: Convert to all views to Tabs to change indentation method of all open files.

Vim on Windows started showing ^M characters

I am on Windows 7 running PowerShell version 2.0.
I used the following PowerShell code (from this answer: https://stackoverflow.com/a/65148/360840) to modify my PowerShell profile:
"`nNew-Alias which get-command" | add-content $profile
As expected, it appended the line in question to my profile BUT afterwards, my Vim sessions started showing ^M characters at the end of each line. Very strange! Any clue on what caused it and how to fix it? I tried set fileformat=dos in Vim but it did nothing.
Here's my Vim version information:
(Not sure how to make the picture larger - this is not its normal size.)
There are some options to solve the problem in vim-faq 14.13. I am seeing a lot of ^M symbols in my file. I tried setting the 'fileformat' option to 'dos' and then 'unix' and then 'mac'. None of these helped. How can I hide these symbols?
It is possible that your file was in unix file format (all lines ending in <CR>), and you appended lines with dos file format (lines ending in <CR><NL>, what makes Vim confused.
So, I ran into this issue, and the solution was slightly different. I've checked my file format, and it's unix, you can check yours with
:set fileformat?
And it displays all of the ^M characters I don't want... The interesting thing, is that in the VIM manual as pointed to above at: http://vimhelp.appspot.com/vim_faq.txt.html#faq-14.13 states that the ^M line ending is from the DOS file format and that "If the file has some lines ending with and some lines ending with followed by a , then the fileformat is set to 'unix'."
So, given this it would seem that you would want to remove all of the ^M endings, and keep the endings, but removing the endings with :%s/\r$// immediately resolved the issue for me, and the file format still says 'unix'.
That's what worked for me. As a side-note, in order to provide a more google-able reference, I would like to note that I ran into this while using LocomotiveCMS. I was pulling my CSS file back down to work on locally, using Wagon. When I got it back it was full of ^M characters, the file still works fine, it's just ugly.
The problem was that the file became corrupted. In such a case, Vim starts displaying ^M characters to alert you to the problem. The fix was to manually remove these characters (%s/^M//g).
Still not clear to me, because these characters are SUPPOSED TO BE in the file as Windows file format uses them. But at least the problem was solved.
EDIT: Please see the exchange of comments w/ dash-tom-bang, which provides clarification.

Komodo IDE FTP (ASCII, binary) end-of-line characters

I've some problem when working on remote files (perl scripts) with Komodo IDE. There is (as far as I know) no way to change ftp transfer mode from binary to ASCII, which result in "^M" character at the end of every line. My setup is Linux server, and Windows client. Is there any way to solve this issue without nessecity of correcting saved file on Linux every time. This behaviour disqualify Komodo IDE, which was my favourite IDE until now.
The "^M" you observe has nothing to do with your file being ASCII, but line ending format (carriage return and line feed characters.)
I have not verified this, but here's a link showing how to save files in Komodo using a different line ending method. Saving files in DOS mode is not needed anymore, since most editors recognize UNIX file format nowadays.
Add switch -w to your Perl shebang.

Could someone explain line endings?

The current project I'm working requires me to follow certain procedures to eliminate whitespace in my code. Apparently this has got something to do with line endings since one requirement explicitly tells me to "end all lines with a Unix line ending (\n)".
I code in VIM from the terminal, and I press enter for a new line to write on. Am I missing something here?
What is the reason to keep the code clean from trailing whitespace and using specific types of line breaks?
On a side note, what standard VI/VIM settings do you guys use to adhere to common coding standards?
Sincerely,
Why
Different operating systems have different line break conventions. Unix-like systems prefer \n (LF); Windows prefers \r\n (CR LF); pre-OSX Mac OS used \r (CR). Maintaining one convention across a project is usually a good idea.
As for trailing whitespace, AFAIK it's just sloppy (may indicate "quick and dirty" reformatting). In some environments trailing whitespace might also be significant.
Perhaps not everyone on the project codes in VIM, they might be using a windows based IDE which would insert \r\n for a new line.
They would have to ensure that their line-endings are correct before committing code, whereas you shouldn't have this problem as vim will use \n as its natural line ending.
To enforce this in vim, you can use the fileformat option. Setting it to unix will make your newlines use \n.

Resources