Visual Studio 2010 Formatting - visual-studio

I have plenty of experience with Eclipse, and now I'm trying out Visual Studio 2010. I find its formatting somewhat counter-intuitive. Here are some things I'm trying to figure out:
Is there a way to select all text and format/indent it properly, like SHIFT+A SHIFT+I in Eclipse?
Why is it that when I type a line like if (n == 0) {, as soon as I type the opening brace, the text cursor is moved to the beginning of the line? Is this some productivity speedup I'm failing to see?
When I hit ENTER after the aforementioned line, I'd like the closing brace to be put in place automatically for me. How can I do this?
I've looked for hotkey documentation, and it's helped a bit, but this still feels clunky to me.

The Format Document shortcut key combination is Ctrl K, Ctrl D. Since this command is not supported in C++ ( Visual Studio 2010: Why aren't key combinations available?), the workaround for C++ files is to Select All then Format Selection: Ctrl A, Ctrl K, Ctrl F.
On your second and third question, see Creating and Using IntelliSense Code Snippets. Short version: for if, type "if {TAB} {TAB}". Again, this not supported for C++. So if you're in C++, what you're seeing is when you typed typed the { on the line after the if, what the editor did was move the { to the same indention level as the if (not necessarily the beginning of the line), because the coding style it's helping you achieve is
if (n == 0)
{
n = 1;
}

The formatting commands are by default bound to Ctrl+K Ctrl+??. Ctrl+K as the first keystoke, followed by another key stroke that determines the specific formatting option.
Look at the Advanced submenu of the Edit menu. It will show you that
"format selection" is Ctrl+k Ctrl+f
"comment selection" is Ctrl+k Ctrl+c

To format a document in visual studio the key combination is: ctrl-k ctrl-d

just FYI in eclipse it's: ctrl-a -> ctrl-i
not shift-a -> shift-i
I'm sure I'm the only one that actually tried that in eclipse.

Related

Does Visual Studio have a begin selection/set mark command like Emacs?

In Emacs, one can begin a selection (more accurately "set the mark") by pressing Ctrl-Space. Then, one can navigate around the file, e.g. by pressing End to go to the end of the current line, and the selection will follow the caret.
There is more to the Emacs mark than that, but my question is:
Does Visual Studio 2013 have a way to begin a selection at the caret and have the selection follow the caret until ESC is pressed, or something similar? Visual Studio has a command called "Edit.EmacsSetMark", but it does not seem to work for me.
I only know of SHIFT + cursor keys (including PgUp/PgDn, Home/End, and most of them also with Ctrl). Could take a bit of practice in your case, but very convenient IMO. The selection is cleared as soon as you use a cursor key with SHIFT released.
Even rectangular selections are supported if you also press ALT.
Then there is also the mouse of course. With variations by using SHIFT (extend selection) and ALT (rectangular).
Not that I know of for 2013, but you could do it in 2010 with this...a link

Keyboard Shortcuts in Visual Studio for faster coding

Visual Studio autocompletes brackets and tags and then shifts the cursor inside a bracket/tag upon creation.
I usually have to hit END then ENTER to continue past the tag, it would be really useful to 'jump' over the closing tag/bracket and possibly to the next line.
Is there a fundamental keyboard shortcut I'm missing here in order to accomplish this?
Can't find this in the MSDN VS Keyboard Shortcut manual either.
The keyboard shortcut you are looking for is Ctrl + Shift + Enter.
Pressing this will take you to the next line instead of inserting an enter, allowing you to move the cursor outside of the tag.

How to navigate to a closing bracket with ReSharper?

Say you have a large amount of C# code in an if statement. If you place your carat next to the opening bracket, is there a hotkey or something in ReSharper that will automatically take you to the closing bracket?
VS offers this shortcut, regardless of whether you have R# installed.
Ctrl + ] will take you to the opening brace. Subsequent presses will jump between the RHS/LHS of the scope.
See Go to Matching Brace in Visual Studio? (now as an answer as requested!)
ReSharper assigns the shortcut (Control + ´ - I have german keyboard) to a different command. In order to restore it go to Tools - Options - Environment - Keyboard, search for Edit.GotoBrace and enter the desired shortcut key.
See to what command it is currently assigned and then remove it for this command first by searching this command and clicking Remove. Then again search for Edit.GotoBrace and assign the shortcut.
Directly assigning without Removing it first didn´t work.
Visual Studio's shortcut is (under the IntelliJ shortcut set): Control + ] when your cursor is on the opening brace goes to the ending brace. The inverse is also true.

How to navigate back to the last cursor position in Visual Studio?

What is the keyboard shortcut navigate back to the last cursor position in Visual Studio?
It Will not work for red color (-) key. For me it only works for blue color combination.
According to Visual C# 2008 Keybinding Reference Poster it is Ctrl + -. The name of the specific keybinding is View.NavigateBackward.
PS: While researching I also found that Ctrl + . is the same as Shift + Alt + F10. Nice!
ctrl + - (dash) navigates backward.
ctrl + shift + - (dash) navigates forward.
These settings can be found under Environment -> Keyboard:
For Changing the setting in Visual Studio 2019:
Search for view.navigate
CHOOSE "Text Editor" from the "Use new shortcut in:" drop down menu
Select your shortcut
Global doesn't catch for this.
For new VS Code(1.28.2)
Back: Ctrl+Alt+- (dash)
Forward: Ctrl+Shift+- (dash)
The most generic answers is: there is no working default and you need to define your own keyboard shortcuts for View.NavigateBackward and View.NavigateForward.
Why? For most keyboards, the default shortcut is a broken, unusuable combination because VS badly handles the shift and altGr modifiers. MS did not pay attention to portability and internationalisation so much when they redeveloped VS after version 6, and this is still true today. This bug has been there for way more than a decade, nearly two decades. At this rate, it will never be fixed. And yes, I have filled a bug report, and I'm certainly not the only one.
However, their "VSCode" product line does have better keyboard handling as it doesn't depend on the shift or altGr modifiers to identify the key. For example, when you are in text writing mode and press the key that has the dash symbol, without using shift nor altGr, let's say it writes something else, like number 6. To VSCode when it comes to shortcut handling, that's still the dash key for its purpose. As long as a key has the symbol written on it, whether this is painted as the 1st, 2nd or 3rd level doesn't matter, it just that key.
Of course, it's never a good thing to make the default shortcuts use non-alphanumeric symbols, that's always confusing, whether it works or not. The good mature text editors have known that for a long time and should be taken as examples of things done right. In some ways, VS learned a few good things from emacs with shortcuts that are a sequence of two letters, but ultimately screwed up on other parts with the choice of non-alphanumeric bindings, combined with a broken low-level keyboard handling.
This works for me in Visual Code
Navigate backward Alt+←
Navigate forward Alt+→

How to automatically indent source code?

How can I automatically indent source code in Visual Studio 2010?
I have used Ctrl + K, Ctrl + F, but it does not work; is there any other way/plugin to do this?
Ctrl+E, D - Format whole doc
Ctrl+K, Ctrl+F - Format selection
Also available in the menu via Edit|Advanced.
Thomas
Edit-
Ctrl+K, Ctrl+D - Format whole doc in VS 2010
In 2010 it is Ctrl+k, Ctrl+d. See image below.
In Visual Studio 2010
Ctrl +k +d indent the complete page.
Ctrl +k +f indent the selected Code.
For more help visit : http://msdn.microsoft.com/en-us/library/da5kh0wa.aspx
every thing is there.
In 2010 it is ctrl +k +d for indentation
Also, there's the handy little "increase indent" and "decrease indent" buttons. If you highlight a block of code and click those buttons the entire block will indent.
I have tried both ways, and from the Edit|Advanced menu, and they are not doing anything to my source code. Other options like line indent are working. What could be wrong? – Chucky Jul 12 '13 at 11:06
Sometimes if it doesnt work, try to select a couple lines above and below or the whole block of code (whole function, whole cycle, whole switch, etc.), so that it knows how to indent.
Like for example if you copy/paste something into a case statement of a switch and it has wrong indentation, you need to select the text + the line with the case statement above to get it to work.
It may be worth noting that auto-indent does not work if there are syntax errors in the document. Get rid of the red squigglies, and THEN try CTRL+K, CTRL+D, whatever...

Resources