What’s in my AutoHotKey file

I was asked awhile back to blog about what is in my AutoHotKey script file. If you don’t know what AutoHotKey is, it allows you to remap keys on your keyboard or create macros that are associated with keystrokes or key combinations. So here’s what I got.

#SingleInstance

#SingleInstance says that if I load up my AutoHotKey script and there is already one loaded, then just use the new one without prompting. If you make changes to your script file, you can double click the file to reload it. This way you won’t get annoying prompts when you do so. This has to be the first line in your AHK file if you use it.

SetKeyDelay, -1

By default, AHK puts a slight delay between each keystroke in a macro. I have no idea why you would normally want this, so the above line removes that delay and it will execute the keystrokes as fast as possible.

CapsLock::
Send {End}
return

Remaps Caps Lock to End. The Caps Lock key is in such a prime location in the home row, and you don’t usually need to use it. So I remap it to End, which I use a lot when I want to go to the end of a line of code. This way, I don’t have to take my hands off the home row and stab at the End key (and often miss). This was my first AutoHotKey shortcut and it still my favorite.

+Capslock::Capslock

Shift-Caps Lock will now toggle Caps Lock. Because sometimes you actually do need Caps Lock.

#j::run explorer c:proj

Windows+J opens the folder where I keep all of my code. The “run” keyword allows you execute whatever you want.

!^Up::
Send {Up}{Up}{Up}{Up}{Up}
return

!^Down::
Send {Down}{Down}{Down}{Down}{Down}
return

!^Left::
Send {Left}{Left}{Left}{Left}{Left}
return

!^Right::
Send {Right}{Right}{Right}{Right}{Right}
return

When I hit Ctrl-Alt-arrow key, it will go 5 characters/lines in that direction. This really helps you move around faster in a code file. Ctrl-left arrow or Ctrl-right arrow will go to the beginning/end of the current word, which is nice, but sometimes you want to go to a spot in the middle of a long word, so the Ctrl-arrow doesn’t really help. This is another favorite of mine.

:*:ssf]::
SendEvent select * from `
return

When I type “ssf]”, it will change it to “select * from “. The tick at the end of the second line tells AHK that the space at the end of the line is important and should be included.

:*:ietable]::
SendEvent if exists (select 1 from sysobjects where name = 'TableName' and xtype = 'U'){Enter} drop table [dbo].[TableName]{Enter}{Backspace}go
return

:*:ieview]::
SendEvent if exists (select 1 from sysobjects where name = 'ViewName' and xtype = 'V'){Enter} drop view [dbo].[ViewName]{Enter}{Backspace}go
return

:*:ietrigger]::
SendEvent if exists (select 1 from sysobjects where name = 'TriggerName' and xtype = 'TR'){Enter} drop trigger dbo.TriggerName{Enter}{Backspace}go
return

:*:ieproc]::
SendEvent if exists (select 1 from sysobjects where name = 'ProcName' and xtype = 'P'){Enter} drop procedure [dbo].[ProcName]{Enter}{Backspace}go
return

:*:newproc]::
SendEvent if exists (select 1 from sysobjects where name = 'ProcName' and xtype = 'P'){Enter} drop procedure [dbo].[ProcName]{Enter}{Backspace}go{Enter}{Enter}create procedure dbo.ProcName{Enter}({Enter}{Enter}){Enter}as{Enter}begin{Enter}{Enter}end{Enter}go{Enter}{Enter}grant execute on dbo.ProcName to EdwardsSteelUser{Enter}go
return

Prints out the following code when I enter “ietable]”:

if exists (select 1 from sysobjects where name = ‘TableName’ and xtype = ‘U’)
drop table [dbo].[TableName] go

I put this at the top of the file every time I create a new database table. Also included are similar macros for views, triggers, functions, and stored procedures.

:*:se]::
SendEvent ShouldEqual(
return

When I write tests, I’m often writing things like “employee.Name.ShouldEqual(“Jon”);”. When I’m typing this stuff, I can just type “se]” and get “ShouldEqual(“. Sure, Intellisense could do it for me, but AHK is a lot faster than waiting for Intellisense to figure out what I’m doing.

:*:c:|::
SendEvent c:
return

Because I often screw up when typing “c:” and type “c:|” instead. You can do similar things for common misspellings that you often find yourself typing.

I have some other project-specific macros in my AHK file too. If you find yourself typing the same class name, database table name, or anything else over and over, create a macro for it. It’s a small thing but it will help you go a lot faster.

I wanted a way to add stuff to my file quickly because I knew that I would be more likely to do it if it were easier. This led to the AutoHotKeyShortcutAppender, which is a small console app that takes in two arguments: the key combination for a macro and the output. Now I can hit Windows-Q to bring up SlickRun and type “asc scn] SomeClassName” and instantly it adds the macro to the end of my AHK file and reloads it. (If you use the code, you have to edit the app.config file and set the path to your AHK file.)

Have any other AHK favorites? Please post a comment and let me know!