In Praise of AutoHotKey

栏目: IT技术 · 发布时间: 4年前

内容简介:People think it’s weird that I do all my development on a Windows machine. It’s definitely a second-class citizen experience in the wider development world, and Windows has a lot of really frustrating issues, but it’s still my favorite operating system. Th

People think it’s weird that I do all my development on a Windows machine. It’s definitely a second-class citizen experience in the wider development world, and Windows has a lot of really frustrating issues, but it’s still my favorite operating system. This is for exactly one reason: AutoHotKey .

AHK is an engine for mapping keystrokes to scripts. I wouldn’t call it particularly elegant, and it’s filled with tons of redundancy and quirks. Even its fans admit how nasty the language can be. But it hooks into the whole Windows system and makes it easy to augment my workflow. It’s given me a far greater degree of control over my computer than I ever managed to achieve with another OS.

Here’s a short list of some of the scripts I’ve written, in roughly ascending order of complexity.

Mouse shortcuts

Let’s start with a simple one. I use Dragon voice to text for writing as I can speak prose a lot faster than I can type. My microphone is very sensitive and often picks up the sounds of the keyboard, so I need a way to toggle dictation on and off. I wanted to map the toggle button to tapping my mouse wheel left, but Dragon only supports keyboard shortcuts. So I glued the two together with AutoHotKey:

WheelLeft::Send, {NumpadAdd}

The WheelLeft is the shortcut itself. On the right of the :: is the actual command. In this case all it does is send a Numpad + keystroke, which happens to be the “toggle dictation” hotkey in Dragon. It’s a small thing that only took me about a minute to write and makes using Dragon more pleasant. Most my AutoHotKey uses are like this: tacking on a crude API on to something that doesn’t have one.

Hotstrings for math

I write a lot of mathematical notation, such as ∀x ∈ S: P(x) ⇒ Q(x) , for work. Historically people wrote these characters by using a rendering engine like LaTeX or Mathjax, which are cumbersome and not portable. I can’t put arbitrary LaTeX into an email. The alternative to those is writing Unicode. But Unicode symbols are not on standard keyboards and there are a lot of characters to write. Most of the time I’ve seen people just copy and paste the characters they want, one character at a time.

AutoHotKey lets you define hotstrings , which trigger when certain strings are typed. While this can be any kind of command, it’s most often used to replace a short string with a much longer one.

::;shrug::¯\_(ツ)_/¯

I can also use it to map strings to complex Unicode characters. Here’s a sample of my current hotstrings list:

:O:;e::∃
::;in::∈
::;cup::∪
:O:;diamond::◇
; etc

Now if I type ;a x ;in S: ;diamond(f[x] ;geq 0) , I get ∀x ∈ S: ◇(f[x] ≥ 0) . This is a hell of a lot more convenient than copy and paste.

Markdown links

This blog is in Markdown. The syntax for a markdown link is [title](url) . It’s really annoying if I have to copy both the title and the URL from somewhere else. Then I need to do two trips between the source and the markdown text, one to copy each part. Enter AutoHotKey:

#!c:: ; win+alt+c
ctmp := clipboard ; what's currently on the clipboard
clipboard := ""
Send ^c ; copy to clipboard
ClipWait, 2 ; wait for the clipboard to change
clipboard := "[" . clipboard . "](" . ctmp . ")"
Return ; ends a multiline command

If url is on the clipboard and I press win+alt+c while title is the selected text, then the clipboard is replaced with [title](url) . This might seem like a small affordances but it makes adding links so, so much nicer.

Fast Window Switching

alt+tab is annoying because you have to visually confirm you’re on the right app. You can’t use muscle memory here because the stack depends on your recent windows. As a hard-core Vim user, anything that gets in the way of muscle memory is bad and should be avoided. Let’s write a better window switcher in AutoHotKey.

AHK lets you create window groups based on window titles, .exe name, or process ID. I can create a separate window group for each app, and then make a hotkey that specifically cycles through that window group:

GroupAdd, firefox, ahk_exe firefox.exe 

; right alt + 1
>!1:: GroupActivate, firefox, R

Now no matter where I am, pressing rightalt+1 will immediately switch to Firefox. If I’m already in Firefox, it cycles through the active windows. A window can belong to multiple groups, so I can have a special “workshops” key to cycle between lecture notes, slides, and the IDE. I have a separate window group for each of my most commonly used apps, including Neovim, the TLA+ toolbox, and the terminal.

Speaking of commonly used apps:

Remapping the calculator button

  1. Nobody likes the default Windows calculator
  2. If you press the calculator button when the calculator is already open, it just opens a 2nd calculator. Why would anyone want that?

So instead I have it open up Frink . If Frink is already open, it activates it. If it’s already active, it minimizes it. Much better UX.

toggle_app(app, location) 
{
    if WinExist(app)   
    {
        if !WinActive(app)
        {
            WinActivate
        }
        else
        {
            WinMinimize
        }
  }
    else if location != ""
    {
        Run, %location%
    }
}

Launch_App2::toggle_app("Frink", "\Path\to\frink.jar")

I have the same thing set up for Spotify.

Scihubize

sci-hub is a service that lets you access research papers for free. It’s not quuiiiiiiiiite legal, but buying papers is super expensive, like fifty dollars per paper. Not something I can afford, and I think the entire industry is evil anyway.

Normally to use sci-hub you have to copy the blocked paper url, go to sci-hub, paste the link into a form on the website, and click go. It’s a lot more convenient than paying 50 bucks for a paper, but it’s still pretty annoying. If I’m trying to skim a dozen papers, doing that every single time breaks my flow.

Fortunately sci-hub uses a standard format for its links, so I can automate the entire process. This script reopens the current paper in sci-hub:

; Only enable hotkey in Firefox
#IfWinActive ahk_exe firefox.exe

; right alt + right ctrl + s
>!^s::
; wait for both control keys to be lifted
Keywait, RControl
Keywait, RAlt
; go to url bar
SendEvent, ^l

; add prefix, go
SendInput, {left}sci-hub.tw/{enter}
return

I found that after adding this I’ve read a lot more science papers. There’s something about the difference between “a single hotkey” and “four steps” that makes me more likely to bother.

I have something set up similar for the Internet Archive, where I can just press a hotkey to reload the current page in the archive. This is handy for doing any kind of historical research.

I don’t think AutoHotKey or Windows is special in this regard. I could have gotten something similar working on Mac or Linux. But when I tried, I found it much harder. I had to dive into about how the OS worked. I remember trying for two days to get the sign thing working on Linux and eventually had to read up on how the scan codes worked. In the end I decided that the added convenience was just not worth the extra effort. And that was just one system. If I wanted to add my window switcher, I’d have to learn how the window management works.

The real draw is that AutoHotKey makes it all convenient. Each of these scripts takes me just a few minutes to write, no deep dives into Windows internals needed. When adding affordances to my computer is easy, I’m more likely to add them. Using AutoHotKey makes me feel like I can customize my machine. My scripts bring the computer closer to what I need.

The downside is that it’s pretty fragile. I have to tweak delays and sometimes reorganize my computer just to keep my scripts from breaking. I disabled every animation I can to minimize nondeterminism. I wish it was easier to use AutoHotKey if you weren’t a technical expert, so I could give scripts to my friends to help them too. But even with these issues it’s still one of my favorite programs, and still the reason I prefer working on Windows to any other operating system.

Thanks to Lito Nicolai for feedback.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

集体智慧编程

集体智慧编程

TOBY SEGARAN / 莫映、王开福 / 电子工业出版社 / 2009-1 / 59.80元

本书以机器学习与计算统计为主题背景,专门讲述如何挖掘和分析Web上的数据和资源,如何分析用户体验、市场营销、个人品味等诸多信息,并得出有用的结论,通过复杂的算法来从Web网站获取、收集并分析用户的数据和反馈信息,以便创造新的用户价值和商业价值。全书内容翔实,包括协作过滤技术(实现关联产品推荐功能)、集群数据分析(在大规模数据集中发掘相似的数据子集)、搜索引擎核心技术(爬虫、索引、查询引擎、Page......一起来看看 《集体智慧编程》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具