Visual Studio Code, New Editor from Microsoft For Windows, OS X, and Linux
Visual Studio Code is a source code editor developed by Microsoft for Windows, Linux and macOS. It includes support for debugging, embedded Git control, syntax highlighting, intelligent code completion, snippets, and code refactoring. It is also customizable, so users can change the editor's theme, keyboard shortcuts, and preferences. It is free and open-source, although the official download is under a proprietary license
What Will I Learn?
Quick Access
I find myself looking for a file, a variable, a function, a git command, or a task quite often. Especially when I do not know exactly where it is. VSCode provides quick access to these through quick access palettes and commands. here what will we learn from VS code:
- User Interface - View the documentation for VS Code.
- Setup Overview - Documentation for getting up and running with VS Code, including platform specific setup.
- Keyboard Shortcuts - Customize your own shortcuts, download a reference sheet, or install a -Keymap extension.
- Keymaps in the Marketplace - Install a Keymap extension to bring the keybindings from your previous editor to VS Code.
Requirements
- Basic knowledge of Visual code
- Download and install VS Code here: https://code.visualstudio.com/
- Explore VS Code features in the Interactive Editor Playground
- See an overview of the user interface.
- Customize your editor with the View menu and by changing the color theme.
- Change your keyboard shortcuts using a Keymap extension.
- Focus on your source code with Zen Mode.
Visual Studio Code has also been integrated into GitHub. So you can call and store all the application code you are working directly from this application.
Difficulty
- Intermediate
Tutorial Contents
Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (such as C++, C#, Java, Python, PHP, Go) and runtimes (such as .NET and Unity).
The Basics of Visual Studio Code
At its heart, Visual Studio Code is a code editor. Like many other code editors, VS Code adopts a common UI and layout of an explorer on the left, showing all of the files and folders you have access to, and an editor on the right, showing the content of the files you have opened.
In addition, there are a number of unique features in the VS Code user interface. This topic describes these features.
Files, Folders & Projects
VS Code is file and folder based ‐ you can get started immediately by opening a file or folder in VS Code.
On top of this, VS Code can read and take advantage of a variety of project files defined by different frameworks and platforms. For example, if the folder you opened in VS Code contains one or more package.json , project.json , tsconfig.json , or ASP .Net v5 Visual Studio solution and project files, VS Code will read these files and use them to provide additional functionality such as rich IntelliSense in the editor.
Basic Layout
VS Code comes with a simple and intuitive layout that maximizes the space provided for the editor while leaving ample room to browse and access the full context of your folder or project. The UI is divided into four areas .
- Editor the main area to edit your files. You can open up to three editors side by side
- Side Bar contains different views like the Explorer to assist you while working on your project
- Status Bar indicates information about the opened project and files you edit
- View Bar on the far left‐hand side, lets you switch between views and gives you additional context specific‐indicators, like the number of outgoing changes when Git is enabled
Each time you start VS Code, it opens up in the same state it was in when you last closed it. The folder, layout and opened files are preserved.
Keyboard Shortcuts
Being able to keep your hands on the keyboard when writing code is crucial for high productivity. VS Code has a rich set of default keyboard shortcuts as well as allowing you to customize them.
- Keyboard Shortcuts Reference - Learn the most commonly used and popular keyboard shortcuts by downloading the reference sheet.
- Install a Keymap extension - Use the keyboard shortcuts of your old editor (such as Sublime Text, Atom, and Vim) in VS Code by installing a Keymap extension.
- Customize Keyboard Shortcuts - Change the default keyboard shortcuts to fit your style.
Multiple selections (multi-cursor)
VS Code supports multiple cursors for fast simultaneous edits. You can add secondary cursors (rendered thinner) with Alt+Click. Each cursor operates independently based on the context it sits in. A common way to add more cursors is with Ctrl+Alt+Down or Ctrl+Alt+Up that insert cursors below or above.
Note: Your graphics card driver (for example NVIDIA) might overwrite these default shortcuts.
(Windows, Linux Ctrl+D) selects the word at the cursor, or the next occurrence of the current selection.
Multi-cursor modifier
If you'd like to change the modifier key for applying multiple cursors to Cmd+Click on macOS and Ctrl+Click on Windows and Linux, you can do so with the editor.multiCursorModifier setting. This lets users coming from other editors such as Sublime Text or Atom continue to use the keyboard modifier they are familiar with.
The setting can be set to:
- ctrlCmd - Maps to Ctrl on Windows and Cmd on macOS.
- alt - The existing default Alt.
There's also a menu item Use Ctrl+Click for Multi-Cursor in the Selection menu to quickly toggle this setting.
The Go To Definition and Open Link gestures will also respect this setting and adapt such that they do not conflict. For example, when the setting is ctrlCmd, multiple cursors can be added with Ctrl/Cmd+Click, and opening links or going to definition can be invoked with Alt+Click.
Shrink/expand selection
Quickly shrink or expand the current selection. Trigger it with ⌃⇧⌘← (Windows, Linux Shift+Alt+Left) and ⌃⇧⌘→ (Windows, Linux Shift+Alt+Right)
Here's an example of expanding the selection with ⌃⇧⌘→ (Windows, Linux Shift+Alt+Right):
Column (box) selection
Hold Shift and Alt while dragging to do column selection:
Save / Auto Save
By default, VS Code requires an explicit action to save your changes to disk, ⌘S (Windows, Linux Ctrl+S).
However, it's easy to turn on Auto Save, which will save your changes after a configured delay or when focus leaves the editor. With this option turned on, there is no need to explicitly save the file. The easiest way to turn on Auto Save is with the File > Auto Save toggle which turns on and off save after a delay.
For more control over Auto Save, open User or Workspace settings and find the associated settings:
- files.autoSave: Can have the values:
- off - to disable auto save.
- afterDelay - to save files after a configured delay (default 1000 ms).
- onFocusChange - to save files when focus moves out of the editor of the dirty file.
- onWindowChange - to save files when the focus moves out of the VS Code window.
- files.autoSaveDelay: Configures the delay in milliseconds when files.autoSave is configured to afterDelay. The default is 1000 ms.
Hot Exit
VS Code will remember unsaved changes to files when you exit by default. Hot exit is triggered when the application is closed via File > Exit (Code > Quit on macOS) or when the last window is closed.
You can configure hot exit by setting files.hotExit to the following values:
- "off": Disable hot exit.
- "onExit": Hot exit will be triggered when the application is closed, that is when the last window is closed on Windows/Linux or when the workbench.action.quit command is triggered (from the Command Palette, keyboard shortcut or menu). All windows with backups will be restored upon next launch.
- "onExitAndWindowClose": Hot exit will be triggered when the application is closed, that is when the last window is closed on Windows/Linux or when the workbench.action.quit command is triggered (from the Command Palette, keyboard shortcut or menu), and also for any window with a folder opened regardless of whether it is the last window. All windows without folders opened will be restored upon next launch. To restore folder windows as they were before shutdown, set window.restoreWindows to all.
Search Across Files
VS Code allows you to quickly search over all files in the currently opened folder. Press ⇧⌘F (Windows, Linux Ctrl+Shift+F) and enter your search term. Search results are grouped into files containing the search term, with an indication of the hits in each file and its location. Expand a file to see a preview of all of the hits within that file. Then single-click on one of the hits to view it in the editor.
Advanced Search Options
In the two input boxes below the search box, you can include and exclude files. If you enter example, that will match every folder and file named example in the workspace. If you enter ./example, that will match the folder example/ at the top level of your workspace. You can also use glob syntax:
- to match one or more characters in a path segment
- ? to match on one character in a path segment
- ** to match any number of path segments, including none
- {} to group conditions (e.g. {/.html,/.txt} matches all HTML and text files)
- [] to declare a range of characters to match (e.g., example.[0-9] to match on example.0, example.1, …)
VS Code excludes some folders by default to reduce the number of search results that you are not interested in (for example: node_modules). Open settings to change these rules under the files.exclude and search.exclude section.
Also note the two toggle buttons in the files to exclude box. The left one determines whether to exclude files that are ignored by your .gitignore file. The right determines whether to exclude files that are matched by your files.exclude and search.exclude settings.
Search and Replace
You can also Search and Replace across files. Expand the Search widget to display the Replace text box.
When you type text into the Replace text box, you will see a diff display of the pending changes. You can replace across all files from the Replace text box, replace all in one file or replace a single change.
IntelliSense
We'll always offer word completion, but for the rich languages, such as JavaScript, JSON, HTML, CSS, Less, Sass, C# and TypeScript, we offer a true IntelliSense experience. If a language service knows possible completions, the IntelliSense suggestions will pop up as you type. You can always manually trigger it with ⌃Space (Windows, Linux Ctrl+Space). By default, Tab or Enter are the accept keyboard triggers but you can also customize these key bindings.
JavaScript and TypeScript developers can take advantage of the npmjs type declaration (typings) file repository to get IntelliSense for common JavaScript libraries (Node.js, React, Angular). You can find a good explanation on using type declaration files in the JavaScript language topic and the Node.js tutorial.
Formatting
VS Code has great support for source code formatting. The editor has two explicit format actions:
- Format Document (⇧⌥F (Windows Shift+Alt+F, Linux Ctrl+Shift+I)) - Format the entire active file.
- Format Selection (⌘K ⌘F (Windows, Linux Ctrl+K Ctrl+F)) - Format the selected text.
You can invoke these from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) or the editor context menu.
VS Code has default formatters for JavaScript, TypeScript, JSON, and HTML. Each language has specific formatting options (for example, html.format.indentInnerHtml) which you can tune to your preference in your user or workspace settings. You can also disable the default language formatter if you have another extension installed that provides formatting for the same language.
"html.format.enable": false
Along with manually invoking code formatting, you can also trigger formatting based on user gestures such as typing, saving or pasting. These are off by default but you can enable these behaviors through the following settings:
- editor.formatOnType - Format the line after typing.
- editor.formatOnSave - Format a file on save.
- editor.formatOnPaste - Format the pasted content.
Folding
You can fold regions of source code using the folding icons on the gutter between line numbers and line start. Move the mouse over the gutter and click to fold and unfold regions. Use Shift + Click on the folding icon to fold or unfold the region and all regions inside.
You can also use the following actions:
- Fold (⌥⌘[ (Windows, Linux Ctrl+Shift+[)) folds the innermost un-collapsed region at the cursor.
- Unfold (⌥⌘] (Windows, Linux Ctrl+Shift+])) unfolds the collapsed region at the cursor.
- Fold Recursively (⌘K ⌘[ (Windows, Linux Ctrl+K Ctrl+[)) folds the innermost un-collapsed region at the cursor and all regions inside that region.
- Unfold Recursively (⌘K ⌘] (Windows, Linux Ctrl+K Ctrl+])) unfolds the region at the cursor and all regions inside that region.
- Fold All (⌘K ⌘0 (Windows, Linux Ctrl+K Ctrl+0)) folds all regions in the editor.
- Unfold All (⌘K ⌘J (Windows, Linux Ctrl+K Ctrl+J)) unfolds all regions in the editor.
- Fold Level X (⌘K ⌘2 (Windows, Linux Ctrl+K Ctrl+2) for level 2) folds all regions of level X, except the region at the current cursor position.
- Fold All Block Comments (⌘K ⌘/ (Windows, Linux Ctrl+K Ctrl+/)) folds all regions that start with a block comment token.
Folding regions are evaluated based on the indentation of lines. A folding region starts when a line has a smaller indent than one or more following lines, and ends when there is a line with the same or smaller indent.
Regions can also be defined by markers defined by each language. The following languages currently have markers defined:
- C#: #region and #endregion
- C/C++: #pragma region and #pragma endregion
- CSS/Less/SCSS: /#region/ and /#endregion/
- Coffeescript: #region and #endregion
- F#: //#region and //#endregion and (#_region) and (#_endregion)
- Java: //#region and // #endregion and //<editor-fold> and //</editor-fold>
- PHP: #region and #endregion
- Powershell: #region and #endregion
- Python: #region and #endregion
- TypeScript/JavaScript: //#region and //#endregion and //region and //endregion
- VB: #Region and #End Region
- Bat: ::#region and ::#endregion
- Markdown: (html comment removed: #region ) and (html comment removed: #endregion )
To fold and unfold only the regions defined by markers use:
- Fold Marker Regions (⌘K ⌘8 (Windows, Linux Ctrl+K Ctrl+8)) folds all marker regions.
- Unfold Marker Regions (⌘K ⌘9 (Windows, Linux Ctrl+K Ctrl+9)) unfolds all marker regions.
Auto-detection
VS Code analyzes your open file and determines the indentation used in the document. The auto-detected indentation overrides your default indentation settings. The detected setting is displayed on the right side of the Status Bar:
- auto detect indentation
You can click on the Status Bar indentation display to bring up a drop-down with indentation commands allowing you to change the default settings for the open file or convert between tab stops and spaces.
File Encoding Support
Set the file encoding globally or per workspace by using the files.encoding setting in User Settings or Workspace Settings.
- files.encoding setting
You can view the file encoding in the status bar.
- Encoding in status bar
Click on the encoding button in the status bar to reopen or save the active file with a different encoding.
- Reopen or save with a different encoding
Then choose an encoding.
Command Palette
CMD+SHIFT+P opens the command palette. You can type what you are looking for and perform that action easily. No need to remember menu items or where that button may be hiding. Want to change your theme? Configure debugging? Open keyboard mapping preferences? Run tasks? Open a new console/terminal? This is the place. It’s also a great place to just scroll through the entire list of available commands.
The name "Palette" and the idea is inspired by Sublime Text, which made this an immensely popular and useful feature.
CMD+SHIFT+P is the most helpful keystroke you’ll use in Visual Studio Code. It also shows the keyboard mapping for each command.
Navigate to File
CMD+P opens the generic command palette where you can search for any file or symbol in one place. You can also see recently opened files.
Thanks utopian..
Posted on Utopian.io - Rewarding Open Source Contributors
@farouqadam, No matter approved or not, I upvote and support you.
Thank you,, i appreciate it so much.. :)
Really good tutorial! I love it
Keep doing good work :D
Thanks dude,,, i wanna be a game developer too,, :)
Your contribution cannot be approved because it does not follow the Utopian Rules.
This is plagiarism of the official documentation: https://github.com/Microsoft/vscode-docs/blob/master/docs/getstarted/
You are now permanently banned from utopian.
You can contact us on Discord.
[utopian-moderator]