Skip to main content

Visual Studio Code Tips & Tricks


Visual Studio Code Tips & Tricks

Good code editors increase development speed and reduce application development time. Visual Studio Code proves this sentence true.

Visual Studio Code or VS Code is one of the most popular Code Editor Nowadays. It is Open Source Code Editor with over 52K stars on GitHub

VSCode was released in 2015 and now It is used by over 35% of developers in the world according to the Stack overflow Developer Survey.

VSCode Statistics : Stackoverflow Survey

VSCode combines the simplicity of a source code editor with powerful developer tooling, like IntelliSense code completion and debugging.

Now It is not only a simple code editor but used as a proper IDE.

If you have not tried VSCode before I strongly recommend you to try it once and I am sure you will love it.

If you are using it then you will be aware with the basic VS Code shortcuts and techniques.

In this article, you will find important Visual Studio Code tips and tricks which I am sure will increase your development speed.

We will start with the important visual studio code shortcuts for Windows, Linux and Mac.

VSCode comes with a set of default shortcuts. here we will see important shortcuts which are frequently used in coding.

Command Palette

Using Command Palette you can access all available commands based on your current context.

Windows / Linux : Ctrl + Shift + P or F1
Mac : Cmd + Shift + P or F1

Command Palette : VSCode Tips and Tricks

Quick Open

This shortcut is used to quickly navigate between files, with additional options. 

Using 

<b>@:</b>
 we can navigation between methods and symbols in file.

Using 

:
 we can navigate to a particular line in a file. At that time it also show you maximum number of lines in file.

You can also navigate to the previously accessed code window using Ctrl + P P

Windows / Linux : Ctrl + P 
Mac : Cmd + P

Quick Open : VSCode Shortcut

Format Document

This is an extremely helpful feature of VSCode.  We can format coding document using Format Document Command from Command Palette, Context Menu or using below Shortcut.

Windows / Linux : Altr + Shift + F

To format selected portion from document use below shortcut.

Windows / Linux : Ctrl+K Ctrl+F

Format Document : Visual Studio Code Tips and Tricks

Toggle Sidebar

Sidebar contains various panels, like Open Editors, File Tree Panel etc. We can toggle this sidebar based on requirement.

Windows / Linux : Ctrl + B
Mac : Cmd + B

Toggle Sidebar

Toggle Terminal

VSCode come with the inbuilt terminal, we can toggle this terminal window using below shortcut.

Windows / Linux : Ctrl + J
Mac : Cmd + J

Toggle Terminal

Copy Line or Code Block Up / Down

We can copy a single line or code block above or below the selected line just easily.

Windows / Linux : Altr + Shift + Down 
Mac : Opt + Shift + Down

here you can use UP key if you want to copy the code above the selected line.

Copy Code Block

Move Line or Code Block Up / Down

We can move a single line or code block above or below the selected line just using shortcut.

Windows / Linux : Altr + Down 
Mac : Opt + Down

here you can use UP key if you want to move the code above the selected line.

Move Code Block : VSCode Tips

Go To Definition

Select a symbol then press F12. Alternatively, you can use the context menu or Ctrl+click (cmd+click on macOS).

Go to Definition : VSCode Shortcut

Peek Definition

Select a symbol then press Altr + F12. Alternatively, you can use the context menu.

Peek Definition : VSCode Shortcut

Find All References

You can find all references of a particular symbol in a project using this feature.

To use this feature select a symbol then type Shift+F12. Alternatively, you can use the context menu.

Find All References : VSCode Shortcut

Multi Cursor Selection or Column Selection

Multi Cursor selection is very helpful while we required to edit multiple rows at the same time.

Windows : Ctrl + Altr + Down 
Linux : Altr + Shift + Down 
Mac : Opt + Cmd + Down

here you can use UP key or Down key.

You can also add cursor in multiple line using Altr + Click.

For column selection you can use Middle mouse button click and move on multiple lines.

Multi Cursor Selection : Visual Studio Code Tips and Trics
  • Note

You can see all other shortcuts in File > Preferences > Keyboard Shortcuts. You can also update and customize the shortcuts here.

If you are using any other editor before like sublime or atom, and you want to use same shortcut in vscode then you can also change the default shortcut setting. refer VSCode Keybindingsfor more information.

Find here Visual Studio Code Shortcut Cheat Sheet for Windows, Linux and Mac

Visual Studio Code supports Emmet Notation right in the editor. No extension is required. It supports the majority of the Emmet Actions including expanding Emmet abbreviations and snippets

Visual Studio Code emmet notation

You can use most of the Emmet actions with multi-cursors as well.

Find here Visual Studio Code Emmet Cheat Sheet

emmet multi cursor : VS Tips and Tricks

Generally, Angular or Typescript application communicates to the web server using JSON web services, we write interface same as JSON api to map TypeScript object and JSON request & response. We can automate this Object conversion from JSON.

Instead of writing interfaces manually we can use very popular JSON to TS extension

Convert from clipboard (Ctrl + Alt + V)

Convert from selection (Ctrl + Alt + S)

JSON to TS
  • Note

If you want to paste JSON to the code of Go, C#, C++ or TS then you can use another extension called Paste JSON as Code.

Refer here Best Visual Studio Code Extensions.

Debugging is the crucial part of any application development. VSCode supports inbuilt debugger. It also has Chrome debugger extension to debug an application running on chrome.

VSCode provides additional feature other than the normal debugger,

  • Conditional Breakpoint
  • Log points.

Conditional Breakpoint

You can add a condition and/or hit count either when creating the breakpoint with the Add Conditional Breakpoint action or with the Edit Breakpoint action for existing breakpoints. In both cases an inline text box with a drop-down menu opens where the expressions can be entered:

conditional breakpoint : VSCode Debugging

Log points

A Logpoint is a variant of a breakpoint that does not “break” into the debugger but instead logs a message to the console. Logpoints are especially useful for injecting logging while debugging production servers which cannot be stopped.

A Logpoint is represented by a “diamond” shaped icon. Log messages are plain text but can include expressions to be evaluated within curly braces (‘{}’).

log points : VSCode Tips and Tricks

Zen Mode lets you focus on your code by hiding all UI except the editor (no Activity Bar, Status Bar, Sidebar and Panel) and going to full screen.

Zen mode can be toggled using the View menuCommand Palette or by the shortcut Ctrl+K Z. If you prefer not to transition to full screen, that can be disabled via 

window.fullScreenZenMode
.

Double Escape exits Zen Mode.

Zen Mode : Visual Studio Code Tips and Tricks

VSCode has a large number of extensions which are very helpful to reduce boilerplate code writing.

For example Angular 6 snippets, Bootstrap & Font-awesome snippetsetc. 

The same way we can also write our own frequently used code snippets, refer here to see how to create user-defined snippets

Angular v5 Snippets

Live Share in VSCode code is recently released. 

Live Share enables your team to quickly collaborate on the same codebase without the need to synchronize code or to configure the same development tools, settings, or environment.

For more information refer VSCode Live Share article

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...