"Always-indent" style for visual studio express - c#

Is there a way to have Visual Studio (2012 Express) auto-indent like it is possible in XCode or Emacs? Saying auto-indent I mean that the IDE indents to the correct level when pressing TAB no matter what position in the line the cursor is in.
I know I can press Ctrl+K, Ctrl+F or Ctrl+K,D to indent a document or selection, but this is kind of cumbersome. Writing code and just pressing TAB to auto-indent the current line is just much faster. Is there a way to do this in VS?

Go to
Tools -> Options
choose then
Text Editor -> All Languages -> Tabs
There you can specifiy:
Tab size
Indent size
Insert spaces (instead of a tab)

Related

Visual Studio 2022 turn off auto indenting

So i upgraded to VS 2022. and anytime i add a semicolon. the whole code block gets backtabbed. does anyone know how to turn this off? getting tired of hitting ctrl z repeatedly..
Before.
After
Go to VS2022 Tools - Options Menu
Type indent in search
Look for Indentation under formatting option which appears under
Text Editor - C# - Code Style - Formatting Indentation
check / uncheck boxes to see how your code will look and save when happy
After consolidating code blocks to one block on the razor page... this seems to have corrected the issue. the indenting I believe may have been caused by some bug where the indenting fails due to attempting to address other formatted code. this is not really a solid answer but i did find that moving things around prevented this from occuring and this was nothing to do with any indenting settings within the tools text editor options.
I'm not sure if you ever found the correct answer to your question, but I was having the same problem, and reading the other two answers led me to figuring it out. The problem is with the adaptive formatting option being turned on. To turn it off, go to
Tools -> Options -> Text Editor -> Advanced
You should see
Uncheck 'Use adaptive formatting'
Save, and restart Visual Studio
This should do the trick for you.
In visual studio 2022 community:
Go to Tools -> Options -> "Generate .editorconfig file from settings"
Open .editorconfig in some text editor
Search for "indent_size" and "indent_style" and adapt them to your needs
effects are reflected without even restarting visual studio
There is another option under Tools - Options
Text Editor - All Languages - Tabs.
Try to specify Tab size and indent size

VisualStudio curly brackets won't indent macos

I downloaded and installed visual studio on my mac to work with xamarin and every time I try to use the text editor I just can't.
This is how the layout looks like:
Visual studio text editor
And this is how it looks like when I use monoDevelop instead:
MonoDevelop text editor
Now, I want to use xamarin and I can't seem to find a solution to the brackets acting they way they do. If anyone can help I would much appriciate it. It's been bothering me for a while now.
It seems like the tabs for C# were not set up correctly within your Visual Studio.
Please try the following:
a) On the 'Tools' menu, click 'Options'.
b) Click 'Text Editor'.
c) Open C# and click 'Tabs'.
d) Make sure that 'Smart' Indentation is selected.
For further information please check out: https://msdn.microsoft.com/en-us/library/999b219z(v=vs.71).aspx
Note
You can automatically let Visual Studio indent your code:
Autoformat selection: Ctrl+K, Ctrl+F
Autoformat document: Ctrl+K, Ctrl+D

Unable to customize color settings for Code Coverage Result in Visual Studio

I wanted to customize color settings for Code Coverage Result in Visual Studio, but when I tried doing this with a solution suggested somewhere as shown below:
To change color, or customize the style, you need to open Options
Dialog window, from Tools –> Options. Then search for “Fonts and
Colors” option in the right side tree explorer. Once Fonts and Colors
is selected, search for “Coverage… “ under Display Items. You will
find following three Display items ◾Coverage Not Touched Area ◾Coverage Partially Touched Area ◾Coverage Touched Area
But I am unable to see the following options in the list:
Coverage Not Touched Area
Coverage Partially Touched Area
Coverage Touched Area
Can anyone suggest me how to add these options in that list?
Close VS.
Then try running devenv /resetuserdata in visual studio command prompt.
Re open VS and see if these options are found.
Otherwise you just have to repair your VS. That should enable these options.

Collapse all #regions only(!) in C# (Visual Studio)

There's a number of keyboard shortcuts and menu commands to automatically expand or collapse all foldables in the current document. Ctrl+M,
Ctrl+L toggles all foldables recursively, from the top namespace down to the inner methods and comments. Basically everything that has a [+] icon in the left margin. Ctrl+M, Ctrl+O does it bit less, but it still touches methods and comments inside the class.
I'm looking for a function that specifically only regards #region foldables and nothing else. Not namespaces, classes, comments, or methods. I really only want to fold those areas that are explicitly marked as such with the #region keyword.
Is there any hidden shortcut or an extension for that?
I'd prefer a keyboard solution, for Visual Studio 2015 (RC for now).
In case somebody asks why: I want to get an overview of the file, but immediately see all the details when I decide to look inside one region.
Update:
Solution in 2020
As this question has received many answers, some of them useless, others outdated, the working solution for Visual Studio 2019 can be found in this underappreciated answer here: https://stackoverflow.com/a/58999093/143684 Link to VS 2022 version in the comments.
in Visual Studio 2017 I have to activate 'Collapse #regions when collapsing to definitions' in
Tools -> Options -> Text Editor -> C# -> Advanced
explicitly to collapse all when pressing Ctrl+M+O
Ctrl+M+O will collapse all.
Ctrl+M+L will expand all. (in VS 2013 - Toggle All outlining)
Ctrl+M+P will expand all and disable outlining.
Ctrl+M+M will collapse/expand the current section.
These options are also in the context menu under Outlining.
Right click in editor -> Outlining to find all options. (After disabling outlining, use same steps to enable outlinging.)
To collapse regions : Tools > Options > Text Editors > C# > Advanced >
Check "Collapse #regions when collapsing to definitions"
Update for VS2022: https://github.com/mdmower/CollapseRegionExtension/releases/tag/v1.5 (thanks #ygoe)
I know this is an old question, but here's an update for visual studio 2019:
The Quick Tasks solution from #ygoe's answer does not work for Visual Studio 2019, because that version changed the way the Quick Task bar searches. Plus it was never the preferred solution to the original question, since you needed to type a quick action instead of having a keyboard shortcut.
I found a different solution on the marketplace which does exactly the collapsing and expanding of all regions in the currently opened file with keyboard shortcuts: Collapse Region
From the extension page:
Default key bindings are (Ctrl+R, Ctrl+Num+) and (Ctrl+R, Ctrl+Num-).
Can be changed in Tools->Options->Environment->Keyboard.
The Visual Studio extension Productivity Power Tools 2015 from Microsoft has a feature called Quick Launch Tasks that adds new commands to the Quick Launch menu. One of them is CollapseRegions and it does exactly that.
The opposite command is ExpandRegions and it expands all regions for quick browsing of the entire file. These commands can be used pretty quickly by pressing the CtrlQ hotkey and typing Coll resp. Exp, then pressing Enter (supposed you don't have other commands with the same prefix).
tick the Checkmark in Settings -> TextEditor -> C# -> Advanced -> (section Outlining) Collapse #regions when collapsing definitions.
then right click in the editor > outlining > collapse to definitions
I dont know if this is new, but there is a setting for the c# text edior:
Settings -> TextEditor -> C# -> Advanced -> Collapse #regions when collapsing definitions.
When its set CtrlM CtrlO will collapse the #regions.
Looks like the closest thing is Ctrl+M, Ctrl+S
Which will collapse the current region you are in, while Ctrl+M, Ctrl+E will expand the current region you are in.
List of default keyboard shortcuts:
https://msdn.microsoft.com/en-us/library/da5kh0wa(v=vs.140).aspx
I recommend using the free Visual Studio extension "Menees VS Tools Extension for Visual Studio." Just search for it in Tools --> Extensions and Updates in Visual Studio. Once installed you just have to hit Ctrl+M+K and just the regions will be collapsed. Here is their website https://marketplace.visualstudio.com/items?itemName=BillMenees.MeneesVSTools2013
To edit the settings look under Tools --> Options --> Menees VS Tools --> General.
For Visual Studio 2022, here is the tool that works well:
https://marketplace.visualstudio.com/items?itemName=EngineDesigns.CollapseAllRegions
CTRL-M CTRL-R
to collapse all #region groups.
Not sure about previous Visual Studio versions, but in VS 2022 we have new shortcut
CTRL + M + M
It will collapse any part of code like namespace, class, region etc.,
Steps:
Go to #region piece of code.
Select #region or click on #region anywhere in that word.
From Keyboard click CTRL + M + M
If you want for entire namespace to collapse, you can do same steps by selecting or clicking namespace.
Select All via Ctrl A and then Ctrl M M
i.e. Press M twice while holding down Ctrl

Visual Studio hot keys change occasionally, specifically F6 vs Ctrl-Shift-B for building. WHY?

I always press F6 to build my project. Suddenly some of my Visual Studio instances are wanting me to use Ctrl-Shift-B. It's not keyboard related - the actual text of the menu option changes from "F6" to "Ctrl-Shift-B".
Any answers as to how to fix, and what causes this?
Keyboard mapping corruption issue perhaps? Check Tools / Options, Environment / Keyboard. Should see a drop-down for your Keyboard mapping scheme and next to it a Reset button. Hit the reset button.
I'm not responsible for this screwing with your settings - save them prior to doing this just in case you screw the pooch.
Oh, in case the narcs out there care, I think this is a valid question and would be put out if someone voted to close it. Of course now that I've said that, its a certaintity, isn't it?
Your keyboard scheme has changed - go to Tools/Options/Environment/Keyboard and set the default keyboard scheme to 'Visual C# 2005'
This is because you might have selected "General Development Settings" as your default environment settings for your Visual Studio (which usually we do on the first launch of VS after installing it as it pops up a dialogue box to choose default environment settings).
If you only want to change the keyboard hotkeys settings, you just follow the solution proposed by others (Tools > Options > Environment > Keyboard > Visual C# 2015). BUT this will only change the keyboard settings to C# settings while keeping rest of the settings as General settings only.
If you are really intending to change complete environment to C# settings, then follow this approach.
SOLUTION 1:
Go to "Tools" (menu)
"Import and Export Settings"
"Import selected environment settings"
Either select "Yes, save my current settings" or "No, just import new settings, overwriting my current settings"
"Which collection of settings do you want to import?"
Under "Default Settings" tree select "Visual C#" node (you might see a yellow warning sign, NO need to worry about it) and then click "Finish" button.
or you can do the same like
SOLUTION 2:
Go to "Tools" (menu)
"Import and Export Settings"
"Reset all settings"
Either select "Yes, save my current settings" or "No, just import new settings, overwriting my current settings"
Select "Visual C#" from the list and then click "Finish" button.
Both approaches would give you the same result of changing your complete environment to 'Visual C#" there onward.
Enjoy Coding :)
If you have other key bindings and dont want to reset the lot use: Tools -> Options -> Environment -> Keyboard
Then find Build.BuildSolution or Build.RebuildSolution (on your preference) select Global in the Use shortcut in dropdown, place the cursor in Press Shortcut Key and slap F6. It'll show you what it is currently assigned to, for me is was to traverse split panes (maybe it overrides F6 when you use split panes for the first time? I've always wondered why my home development machine built with F6 and my work one doesn't and come to think of it I've never used split panes at home) then hit Assign.
Its because you installed VMWare. It remaps that key so that F6 can be used to start debugging on a virtual machine.
Annoyed me too.
You can reassign SHIFT + F6 to build the current project only:
1/ Go to Tools -> Options -> Environment -> Keyboard
2/ Show commands containing: Build.BuildSelection
3/ Focus the field Press shortcut keys & press Shift + F6 to assign it
Notepad++'s NPPscripting plugin broke Ctrl-Shift-B for my whole OS while NP++ was running. Uninstalling the plugin (I had to delete the DLL manually from the Notepad++ plugins folder) fixed the broken key mapping for me.
I had TPFanControl installed which captured Ctrl+Shift+B globally. I used a tool mentioned in this answere to find out which program registered for which shortcuts.
I had a stale build in my solution (included libraries were apparently using old DLL versions and I was getting MissingMethodException's at runtime) so I deleted all the \bin directories in my C# projects, restarted Visual Studio, and suddenly this issue is now affecting me for the first time in the 3+ years since I started using Visual Studio.
No clue how this could have affected my user settings, but it apparently did. They went from using the "Visual C# 2005" keyboard profile to "default"
Tools -> Options -> Environment -> Keyboard
search this command: "ClassViewContextMenus.ClassViewProjectBuild"
Assign F6
Visual Studio 2019, default keyboard mapping scheme is "Visual C# 2005"
When I open a Git repo as a folder (NOT open a VS solution), the "Build Solution" shortcut is Shift+F6 (which does nothing, because there isn't a solution open, which can be built)
As soon as I open a VS solution, the "Build Solution" shortcut changes to F6.
So please check: Did you open a VS solution, or is a Git repo only open "as a folder"?

Categories

Resources