The best Visual Studio shortcuts

It is found that you could be more efficient and appear smarter if you use shortcut keys rather than doing things with the mouse. Sometimes I used to wonder how the system administrators are very fast in setting up things, restarting a crashed network, etc.

They do all with their precious keystrokes combined with Shift, Ctrl, and Alt,  Is’nt it? And who wouldn’t like shortcuts after all?

For those people who use Visual Studio to write code, behold I present you few of my favorite and best shortcuts that can be used while writing and managing code.

  • Collapse/Expand selection Ctrl+M+H/Ctrl+M+U
    Sometimes you would want to hide the annoying code that is blocking your view or distracting you, use this then.
  • Comment/Uncomment block of code Ctrl+K+C/Ctrl+K+U
    //bored of adding ‘// ‘on every line??
    //You can bulk comment and uncomment by using this shortcut.
  • Navigate Forward/Backward Ctrl+–/Ctrl+Shift+–
    Forgot the previous location of your cursor? Don’t worry. This is the hotkey for the back and forward buttons
  • Full Screen without losing the panels Alt+Shift+Enter
    Searching for the menubar and the panels while on Full Screen? This is here to rescue
  • Simple search Ctrl+I
    A simple search box is opened you can start typing after pressing this hotkey and press again to go to next result
  • The TAB key for “snippets”
    Best of the shortcuts. Bored of writing if-conditions and making sure that they are surrounded by braces?
    Type if and then press the tab key twice. Baam! the if-condition is completed for you. VS offers many snippets like try-catch, for, class, foreach etc
  • Surround with snippets CTRL+K+S
    Want a piece of code within an if-condition or a for-loop. Don’t  worry, select the code and press these keys. Select any snippet from the context menu shown and consider it done.
  • Find Matching braces Ctrl+]
    A conversation with the Open brace “{“
    Me: Hey you!! Where is your pair??
    Open Brace: I dunno 😔
    Me: Okay let me find it!
    After a while
    Got you!! You cant hide from me, You dumb closing brace!
    -To find the matching brace of a function/class use this shortcut.
    -To select the code between the braces, add shift key to the combination.
  • Ctrl+C+V to duplicate the current line
    This is a well-known legend yet in the form of a simple shortcut key.
  • Create/Remove a bookmark Ctrl+K+K
    Bookmarks, they are cousins to the breakpoints. They are not hit by the debugger but can still help you remember where a bug that you discovered yesterday is present.
    -Open BookMark window -Ctrl+K+W
    -Go to the next bookmark -Ctrl+K, Ctrl+N
  • Cycle through the list of clipboard contents Ctrl+Shift+V 
    This is also a well-known shortcut. Tired of copy-scroll-paste, copy-scroll-paste? You can replace it with copy-copy-scroll-paste-paste by using this shortcut
  • Collapse all functions Ctrl+M+O
    Too large code base? Minimize the code and find it easy to navigate through the code.
  • Format Selection Ctrl+K+F
    When the code is shabby and does not have proper spacing and indents, use this shortcut and see the Magic.

There are yet many more amazing shortcuts but these are my favorite, hope these are helpful in your programming career. You can take the shortcut keys to a whole new level using Auto Hot Keys, read it here.

Happy Coding 🙂

Local vs Global scope – JavaScript

Local scope

Let’s start by declaring a variable inside a function called cool (just a name) to understand the difference between local and global scope

Let’s declare a variable called localVariable with type var in our function as below

var cool = function (){
 var localVariable = 10;
 console.log('Inside function: ' +localVariable)
}
cool();
console.log('Outside variable: ' +localVariable);

And we call our function, and also we are trying to write our variable value inside and outside the function in the console.

Once the following line is executed

console.log('Inside function:' +localVariable)

We will be getting output as

Inside function: 10

Whereas when the following line is executed

console.log('Outside variable: ' +localVariable);
Uncaught ReferenceError: localVariable is not defined(…)

We’re getting above error because the localVariable is a local variable and its scope is available within the function only.

Global scope

Let’s edit the same function by renaming the localVariable into localVariable2

And this time we are going to remove the variable type in our code.

Let’s execute and check what will be output.

var cool = function (){
 var localVariable2 = 10;
 console.log('Inside function: ' +localVariable2) // Inside function: 10
}
cool();
console.log('Outside variable: ' +localVariable2); //Outside variable: 10

Now the output will be

Inside function: 10
Outside variable: 10

The interesting thing happening when we are initializing a variable without declaring the type, the JavaScript decides to make it as Global variable with Global scope.

When the variable which is initialized inside a function is assumed as Global scope by JavaScript, we can able to use it outside the function too.

JavaScript has function scope, not block scope. 

Let’s see another example about local and global scope.

var myVariable= "global"; // global
var cool= function() {
 var myVariable = "local";
 console.log(window.myVariable); // global
 console.log(myVariable); // local
}
cool();

Output

global
local

Here we are writing a global and local variable inside the function just by differentiating using window from browser environments.
We can access the variable as the property of global object (using window). And it’s not recommended to use window to access global objects.

Happy exploring JavaScript 🙂