SublimeText: Change line endings of all files in a project - sublimetext3

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.

Related

How to show invisible line endings in Sublime Text 3?

I would like to display invisible line endings in Sublime Text 3, as for example in Notepad++. How can we do that?
This is useful because when the automatic word wrap mode is ON for example, because it becomes hard to distinguish soft line breaks from real ones.
The package RawLineEdit is pretty good for this. It toggles on-and-off a mode where you view and can edit line endings by selecting Raw Line Edit: Toggle from the Sublime ctrlshiftp command prompt.
This is good if you want to be able to flick line endings visibility on and off on the occasions when you want to see it (it's not the best option if you want line endings to be always visible).
It's also very useful for seeing (and editing) if you've ended up with crlf problems, with windows-style cr carriage return characters getting mixed up with the standard lf line end characters shown as ¬:
I have written a basic plugin to display line endings as an explicit character, inline. There's certainly some room for improvement but it does the job.
It is available here: https://github.com/sdive/sublime-text_managelineendings
The other suggestions found here didn't work for the character I was encountering (U+2028). I found that this plugin was able to show me where the character was occurring:
https://packagecontrol.io/packages/Highlight%20Dodgy%20Chars
ctrl + shift + p on editor
now, type toggle word wrap and select it

How to associate specific files (NOT file types) with syntax highlighting in Sublime text 3?

I have build_config (and other *_config files), GdbRun and build.txt files which are basically bash shell scripts.
How could I associate these files with shell syntax ? To place a pattern like
'if filename is *_config or GdbRun or build.txt' somewhere.
BufferScroll plugin can remember most of view settings for a particular file including syntax.
Just install BufferScroll and change the syntax manually using the status bar or the command palette and it'll remember it next time you open that file.

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.

Why would Vim add a new line at the end of a file?

I work with Wordpress a lot, and sometimes I changed Wordpress core files temporarily in order to understand what is going on, especially when debugging. Today I got a little surprise. When I was ready to commit my changes to my git repository, I noticed that git status was marking one of Wordpress files as not staged for commit. I remember I had reverted all the changes I did to that file before closing it, so I decided to use diff to see what had changed. I compared the file on my project with the file on the Wordpress copy that I keep in my downloads directory. It turns out the files differ at the very end. diff indicates that the there is a newline missing at the end of the original file:
1724c1724
< }
\ No newline at end of file
---
> }
I never even touched that line. The changes I made where somewhere in the middle of a large file. This leads me to think that vim added a newline character at the end of the file. Why would that happen?
All the answers I've seen here address the question "how could I prevent Vim from adding a newline character at the end of the file?", while the question was "Why would Vim add a new line at the end of a file?". My browser's search engine brought me here, and I didn't find the answer to that question.
It is related with how the POSIX standard defines a line (see Why should files end with a newline?). So, basically, a line is:
3.206 Line
A sequence of zero or more non- <newline> characters plus a terminating <newline> character.
And, therefore, they all need to end with a newline character. That's why Vim always adds a newline by default (because, according to POSIX, it should always be there).
It is not the only editor doing that. Gedit, the default text editor in GNOME, does the same exact thing.
Edit
Many other tools also expect that newline character. See for example:
How wc expects it.
GCC warns about it.
Also, you may be interested in: Vim show newline at the end of file.
Because vim is a text editor, it can sometimes "clean up" files for you. See http://vimhelp.appspot.com/vim_faq.txt.html#faq-5.4 for details on how to write without the ending newline, paraphrased below:
How do I write a file without the line feed (EOL) at the end of the file?
You can turn off the eol option and turn on the binary option to write a file without the EOL at the end of the file:
   :set binary
   :set noeol
   :w
Alternatively, you can use:
   :set noeol
   :w ++bin
Adding a newline is the default behavior for Vim. If you don't need it, then use this solution: VIM Disable Automatic Newline At End Of File
To disable, add this to your .vimrc
set fileformats+=dos
You can put the following line into your .vimrc
autocmd FileType php setlocal noeol binary
Which should do the trick, but actually your approach is somewhat wrong. First of all php won't mind that ending at all and secondly if you don't want to save your changes don't press u or worse manually try to recreate the state of the file, but just quit without saving q!. If you left the editor and saved for some reason, try git checkout <file>
3.206 Line
A sequence of zero or more non- characters plus a terminating character.
Interestingly, vim will allow you to open a new file, write the file, and the file will be zero bytes. If you open a new file and append a line using o then write the file it will be two characters long. If you open said file back up and delete the second line dd and write the file it will be one byte long. Open the file back up and delete the only line remaining and write the file it will be zero bytes. So vim will let you write a zero byte file only as long as it is completely empty. Seems to defy the posix definition above. I guess...

Windows/Unix line ending issues?

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...

Resources