I am a massive proponent of streamlining my workflow and I feel one of the biggest improvements I've made in this area is simply navigating my computer. It's something I don't hear about much, in fact I know a few devs that don't even learn the hotkeys that come with Windows or their IDE and are happy to point and click their way through the day. Although there is nothing wrong with this I personally feel that there are massive benefits in shortcuts, hotkeys and macros.
Why would you do this? π€·β
I've fallen in and out of favour with Vim over the last couple of years, having only been developing for 3-4 years Vim keeps sucking back into its black hole. I manage to stay out of it most of the time nowadays but what's really appealing about Vim for me is the mouseless operation, I really took to the idea of not having to move your hands away from the home row.
Apart from looking like a 90's hacker
there are productivity gains in not using the mouse or even the arrow keys.
Prep π
I set out originally only looking for a solution to move my arrow keys to the home row permanently. To do this there were a few problems that had to be solved:
How to implement it (What program?)
How would the keys be activated? (What modifier?)
I had used AutoHotKey in the past but had drastically underestimated the power of this tool by only using it to create the odd macro for repeating tasks at work or for games. After researching other options (AutoIt and Kantu) I decided to go with AutoHotKey as it seemed well documented, mature and I had previous experince.
Small Aside
It's worth noting I did find a handy browser extension that does fulfil some of my requirements (re mapping of arrow keys) but only in chrome. Vimium adds basic vim-like navigation to the browser but unfortunately I couldn't leverage it as it only works in the browser. Definitely worth a look as it's a plugin I still use daily.
The modifier hidden in plain sight π
I had started to address what key would be used to modify the home row to allow for the input of arrow keys in a vim-like style:
Key | Direction |
---|---|
H | Left |
J | Down |
K | Up |
L | Right |
After debating for what seemed like an age if to use CTRL, ALT or WIN/META keys I came across an article which was talking about swapping the escape and CAPSLOCK and there it was... staring me right in the face gathering dust...
Caps Lock!
I started practising hotkeys with the caps under my little digit and it was a pure penny drop moment. It felt so comfortable and natural compared to anything that lives on the bottom row.
Let's be honest, it's probably the most wasteful key on the keyboard:
- It's massive
- The functionality it brings can be achieved by holding shift
- It's used much less than other more useful keys that are harder to reach like ESCAPE and most of the keys on the bottom row
CAPSLOCK === new modifier π‘π‘π‘
This was probably the trickiest part of setting this up. I wanted this new modifier to work well, as a modifier. The issue with this is that there are only a few keys that work as modifiers namely:
- CAPSLOCK
- CTRL
- ALT
- WIN/META
- ALT GR (CTRL + ALT (most of the time))
- SHIFT
We could just swap CTRL and CAPS which isn't a bad deal, you don't have to learn any new combinations/macros and get all the accessibility benefits of using CAPS. The issue with this approach is that I want to make my own macros and it's much easier to start with a blank slate then it is to figure out which commands you might be overwriting in different programs by remapping CTRL + whatever to your own macro.
I attempted to create this functionality in AutoHotKey and after hours of messing around and then the subsequent google session I found the following snippet to do what I wanted:
#Persistent
SetCapsLockState, AlwaysOff
; Caps Lock Disable
capslock::return
; Caps Lock with shift+caps
+Capslock::
If GetKeyState("CapsLock", "T") = 1
SetCapsLockState, AlwaysOff
Else
SetCapsLockState, AlwaysOn
Return
The snippet achieves two things:
- It turns CAPSLOCK off so it doesn't work as CAPSLOCK
- Enables SHIFT + CAPSLOCK to recreate normal CAPSLOCK functionality
THIS IS AMAZING
Not only can I now assign whatever I want to CAPSLOCK + my_desired_key but I can turn caps lock back on if needed with SHIFT + CAPS.
I don't know how to measure how much time I had put into finding a way to "Add" a modifier up until this point, all I can tell you is when I found this my head didn't leave my desk for 10 minutes π€¦ββ As always the simplest solutions prevail!
For example to assign CTRL + c to CAPSLOCK + c it would look like this:
Capslock & c::send, ^c
If you're not familiar with AutoHotKey the part on left-hand side of the '::' is the combo you would like to press. On the right-hand side is the command to execute, so 'send, ^c' means Send keys CTRL + C.
In this context ^ = CTRL see here for a full reference
Putting it all together π₯³
So now we can define custom hotkey combos to do tasks for us this is what it looks like to remap the directional buttons to the home row:
CapsLock & h::Left
CapsLock & j::Down
CapsLock & k::Up
CapsLock & l::Right
Easy, right? So let's take it a bit further. Above the arrow keys, I like to have some of the keys you would typically find in between the main set of keys and the Numpad.
Adding those in is as easy as
Capslock & y::Home
CapsLock & o::End
CapsLock & u::PgDn
CapsLock & i::PgUp
This, in combination with the arrows not only is more comfy but has levelled up my navigation skills in a big way πͺ
While we are on the topic of navigation here are the other useful commands I use for navigating my computer:
Capslock & space::Escape
CapsLock & <::BackSpace
CapsLock & >::Delete
CapsLock & 1::switchDesktopByNumber(1)
CapsLock & 2::switchDesktopByNumber(2)
CapsLock & 3::switchDesktopByNumber(3)
CapsLock & 4::switchDesktopByNumber(4)
CapsLock & 5::switchDesktopByNumber(5)
CapsLock & s::switchDesktopByNumber(CurrentDesktop + 1)
CapsLock & a::switchDesktopByNumber(CurrentDesktop - 1)
See here for the desktop switching function
Wrapping Up π
Now you have the basics of creating your own layer of hotkeys what will you do with it?
If you're interested in learning how to do this on Linux let me know with a comment below and I will show you how I translated this to my Linux rig.
π TLDR: Remap useful keys to a custom layer/new modifier using AutoHotKey
Top comments (5)
Nice article and AHK is indeed a powerful tool. I create full featured guis all the time. I think the name really throws people off making them think it's just a hotkey tool when it is far more than that.
It's funny you mention navigating your computer; my next article is about the basics of computers that most of us take for granted: navigation (various methods), basic hotkeys, where common important folders are (profile, programs, system), etc... Knowing how to actually use a machine makes learning all the rest exponentially easier...
Cheers Ian. We might be stepping on each others toes a little :p I have a few more AHK articles to come. I look forward to your next post as I've only learnt what I've needed to solve the issues I face so I'm sure it will come in handy!
Not toe stepping, can't have too many articles about AHK. Not enough love for it, need to spread it around :D lol. I have whole series planned for AHK, Linux Shell, Python, and other scripting languages. I'm actually building a blog type website to post all my scripting tips and functions/classes etc, but I'll still be posting variations of my writing here.
Too true! I look forward to your series π. I've not hard much experience of Python but it's something I'm looking to get into as I'm told it's the "penknife of programming languages" and I've been looking into a bit of pen testing. Let me know when your site is up, looking forward to it!
Nice idea. I've used it to map a UK English keyboard to give French accented letters and some miscellaneous stuff. The script's available if anyone is interested.