In many programming languages, there are more curly braces {} required than square brackets []. Yet, standard keyboards make the brackets easier to press. So, if that’s the sort of thing you are into and want to fix, then this article first explains how to do it, then after that will be some words about the code and why.

Using Windows, I’ll explain how to achieving this goal using AutoHotkey. (Sidenote: The AutoHotkey site already explains pros and cons of this remap method and using registry tweaks method well.)

How to switch braces {} keys and brackets [] keys using AutoHotkey

Prerequisites for this walkthrough:

  • Download and install AutoHotkey
  • Keyboard with curly braces on the same key as the bracket, so “shift+bracket”.

Get started

  1. Create a new file: swap-bracket-and-brace.ahk
  2. Add the following code to that file:

     ; Source: http://superuser.com/questions/425873/replace-with-and-with-using-autohotkey
     *$[::
         if (GetKeyState("Shift"))
             SendInput, {[ Down}
         else
             SendInput, {{ Down}
         return
        
     *$]::
         if (GetKeyState("Shift"))
             SendInput, {] Down}
         else
             SendInput, {} Down}
         return
        
     *$[ Up::
         if (GetKeyState("Shift"))
             SendInput, {[ Up}
         else
             SendInput, {{ Up}
         return
        
     *$] Up::
         if (GetKeyState("Shift"))
             SendInput, {] Up}
         else
             SendInput, {} Up}
         return
    
  3. Save and run the script. Test. Done.

About the code

My first attempt at creating a script for swapping the keys looked a lot simpler than the above code:

; Doesn't work. Only remaps bracket to braces, not vice-versa.
[::{
]::}
{::[
}::]

Ideally, that could have been all that was required, but due to serial intricacies of standard keyboards more work/code is required on our part. Basically, the keyboard doesn’t send the curly brace key code. Instead, it sends serially a “shift down”, then “bracket”, then “shift up”. So, the working code checks for that shift state.

Why?

As I build my personal programming language (an ongoing side-side-hobby), I question and challenge all aspects of the process, assumptions, and requirements. One of the artificial requirements I made for my language was ease/flow of typing and less of it. So, one way to do that I examined was minimizing the amount the shift key had to be pressed. (Around this time I started preferring = over :, in addition to ' over ".) So, my language uses brackets as block identifiers instead of braces or parentheses like other languages, due to the standard keyboard design.

And, as an experiment, I wanted to test this possible coding efficiency by changing around the square brackets and curly braces keys for other areas that I typically code in.

Result: I feel that typing has a better flow when the key presses I make do something rather than just being a meta key. Sure, the efficiency and time gained is very little for each individual key, but added up is a lot. And, it really does feel better not having to use the shift key, I want to stress.

Having said that, when I use JetBrain’s IntelliJ projects, I typically let the IDE auto-generate the matching braces required for methods, conditions, classes, and loops by using the shortcut ctrl+shift+enter. So, in IntelliJ, I don’t even need the curly brace keys. Though, not all IDEs or text editors are able to auto-insert both braces. So, by swapping the keys around, typing code feels more fluid.

One other note.. I don’t just write code a lot. Javadoc documentation requires a lot more curly braces. And, one place that I would miss the simpler bracket keys is when writing in Markdown and adding links, which I also do a lot of (including this post). I believe AHK can also work per-application, but I haven’t looked into that yet.