Use C# interactive in Visual Studio to experiment with your code on the fly

I needed to quickly test a regular expression, and the new feature of Visual Studio 2015 came to the rescue: C# Interactive

You will find this under View → Other Windows → C# Interactive

In my case I wanted to test how the Regex.IsMatch() method handled strings that were null or empty. Note that I had to import .NET’s RegularExpressions library.

> string href = null;
> using System.Text.RegularExpressions;
> Regex.IsMatch(href, @"^http://)
System.ArgumentNullException: Value cannot be null.
> href = "";
> Regex.IsMatch(href, @"^http://)
false
> href = "http://www.test.com";
> Regex.IsMatch(href, @"^http://")
true

Visual Studio Code and AutoHotkey

2016-02-10 Update: This post is about how to get around a bug in Visual Studio Code. This bug is now fixed, and my automatic text replacement (described in this blog post), works just as well in Visual Studio Code as in other applications.

In my previous post, Use automatic text replacement to speed up your coding and typing, I explained how I use AutoHotkey on Windows and equivalent software on OS X to speed up my typing and coding.

The idea is to use software to replace some easy-to-hit keys like “->” with a special symbol like “→”.

I use Visual Studio Code quite a lot, and for some reason text replacement with AutoHotkey does not work in VSCode.

I finally found a workaround, which is to have a small delay before the replacement text is being typed. This my list of shortcuts:

Shortcut →  Result
     <<  →  [  (left bracket)
     >>  →  ]  (right bracket)
     ,,  →  {  (left curly brace)
     ;;  →  }  (right curly brace)
     ((  →  «  (left angled quote)
     ))  →  »  (right angled quote)
     --- →  –  (en dash)
     ->  →  →  (arrow)

This is my AutoHotkey.ahk file. I have one setup for VSCode, where I have 100 milliseconds pause before each text insert. For all other applications, I have the same setup as before:

; Shortcuts for Visual Studio Code (with delay before input)
#IfWinActive ahk_exe code.exe
 :?*:<<::{Sleep 100}[
 :?*:>>::{Sleep 100}]
 :?*:,,::{Sleep 100}{{}
 :?*:;;::{Sleep 100}{}}
 :?*:---::{Sleep 100}–
 :?*:((::{Sleep 100}«
 :?*:))::{Sleep 100}»
 :?*:->::{Sleep 100}→

; Shortcuts for all other applications
#IfWinActive
 :?*:<<::[
 :?*:>>::]
 :?*:,,::{{}
 :?*:;;::{}}
 :?*:---::–
 :?*:((::«
 :?*:))::»
 :?*:->::→

Use automatic text replacement to speed up your coding and typing

In programming, and probably in most other lines of work where you work with text on a computer, there are some symbols and special characters that you use a lot, but that are quite difficult to type. Perhaps you need to hit a combination of keys to produce the symbol, or even more time-consuming, you might need to open a menu or tool to insert the symbol.

For me as a programmer, symbols I use all the time, are { and }.

Let us have a look at the symbol {. On a Norwegian Windows keyboard, this is written by hitting ALT+8. On a Norwegian Apple keyboard, the situation is even worse, there the key combination is ALT+SHIFT+8.

Therefore I have started using AutoHotkey for Windows and Keyboard Maestro for Mac to create shortcuts to this and other symbols I use a lot.

My preferred shortcuts

These are the shortcuts I have started using regularly, both on Windows and Mac. When I type the shortcut on the left, the text is automatically replaced with the text on the right. By using AutoHotkey/Keyboard Maestro, the automatic text replacement works in any application.

Shortcut →  Result
     <<  →  [  (left bracket)
     >>  →  ]  (right bracket)
     ,,  →  {  (left curly brace)
     ;;  →  }  (right curly brace)
     ((  →  «  (left angled quote)
     ))  →  »  (right angled quote)
     --- →  –  (en dash)
     ->  →  →  (arrow)

AutoHotkey (Windows)

AHK is a little utility monitoring your keystrokes, and if you type something you have defined as a shortcut, it can do lots of different tasks. You define the shortcuts in an ahk file. My AutoHotkey.ahk looks like this:

:?*:<<::[
:?*:>>::]
:?*:,,::{{}
:?*:;;::{}}
:?*:---::–
:?*:((::«
:?*:))::»
:?*:->::→

AutoHotkey is a bit difficult to set up, because it is very powerful, and can do many tasks beyond simple text replacement.

Keyboard Maestro, TypeIt4Me, aText (OS X)

I have tried many different applications for text replacement for OS X, and the one I like the most is Keyboard Maestro. It is very expensive for a text replacement app (because it can do a lot more than simple text replacement), but it is the only one I have found to work everywhere.

Some very good, and much cheaper, alternatives are aText and TypeIt4Me. They work 99 % of the time, but the 1 % bugged me enough that I ended up purchasing Keyboard Maestro.

I think one of the reasons why I have struggled to get the alternatives to always work, is that I also use a clipboard manager, and most of the text replacement applications work by pasting text from the clipboard.

My setup for TypeIt4Me

TypeIt4Me makes it really simple to setup text replacement.

Screen Shot 2015-12-04 at 22.41.18

My setup for Keyboard Maestro

Keyboard Maestro has a lot more options, but the setup is almost as simple. You have to create one macro for each symbol. You can duplicate macros, so as soon as you have created one, you are able to create many macros pretty quickly.

Screen Shot 2015-12-29 at 20.50.24

Note: The best option is to use the action Type a Keystroke. This does not conflict with clipboard managers.

However, this only works with symbols that you can actually write. When creating shortcuts for symbols you have to copy and paste, like “→”, you have to use the action Insert Text by Pasting or Insert Text by Typing. I have found Insert Text by Pasting to be the most reliable of these two.

Downloads

Download AutoHotkey:
https://www.autohotkey.com/download/

Download TypeIt4Me:
http://www.ettoresoftware.com/products/typeit4me/

Download aText:
https://www.trankynam.com/atext/

Download Keyboard Maestro:
https://www.keyboardmaestro.com/

Live preview of website when editing in Visual Studio Code

VSCode, being extremely lightweight (at least compared to Visual Studio), does not come with its own web server. This can be a bit confusing for those of us used to just hitting F5 in Visual Studio and getting our website launch in a browser.

When using VSCode, you have to rely on the power of Node.js instead.

This is a tutorial of how to start a really small project, containing only a single HTML file in VSCode and edit it with live preview in a browser.

Continue reading Live preview of website when editing in Visual Studio Code

Getting started with TypeScript in Visual Studio Code

This is a super short introduction to start writing TypeScript in Visual Studio Code. This tutorial is written for Windows users, but it should be roughly the same procedure for Linux and OS X users, since all these tools are cross platform.

This post is not actually about writing TypeScript, it’s just a guide to set up VSCode correctly to be able to write and compile TypeScript. Continue reading Getting started with TypeScript in Visual Studio Code

Use Subversion revision number as version number with MSBuild

(And handle revision numbers larger than 65534)

When using a system for continuous integration to automatically deploy your application, it is important to know what version of your code is deployed. This tutorial will show you how to automatically set the version number of the deployed .dll.

In a .NET project, the version number is defined in AssemblyInfo.cs, on the format 1.0.0.0. The numbers are MajorVersion.MinorVersion.Build.Revision. The two latter, build and revision numbers, should be set automatically by your CI system on each deploy.

Content of this tutorial

  • How to create an MSBuild target to automatically insert the revision number on build
  • What to do when your revision numbers are too large to use as .NET revision numbers
  • How to use Bamboo to get the latest revision number from Subversion and use this as an input parameter to MSBuild

Continue reading Use Subversion revision number as version number with MSBuild

How to use Bootstrap or other CSS frameworks on a small part of a page without affecting the rest of the page’s style

I’m currently working on a project where I have an AngularJS app (a form), that will be displayed on several different external websites. This means that I have to style the form to fit into several different page designs, with different existing stylesheets.

The form itself is styled using Bootstrap, one of the most commonly used front-end frameworks.

TL;DR: To style only a selected part of a website using a CSS framework, I create a container div around the app with a unique id, and then use LESS to change all the style definitions in the framework to only affect this particular id. This process is done automatically on build using .NET MVCs bundling framework.

Continue reading How to use Bootstrap or other CSS frameworks on a small part of a page without affecting the rest of the page’s style

AngularJS and .NET MVC: Convert templates from html to javascript and bundle app and templates together

I am working on a project where I have an AngularJS app on my own .NET MVC website, and I will display this app on several remote websites.

TL;DR: To get the AngularJS templates to load correctly on the remote website, I needed to convert the html template files to javascript and bundle them together with the rest of the app. This article will try to explain why and how to achieve this.

Continue reading AngularJS and .NET MVC: Convert templates from html to javascript and bundle app and templates together

SharePoint: Create a content organizer rule with many content type aliases using PowerShell

Why would I want to add many content type aliases to a content organizer rule?

When creating a content organizer rule in SharePoint, the default is that the rule is only valid for a single content type. To get around this limitation, you can add other content type names to the field Aliases (also known as RoutingAliases), displayed as “List of alternate names” in the SharePoint editor.

Continue reading SharePoint: Create a content organizer rule with many content type aliases using PowerShell