WSL Line Endings with NetBeans

W

While the Windows Subsystem for Linux provides a suitable environment for development, the one fly-in-the-ointment is still the incompatibility of the respective file systems. Microsoft has emphatically warned that writing to the Linux file system from Windows has the potential to corrupt the entire Linux environment. As such, they recommend that files be created and modified in Windows, and then accessed from Linux via /mnt/<drive-letter>/path. This is largely workable, but still does not address the differences in the operating systems’ respective line endings.

This article addresses how I configured my WSL Ubuntu based LAMP installation for easy use with the Windows file system, while handling the line-ending problem with a simple NetBeans plugin.

First, I created an html folder in my Windows C:\Users\<username> directory. Next I used the commands below to remove Apache’s webroot directory and then to recreate it as a symlink to the Windows directory created above.

sudo rmdir /var/www/html
sudo ln -s /mnt/c/users/<username>/html

This solution provides the added benefit that it is no longer necessary to reconfigure Apache to point to a mounted webroot directory. Now I simply save a file to the C:\Users\<username>\html directory in Windows and it is automatically served by Apache.

What remains, of course, is the line ending problem. I found an interesting article that explains how NetBeans deals with disparate line endings. The upshot is that it uses whatever line ending terminates the first line of any file it opens. Therefore if you open a file created in Linux, it will save it using LF, regardless of the fact that you are running NetBeans in a Windows environment. Newly created files, however, will still use the system line ending, which in our case is the dreaded CRLF. Conveniently, there is a easy-to-install NetBeans plugin called change-line-endings-on-save.

  1. Open NetBeans’ Tools > Plugins menu.
  2. Install Change Line Endings on Save from the Available Plugins tab.
  3. Open NetBeans’ Tools > Options menu.
  4. Select Editor at the top, then the newly installed Line Endings menu.
  5. Choose Unix(LF) and close the window.

Line endings can also be set on a per-project basis.

That’s it! You now have a simple solution for using Windows to create and modify files for your WSL-based development environment. Even better, since the line endings are correctly stored in Linux, you should have no need to convert back and forth in Git.

About the author

Chris Peterson
Chris Peterson

As a Web Application Developer & Elephant Trainer I have been putting the PHP mascot to work for more than a decade. I specialize in back-end development and use the LAMP stack to craft software that frees human beings to spend their time on more productive and rewarding things.

Add comment

Time limit is exhausted. Please reload the CAPTCHA.

Chris Peterson By Chris Peterson

Chris Peterson

Chris Peterson

As a Web Application Developer & Elephant Trainer I have been putting the PHP mascot to work for more than a decade. I specialize in back-end development and use the LAMP stack to craft software that frees human beings to spend their time on more productive and rewarding things.

Recent Posts

Recent Comments

Archives