move out of closing brace in visual studio shortcut - visual-studio

I am new to visual studio. Everytime I open a brace in visual studio the IDE automatically closes the brace.
for (int i = 0; i < n; ++i) {
int j = 2 * i; // My cursor is here right now
}
// I want my cursor here without pressing down arrow key
I don't want to press down arrow key every time I want to move out of the block. Is there any shortcut for that in visual studio. I know I can turn off this feature.
Edit 1 : I don't know why this question is marked as duplicate. In fact, the "original" question is completely different than mine. The "original" question asks how to go from opening brace to closing brace and vice versa.

Pressing } gets me out of the block.

Related

How to customize functionality of "Delete Line" hotkey in Visual Studio Community 2022?

In most IDE's I've used, including VS Code, the "Edit.LineDelete" keyboard shortcut deletes the line and tries to keep the cursor in the same place on the following line. For example, this shortcut is Ctrl+Shift+K in VS Code and Ctrl+Y in IntelliJ, and does the following:
// I've used a pipe here to represent the cursor movement...
/* BEFORE line delete */
{
...
int foo = 0; |
foo++;
}
/* AFTER line delete */
{
...
int foo = 0;
foo++;|
}
However, Visual Studio Community Edition (both 2017 and 2022) seem to have a unique way of handling this. Instead of trying to keep the cursor in the same position, it immediately sends the cursor to the very beginning of the line (even before tab spaces). See the following:
/* AFTER line delete on Visual Studio Community */
{
...
int foo = 0;
| foo++;
}
This is extremely annoying for me, as it requires extra work (mouse or End key) to get the cursor back to a useful location. I'm not even sure why they would make this work nicely in VS Code but not VS Community Edition.
Is there a way to change this functionality to mirror the behavior in VS Code or IntelliJ? I don't know of any built-in support for macros, and it seems like overkill to get an extension just for this change. Am I missing something?
NOTE: A similar question was asked here, but I'm looking to do the exact opposite.
The default shortcut is Ctrl + Shift + L
see: https://visualstudio.microsoft.com/keyboard-shortcuts.pdf

Autoformat on custom event in Visual Studio

So currently Visual Studio will autoformat on ; so for example
int a=3
becomes
int a = 3;
but I'd like to autoformat on a custom event, such as when I smartbreak a line. Smartbreaking is by default Shift+Enter, when you break the line so that for example
int a = 3;
will stay like that even if you smart-linebreak in the middle of the statement.
Do you think this is possible? It enhances workflow significantly since my editor will often automatically add the semicolon at the end. By smartbreaking I will save a bunch of time.
This is the closest I've found:

Visual Studio 2015 - brackets not automatically formatting when pressing shift + enter

Normally, to create a new bracket block I would type:
"{" then enter
This produces:
{
}
however, (I have seen this on two different machines so far, it might be the default setting) if like me, you like to keep shift held down, or accidentally keep it down and instead press shift + enter, it creates a new line underneath.
{ }
//current cursor position
Leaving the un-formatted brackets behind!
This is really annoying as I nearly always do this, and have to fiddle around for a little second to get the brackets back to where I want them. What can I do to change the behavior of shift + enter?
Go to Options > Environment > Keyboard
Search for Edit.SmartBreakLine and remove the assigned shortcut (Shift + Enter)
Search for Edit.BreakLine, click on "Press shortcut keys:" textbox, press Shift + Enter and click Assign
If it doesn't work try restarting Visual Studio.
Update: This issue is fixed in Visual Studio 2015 Update 1 - SmartBreakLine works as expected.
We stumbled on this thread and it felt like we could make a nice improvement here to Shift+Enter (SmartBreakLine).
So, in cases where a block was opened { } and if shift was held down intentionally or unintentionally, we now do this:
{
|
}
instead of
{ }
|
this way people who are used to using shift+enter to complete the line (in C#, it adds a semicolon to the end of line if needed, formats the line, adds a new line after current line) do not have to lose those functionalities by remapping the shortcut to BreakLine.
See: https://github.com/dotnet/roslyn/pull/5790
this should make it in the next update of Vs2015 and we hope you like it.
It works well in my VS 2015. Try resetting all the settings.
Tools > Import and Export Settings > Reset all settings > Next > Visual C#
You can reset for all other languages similar like this.
Hope this helps!

Visual Studio Jump to Beginning of Function

I know that Ctrl+} will take you to the corresponding brace in Visual Studio, but say I'm in the middle of a gigantic function and I don't know where the top or the bottom is, is there a shortcut to get directly to the function declaration?
void function()
{
//so many lines of code
//can't see the top or the bottom curly brace
//can i get to the top of the function with a shortcut?
}
I have a fresh install of VS2017. As of 15.9.1, the default for me is Alt+Shift+[.
This is the shortcut for EditorContextMenus.Navigate.GoToContainingBlock. So you may have to execute this shortcut multiple times if you are a few block layers deep, but it'll get you where you want to go.
Alt+Ctrl+UP,Tab,Tab,Enter
This sequence will move you through Project selctor > Scope selector > Function selector > Current Function.
Ctrl+M,Ctrl+M
This sequence will toggle between collapse/expand current block.
Place cursor at any line that is immediately enclosed by the function. Collapse. Place cursor at the end of the collapsed function, i.e after { ... }. Expand the function to get to its last brace.
Note:
If you have difficulty in finding a line immediately enclosed by the function(for example, when the function has lots of nested blocks), you can always goto the beginning to collapse the function.
Update
With last updates Visual Studio, now default keyboard shortcut for EditorContextMenus.Navigate.GoToContainingBlock is Shift+Alt+[
Old Answer:
Visual Studio 2017 version 15.8.0 comes with a new shortcut Ctrl + Alt + UpArrow - Go to Enclosing Block.
Go to Enclosing Block (Ctrl + Alt + UpArrow) allows you to quickly
navigate up to the beginning of the enclosing code block.
Source
This command allows also move to function declaration if you are inside function.
If shortcut doesn't work for you
For the VSCode lovers, this key combination will bring you to the top of the function:
Ctrl-Shift-.
followed by ENTER
and for MAC users:
Cmd-Shift-.
followed by ENTER
I usually double press the white line that is located left of the code.
It closes the function but it also takes you to the declaration of the function.
You can do it with Macros for Visual Studio extension.
Here's the code for macros:
// BeginningOfFunction moves the caret to the beginning of the containing definition.
var textSelection = dte.ActiveDocument.Selection;
// Define Visual Studio constants
var vsCMElementFunction = 2;
var codeElement = textSelection.ActivePoint.CodeElement(vsCMElementFunction);
if (codeElement != null)
{
textSelection.MoveToPoint(codeElement.GetStartPoint());
dte.ActiveDocument.Activate();
}
It is one of the sample macros of the extension. Edited it a little, because for some reason sample didn't work for me. You can get to the end of the function by changing codeElement.GetStartPoint() to codeElement.GetEndPoint().
I found one trick in visual studio:
Place the cursor on the empty to get the context (name of the function), copy the name of the function, then click the drop down arrow where all functions will be listed, paste the function name, enter. Then you are at the beginning of that function!
Another alternative would be to use Edit.PreviousMethod. I prefer this option because even if your cursor lies in multiple nested block, you can get to the method definition in single keystroke! I have mapped Edit.PreviousMethod to ctrl + alt + , and Edit.NextMethod to ctrl + alt + . but you can set it to whatever you prefer.
To setup key binding, goto Tools.Options.Environment.Keyboard, then in Show Commands Containing textbox type edit.previousmethod, set focus on Press Shortcut Keys textbox and press the key combination you want, the hit Assign. Repeat for edit.nextmethod, then Ok.

Indentation in Visual Studio 2010 for C++ using Visual Assist X

I've come acrossed something annoying about indentation in C++ (using Visual Studio 2010 and Visual Assist X).
The thing is that every time I want to start a new line after keying in ctrl+enter and ctrl+shift+enter, the cursor begins at the very beginning of a new line. This fact annoys me a lot when using the hotkeys inside any braces. For example,
for (int i = 0; i < n; i ++) {
std::cout << "say something" /* press `ctrl+shift+enter` here*/ << std::endl;
! |<--
}
After pressing the hotkey (somewhere not at the end of the line, e.g., at the comment /*...*/ above), the cursor will be at the starting position (see ! in the above code). What I want is to automatically put the cursor in the new line with the same indentation of the previous line (see |<--).
How can I make this happen? Many thanks.
Here is an answer from Whole Tomato Software Support Forum

Resources