How to stop Visual Studio from putting extra newline at the end of a file on save - visual-studio

I'm using Visual Studio 2019, and when I save a file (a cs file for example) VS will put an extra newline at the of the file.
I need to stop this behavior, but what I have searched on the internet so far, was about visual studio code or editors other than Visual Studio.
What is the philosophy of putting an extra newline at the end of file?

According to this link, I need to make Advanced Save Options menu visible, and then click it and change line endings dropdown value from Current Settings to Windows (CR LF).

Related

Can you make Visual Studio use tabs instead of spaces for new template files?

I've modified my copy of Visual Studio to keep tabs (instead of spaces) in all my files via Tools => Options => Text Editor => All Languages => Tabs. However, when I create a new project or file, any boilerplate code that Visual Studio includes is always padded with spaces, not tabs (and in cases like web.config, not the specified number of spaces either).
Is there a way to force these all to use tabs automatically when created?
Note: I'm using Visual Studio 2015 Pro, but I suspect other people would be interested in this for other versions as well.
I don't think you can without modifying the template contents, but if you invoke Format Document (Edit => Advanced => Format Document or the keybinding it shows in the menu) after creating the file it should replace them all according to your settings.
I'm using the Productivity Power Tools 2017 with "Fixed Mixed Tabs". As soon as I add some own code with a tab indent and save the file I get a warning about "mixed tabs". I then can replace the spaces with a click on the "Tabify" button.
Not perfect, but pretty close.

MSVC 2012 - Shortcut to duplicate line

What is the key for duplicating a line in Microsoft Visual Studio 2012?
(in Notepad++ it is ctrl+D )
Check this extension:
Duplicate Selection extension : http://visualstudiogallery.msdn.microsoft.com/830a6482-3b8f-41a8-97b5-b9c581e5ad8b
Duplicate the line without touching the clipboard (as Notepad++ does).
Just to make it absolutely clear
ctrl-C at the start of a line copies the whole line without your needing to select it
source Shortcut for duplicating a line of code in Visual Studio 11
Here's a list of keyboard shortcuts, but don't see a duplicate on it. Ctrl+C/Ctrl+V may be your best bet or see if there's something you can define under Tools>Options>Keyboard.

Show space, tab, CRLF characters in editor of Visual Studio

Where are the settings to show a space, tab, paragraph, CRLF, etc. (extended) characters?
Edit > Advanced > View White Space. The keyboard shortcut is CTRL+R, CTRL+W. The command is called Edit.ViewWhiteSpace.
It works in all Visual Studio versions at least since Visual Studio 2010, the current one being Visual Studio 2019 (at time of writing). In Visual Studio 2013, you can also use CTRL+E, S or CTRL+E, CTRL+S.
By default, end of line markers are not visualized. This functionality is provided by the End of the Line extension.
Edit -> Advanced -> View White Space or Ctrl+R,Ctrl+W for Visual Studio 2019
Display white space characters
Menu:
You can toggle the visibility of the white space characters from the menu:
Edit > Advanced > View White Space.
Button:
If you want to add the button to a toolbar, it is called Toggle Visual Space in the command category "Edit".
The actual command name is: Edit.ViewWhiteSpace.
Keyboard Shortcut:
In Visual Studio 2015, 2017 and 2019 the default keyboard shortcut still is CTRL+R, CTRL+W
Type one after the other.
All default shortcuts
End-of-line characters
Extension:
There is a minimal extension adding the displaying of end-of-line characters (LF and CR) to the visual white space mode, as you would expect. Additionally it supplies buttons and short-cuts to modify all line-endings in a document, or a selection.
VisualStudio gallery: End of the Line
Note: Since Visual Studio 2017 there is no option in the File-menu called Advanced Save Options. Changing the encoding and line-endings for a file can be done using Save File As ... and clicking the down-arrow on the right side of the save-button. This shows the option Save with Encoding. You'll be asked permission to overwrite the current file.
My problem was hitting CTRL+F and space
This marked all spaces brown. Spent 10 minutes to "turn it off" :P
The correct shortcut is CTRL-R-W like you don't have to release CTRL button while pressing W. This worked for me in VS 2015
For those who are looking for a button toggle:
The name of this command is View white space in GUI menu (Edit -> Advanced -> View white space).
The name of this command in the Add command popup is Toggle Visual Space.
If you use Visual Studio Code => View => Render Whitespace
That's it!
For me this setting was on, but didn't work. I had to turn it off and on again and than it worked! So this is a bug in Visual Studio Code!
If you use Visual Studio 2022 you can change the display options for whitespace etc. in Tools => Options => Text Editor => General and select "View whitespace"
The shortcut didn't work for me in Visual Studio 2015, also it was not in the edit menu.
Download and install the Productivity Power Tools for VS2015 and than you can find these options in the edit > advanced menu.
To see the CRLF you can try this extension: End of the Line
It works for VS2012+
For Visual Studio for mac, you can find it under Visual Studio -> Preferences -> Text Editor -> Markers and Rulers -> Show invisible characters
Please note you may need to restart Visual Studio for the changes to take effect
For completeness since I haven't seen it mentioned here, it is also in Options->Text Editor->General->View Whitespace.
In the actual version this Option ist under Editor: Render Whitespace

Change EOL Character in Visual Studio

I’m sorry if this is a trivial question for you.
Is there any way to change the End of Line Character (EOL) character in Visual Studio 2010? I want to change it from Windows (‘\r\n’) format to UNIX (‘\n’) format.
Thanks
File / Save As ...
Save with Encoding... (press the arrow next to the Save button to see this option)
Choose appropriate EOL character from the Line endings drop-down list
Click somewhere in the code page. Go to File -> Advanced Save Options -> use the Line Endings Drop box to select the line ending you want on ALL of your output files (when using \n for EOL). This solution is written for Visual Studio 2008.
For doing this globally, Strip'em is an add-in for Visual Studio that will automatically convert the line-ending when saving a file. Works for VS 2008/2010/2012.

Configure Visual Studio to use UNIX line endings

We would like to use Visual Studio 2005 to work on a local copy of an SVN repository. This local copy has been checked out by Mac OS X (and updates and commits will only be made under Mac OS X, so no problem there), and as a consequence the line endings are UNIX-style.
We fear that Visual Studio will introduce Windows-style line endings. Is it possible to force Visual Studio to use UNIX line endings?
Warning: This solution no longer works for Visual Studio 2017 and later. Instead, both of the answers by jcox and Munther Jaber are needed. I have combined them into one answer.
As OP states "File > Advanced Save Options", select Unix Line Endings.
This will only affect new files that are created. Fixing any that were previously created can be done file-by-file or you can search for tools that will fix on-bulk.
Here are some options available for Visual Studio Community 2017
"File > Advanced Save Options" has been removed by microsoft due to "uncommon use". Whatever that means. https://developercommunity.visualstudio.com/content/problem/8290/file-advanced-save-options-option-is-missed.html
You can add it back by going to "Tools>Customize", then "Commands" tab, select the drop down next to "Menu Bar" select "File" then "Add Command">File>Advanced Save Options..". You can then reorder it in the file menu by using "move down".
I don't know if you will have to then set the advanced save options for each and every file, but it might prevent the issue I was having where my Visual Studio kept adding CL RF line endings into my files that were uniformly LF.
But I took it one step further and I added an extension called "Line Endings Unifier" by going to "Tools>Extensions and Updates>Online" and then searching for "line endings" in the search bar to the right. I will use this to automatically force all of my scripts to save with uniform line endings of my choice, but you can do more with it.
https://marketplace.visualstudio.com/items?itemName=JakubBielawa.LineEndingsUnifier
strip'em is another solution that does something similar to Line Endings Unifier. http://www.grebulon.com/software/stripem.php
I am not sure how they differ or the advantages/disadvantages of either. I'm mainly using Line Endings Unifier just because it was in the Visual Studio Marketplace. I think I've used all of these methods in the past, but my memory is fuzzy.
So I found this problem and the answers very confusing, especially since I primarily develop in Linux and PhpStorm and simply never have these issues.
I found that for Visual Studio 2019, only Munther Jaber's solution worked when combined with #jcox's .editorconfig changes for new files.
However, being unfamiliar with Visual Studio itself, it took me a LONG TIME to find out where the line ending box is...
SO I made a video of how to configure this.
https://youtu.be/YfN3igHXTPo
Update: I learned how to make animated GIFs via FFMPEG just so I could embed the video here!
VS2017 supports .editorconfig files, which can be checked in along with your project. This is the relevant command for setting Unix line endings:
end_of_line = lf
"File > Advanced Save Options", select Unix Line Endings works per file based.
Alternatively You can use Strip'em Add-in for Visual Studio
This Add-in converts the text format of a file when it is saved in Visual Studio.
More info here: http://www.grebulon.com/software/stripem.php
There are some VS extension that are useful to solve this problem
I will recommend Trim line ends on save. This extension is free and open source. There are similar extensions that may work for you, just go to TOOLS | Extensions and Updates and search online for "line end"
For Visual Studio 2019 for Mac
Go to Project> Solution Options> Source Code> Code Formatting> Text File
In the Line Terminations drop box select Unix/Mac.
Image
The LF is clickable and you choose either LF or CRLF.
It is possible to force line endings in Visual Studio 2017 for Mac
Go to Tools > Add Custom Tool....
In the dialog box that appears scroll the left menu down to the Text Editor section and select General.
In the first option, Line ending conversion, change Leave line endings as is to Always convert line endings.
In the latest version on Windows you go to
File > Preferences > Settings > Text Editor > Files > Eol

Resources