Visual Studio - "{ }" settings - visual-studio

Seriously, I don't know what to google. Here's the thing, I like this Java-like code writing:
if (condition == true) {
doSomeStuff();
}
But VisualStudio "helps" me with its own "style", which I don't like and I am unable to change (after rather big time of desperate checking all settings :/)
if (condition == true)
{
DoStuff();
}
I obviously want the "{" char to be in same line where condition is ...
I am using MS Visual Studio 2010 professional.

Go to Tools > Options > Text Editor > [Language, i.e.: C#] > Code Style > Formatting > New Lines
This is where you can set your new line options for braces.
See the image below for more clarification.

Tools -> Options ...
alt text http://img6.imageshack.us/img6/158/capturejo.png

Text Editor > C# > Formatting > New Lines > New line options for braces
Uncheck Place open brace on new line for control blocks.
Probably want to uncheck put else on newline as well.

This is for Visual Stuio 2008, so I'm not sure that it's exactly the same in VS 2010:
Go to the Tools menu and open Options. Make sure that the Show all settings checkbox is checked. Open the Text Editor node and then the node for the language you want to change the setting for. Open the Formatting node and select the New Lines node. Here you can change when new lines are added to the code.
After installing Visual Studio I go in there and uncheck every option in the New Lines section.

Worth knowing is that Steve Horn's (and others) answer works even for other languages as they seem to appear to not have the same settings-layout. I did the changes as described in his post and it worked for my C++ projects as well.

Related

How to turn off auto indent in Visual Studio 2019? (not VISUAL STUDIO CODE)

I usually use comma , to set the same variable type like the gif above.
But a problem is when I hit Enter after write second variable, the VS breaks the line automatically.
As a formal Atom user, it's a bit tedious because in Atom it doesn't break the line whatever the indent number is. (My current indent number is 2.)
I changed and tested it all of the options inTools > Options > Text Editor > Basic > Tabs, but none of these turn off the breaking line. Even disabling pretty listening won't work.
How do I turn off this feature?
Judging by the glimpse of Intellisense and the fact that Enter auto-formats, it looks like you're editing a JavaScript file.
To disable auto-formatting after hitting Enter, go to Tools > Options > Text Editor > JavaScript/TypeScript > Formatting > General and toggle Format completed line on Enter to unchecked:
The accepted answer doesn't work in Visual Studio 2019 with Visual Basic. These options on the image aren't even shown in the Options for Basic.
However, when the Pretty listing option is turned off, this behavior ceases.

Visual Studio code style preference csharp_prefer_braces not working

In Visual Studio 2017 I go to Tools > Options > Text Editor > C# > Code Style, and toggle the option "Prefer braces" from yes to no and back.
It does nothing. The preview below the options always has braces regardless of the setting.
Even an explicit .editorconfig file on csharp_prefer_braces = false:none does not change the behavior.
Is this a bug? Or is some other option overriding?
The property after the ":" will decide how this is treated in VS Code. So using none will mean that VSCode doesn't actually enforce this.
The MS docs here describe "none" as:
"None": Do not show anything to the user when this rule is violated. Code generation features generate code in this style, however. Rules with none severity never appear in the Quick Actions and Refactorings menu. In most cases, this is considered "disabled" or "ignored".
Perhaps you could try using csharp_prefer_braces = false:error instead?

Visual Studio 2013 auto indent

In Visual Studio 2013, under Tools > Options > Text Editor > File Extension, I have set three file extensions, namely .cginc, .compute, and .shader (these are Unity3D shader files) to use Microsoft Visual C++ for auto formatting.
Sometimes auto indent (for curly braces) works, and sometimes it doesn't...I can't find any rhyme or reason for this. When it doesn't work, it starts every new line flush to the left.
I Google around every couple weeks and can't find an answer.
Does anyone know any setting to make auto indent work consistently?
Try this:
Tools -> Options -> Select the language of your choice (Expand the menu) -> Select Formatting (Expand this sub menu) -> Select Indendation
Once you have selected Indentation, on the right are options displayed - Check/Tick the option: Indent Braces.
Note: You may also format the entire document/file using the shortcut Ctrl+K+D

Visual Studio 2010 - Curly Braces auto-align has changed, how do I fix this?

Update: Since I see this is one of my most looked at questions, now a few years later I should point out what I found to be the most common cause of this problem: bad syntax elsewhere in your code. 100% of the time I've had this happen, it was because I'd forgotten a curly brace for another block of code, or I had a dangling if, or an earlier line of code I didn't finish. Check for IDE errors first for something like ") or } expected"
Original Question:
I feel like this is an all time dumb question to ask, but I have no idea how to fix this and google is turning up nothing.
In visual studio, when I type:
try {
}
VS would automatically reformat it to
try
{
}
But now it's just leaving it at the first one. I assume I accidentally hit a hotkey or something. Help me fix this please?
Tools->Options
Click Text Editor -> Whatever language -> Formatting
Tools->Options->Text Editor->C# (or whatever language)->Formatting->New Lines
Select the options you want.
If you have Power Commands installed you can then go to the Tools->Options->Power Commands and make sure "Format Document on Save" is selected.
Whenever you save the edited file the Curly Braces will be auto aligned.
In addition to the default Visual Studio settings mentioned, for ReSharper users, this is configured via:
ReSharper [menu] -> Options
In the Options dialog, navigate to Environment -> Editor -> Editor Behavior -> "Auto-format on closing brace". See image:
9 times out of 10 this problem is caused by bad code elsewhere on my document. Either I'm missing a ; on a line, or perhaps a closing }. Visual Studio is unable to figure out what I'm trying to write in code and thus it's unable to format. Check for compiler errors, fix them, and then press Ctrl+K then Ctrl + D to make Visual Studio reformat the current document (your hotkeys may vary, depending on the version of Visual Studio and your settings).

Disabling copy of empty text in Visual Studio

I have somehow misconfigured fingers. This leads to a very annoying situation.
I select a block of text to copy;
I move the cursor the place where I want to paste the code;
I accidentally press Ctrl+C again instead of Ctrl+V;
My block of copied text is replaced by an empty block;
I have to go back and do it all over again. Grrrrr.
Is there any way to disable this behavior, that is to disable copy of empty blocks of text in Visual Studio 2005+?
It's not copying an empty block, it's copying the blank line. You can change this setting in Tools > Options > Text Editor > All Languages > 'Apply Cut or Copy Commands to blank lines when there is no selection'
Press CTRL+SHIFT+V twice.
I'm using Visual Studio 2008 (but I believe this answer applies to Visual Studio 2005).
Select Tools -> Options.
Navigate to the "Text Editor" node and expand it.
Expand "All Languages" (or whatever language you want to apply this to) and check the "Apply Cut or Copy commands to blank lines when there is no selection".
The option that saved my sanity is found in Tools - Options - Text Editor - All Languages - General. There's a checkbox Apply Cut or Copy commands to blank lines when there is no selection. Unchecking this allowed me to hit Ctrl+C all i want on a blank line without losing the content on my clipboard.
Source
Go to Tools > Options > Text Editor > All Languages > General
The option on that page is "Apply Cut or Copy commands to blank lines when there is no selection"
For some reason that option didn't work for me (VS2010)
The answer mentioned here where you assign Ctrl+C to the macro worked however Disabling single line copy in Visual Studio

Resources