Editor’s note: The following post was written by Visual C# MVP Schabse Laks
Visual Studio is a fantastic development environment for .Net-based Windows and web projects. However, this view of Visual Studio overlooks one of its most useful features: the text editor. Visual Studio includes a powerful text editor that can easily be used to edit standalone files without a project.
Unlike competing development editors such as Sublime, Visual Studio maintains a full-featured parse tree as you edit each file. This allows VS to provide unparalleled auto-completion and document-based actions, and can make the development experience much faster and more pleasant. These features are especially powerful in HTML and CSS documents.
All of this power is also available to extensions, making it simple to extend the editors with powerful new features to suit your needs. (I wrote a separate blog post about building your own extensions) A poster-child example is the open-source Web Essentials extension by Mads Kristensen. This extension adds a variety of useful features that are essential to modern web development. You can install Web Essentials from the Extension Gallery in Visual Studio:
Using the text editor alone
To use the VS text editor, you can simply drag any text file into the editor. To work more efficiently with an entire project, you should create a Web Site project to hold all of your files for easier navigation. Click File, Open, Web Site, then select the folder containing your existing files (or create a new folder to start from scratch). Web site projects will automatically list all files in the directory you select; there are no project files or inclusion lists to worry about. If you create a new file outside of Visual Studio, simply click the Refresh button in Solution Explorer and it will show up as expected.
To test your HTML locally, you can simply press F5, and Visual Studio will run IIS Express to host your site on a random port on localhost. No admin privileges needed. Visual Studio is thus especially useful for purely static sites hosted on GitHub pages. If you need to upgrade to an actual server, Windows Azure is just a few clicks away. You can also easily publish your site to an existing host over FTP or Microsoft Web Deploy.
Visual Studio 2013’s HTML editor was rewritten from scratch on top of the modern WPF-based editor stack. This new editor eliminates all of the formatting and validation issues with the old editor, and opens the door for extensions like Web Essentials to add all sorts of additional features.
The editor is now much smarter about HTML attribute IntelliSense. All of the aria-* attributes are now grouped into a single entry to avoid cluttering IntelliSense:
The editor also knows about attributes used by common web development frameworks, such as jQuery Mobile, Knockout.js, and AngularJS:
As you might expect, the editor will warn you about invalid HTML as you type.
Web Essentials adds more features to the HTML editor. You can press F12 (Go to Definition) on any local path (e.g., an image, a link to a separate HTML file, or a reference to a JS or CSS file) and jump directly to that file. You can see referenced images in tooltips. You can even press F12 on a CSS class and it will try to find a matching selector in CSS or LESS files in your project.
Web Essentials also adds support for Emmet/ZenCoding
Web Essentials really shines in its CSS features. Visual Studio’s standard CSS editor already features IntelliSense for CSS pseudo-classes/elements, property names, and values. It will and will give warnings for invalid selectors or properties. It even recognizes CSS hacks and will not show warnings for invalid CSS from common hacks.
Web Essentials adds lots of nice extras to the CSS editor. It adds get full IntelliSense for selectors as well, including all HTML elements and all CSS IDs & classes in the file. It even adds auto-completion for attribute names & values, filtered to only show attributes that apply to the element for element selector. It also adds a small preview every time you write a color (by name, HEX, RGB, or HSL).
You can see browser support information in the tooltip for each CSS property, along with sample usage. If you use a property that has vendor-specific versions for some browsers, Web Essentials will warn you if any of the vendor-specific (or standard) versions are missing, and will automatically update all of the vendor-specific versions as you change the value of the standard property (this feature does not work for vendor-specific values of standard properties).
You can use smart tags to easily convert between color formats, or to add CSS hacks to selector blocks to make them visible only in older versions of IE. You can see the specificity in a tooltip for each selector, to help resolve importance issues.
Web Essentials can warn you when you have duplicate CSS properties or selectors, or other issues like overqualified ID selectors or extra units on zeros.
As you type URLs (for images or fonts), Web Essentials will provide IntelliSense for folders and files in your project to help you select an existing file. Once you’ve entered the URL, you can see a preview of the image in a tooltip, and you can convert it to an embedded data URI in a smart tag. You can also
Web Essentials can also automatically generate minified (and optionally also gzipped) copies of each CSS file as you save. You can also drag an image or web font from the Solution Explorer (or even Explorer) to insert a background property or @font-face directive.
You can press Ctrl+Shift+Up/Down to increment/decrement numbers in CSS properties, or to lighten/darken hexadecimal colors.
Visual Studio has a first-class LESS editor, including all of the features from the CSS editor. Web Essentials adds a preview pane on the left side to show the generated CSS, automatic compilation on save, and tooltips to show the generated selector from complex nested LESS selectors:
Web Essentials also adds useful refactoring command to extract repetitive code into a common variable or mixin.
There are a couple of LESS language features that the editor does not support yet (in particular, nested media queries). The LESS syntax tree is part of Visual Studio itself and cannot be fixed by an extension; this will be fixed in an upcoming Visual Studio update.
Even so, Web Essentials does add some features. After installing Web Essentials, you’ll get IntelliSense for strings passed to DOM selector functions like getElementById, getElementsByTagName, and getElementsByClassName (full CSS selector IntelliSense for querySelector or jQuery is not yet possible). Similarly, you’ll get IntelliSense for module names or file paths passed to the Node.js require() function. There is currently no IntelliSense for module names passed to AMD/RequireJS’ require() function. Pull requests are welcome; tweet me for details. On a smaller but still useful note, you’ll get IntelliSense for “use strict”; and “use asm”; for writing modern code.
You can also press F12 on a Visual Studio /// <reference> directive or Node.js require() call to jump to the included source file.
Web Essentials can also automatically run JSHint whenever you save a JS file, including full support for .jshintrc and .jshintignore configuration files. Like CSS, it can also automatically minify and gzip your code on save (and compile CoffeeScript).
ASP.Net 4.5 added a server-side bundling system in Microsoft ASP.NET Web Optimization; see here for more detail. When writing a web application that involves server-side code, this (or similar frameworks like Cassette) is the ideal way to add bundling, it lets the server take care of everything for you automatically.
This will ask you for a filename, then generate an XML file listing all of the files in the bundle:
<?xml version=”1.0“ encoding=”utf-8“?>
<bundle minify=”true“ runOnBuild=”true“ output=”frameworks.js“>
<!—The order of the <file> elements determines the order of them when bundled.–>
Every time you save the bundle file or source files, or build the project, Web Essentials will concatenate and minify (if enabled in the XML) all of the source files and save the output file.
As the auto-generated comment suggests, you can re-order the <file> tags to change the order in which the files will be concatenated, allowing you to ensure that libraries come before their dependencies. You can also drag other source files into the XML.
Note that you should not include .min.js files in a bundle; Web Essentials will minify the whole bundle for you (and you certainly don’t want to include both minified and non-minified versions of a script, or it will run twice). In fact, if you’re using bundles, you should probably delete your minified source files entirely.
Web Essentials includes a number of Browser Link extensions. You can use Design Mode to click any element and edit its text directly in the browser, and have your edits reflected in source code. This is a great way to make minor textual corrections on a website, especially when working together with a copyeditor / proofreader. You can use Inspect Element to click any element in the browser and jump to its source code in the editor, allowing you to easily find out where that HTML came from and make deeper changes.
You can use F12 auto-sync to change CSS properties in your browser’s dev tools, and have them reflected in real time in your CSS (or even LESS!) files in Visual Studio, and in any other browsers connected to Browser Link. This is an extremely powerful feature for making iterative stylistic fixes; it greatly reduces the gap between making your site look exactly the way you want it to via minor tweaks in the dev tools, and rewriting those changes back in the editor after making it perfect.
You can use the Unused CSS detector to audit your CSS selectors and see which ones don’t match anything in the page. For CSS selectors that only apply in some conditions (e.g., validation), you can click Start Recording CSS, interact with your page so that all of the selectors should apply, then go back to Visual Studio. You will see all of the selectors that didn’t match anything in the warning list.
You can see the status of this feature in the Browser Link dropdown button, next to the Run button in the toolbar. If the button is disabled, make sure that your project targets .Net 4.0 or later, and that your Web.config has <compilation debug=”true”>. (To enable it in release mode, add <add key=”vs:enableBrowserLink” value=”true” /> to <appSettings> instead). Browser Link is implemented entirely as normal ASP.Net modules, so it can work with any local ASP.Net server.
From this menu, you can invoke Browser Link extensions, and show the Browser Link dashboard. The dashboard shows all currently connected browsers, and allows you to run extension commands against individual browsers. The dashboard also shows troubleshooting tips to help solve common problems.
Browser Link can also work with static HTML files; you can use it to debug static sites even if .Net is not involved at all. However, you must still use a .Net 4.0+ based server (such as IIS or IIS Express) so that the ASP.Net module can run. You also need to register the module in Web.config so that it runs for static pages; see the documentation.
Many of Web Essentials’ settings (especially CSS / LESS compilation settings) are highly specific to the projects you’re working on. For some projects (e.g., static sites), you may want Web Essentials to compile everything; for others (e.g., larger sites with deployment-time CDN setups), you may want that to be handled elsewhere, and use less.js in the browser for development.
To support this, you can right-click your solution and click Web Essentials, Create Solution Settings. Web Essentials will create a WE2013-settings.xml file in the solution directory, initialized with the current system-wide Web Essentials settings. Once this file exists, Web Essentials will read all of its settings from the file, and the Web Essentials settings pages in Tools, Options will write to the file in your solution instead of the system-wide Web Essentials settings file. You can check the file into source control to make sure that all developers on a team share the same settings.
Unfortunately, this feature only applies to Web Essentials settings; there is still no good way to save (e.g.) C# formatting settings on a per-project basis. However, you can set tabs/spaces settings on a per-project or even per-file/directory basis using EditorConfig and its Visual Studio extension.
About the author