Skip to content

Instantly share code, notes, and snippets.

@jeeger
Created December 6, 2018 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeeger/7163488df77cf2d97e008aeee6dbda5f to your computer and use it in GitHub Desktop.
Save jeeger/7163488df77cf2d97e008aeee6dbda5f to your computer and use it in GitHub Desktop.
Emacs keys for Firefox and some Office products (Word, Outlook, Powerpoint)
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
#SingleInstance force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
markSet = 0
cxPrefix = 0
GroupAdd, EmacsControls, ahk_exe firefox.exe
GroupAdd, EmacsControls, ahk_exe WINWORD.EXE
GroupAdd, EmacsControls, ahk_exe notepad++.exe
GroupAdd, EmacsControls, ahk_exe OUTLOOK.EXE
GroupAdd, EmacsControls, ahk_exe POWERPNT.EXE
GroupAdd, EmacsControls, ahk_exe zotero.exe
#IfWinActive ahk_group EmacsControls
^/::
Send ^z
return
!d::
Send {Shift down}^{Right}{Shift up}^x
Return
!+d::
Send {Shift down}^{Left}{Shift up}^x
return
^q::
Return
^k::
Send {Shift down}{End}{Shift up}^x
Return
^d::
Send {Del}
Return
^j::
Send {Return}
Return
tooltip(text) {
Tooltip %text%
SetTimer RemoveToolTip, 1000
}
;; Prefixing functionality for C-xh, C-xb, C-xC-s,C-xC-c
$^x::
cxPrefix = 1
SetTimer UnsetCx, 500
Return
markSetMovement(command) {
global markSet
if (markSet) {
Send +%command%
} else {
Send %command%
}
}
sendPrefixed(prefixed, unprefixed){
global cxPrefix
if (cxPrefix) {
Send %prefixed%
cxPrefix = 0
} else {
Send %unprefixed%
}
}
;; For sending mark-preserving movement keys when unprefixed, and other non-movement commands when prefixed.
sendPrefixedMovement(prefixed, unprefixed) {
global cxPrefix
if (cxPrefix) {
Send %prefixed%
cxPrefix = 0
} else {
markSetMovement(unprefixed)
}
}
$^s::
;; Save or find.
sendPrefixed("^s", "^f")
return
;; Necessary so I can send C-xC-b to applications without waiting for the C-x timeout (500 ms).
b::
sendPrefixed("^xb", "b")
return
h::
sendPrefixed("^a", "h")
Return
^c::
sendPrefixed("!{F4}", "^c")
Return
^g::
Send {Escape}
Return
k::
;; Kill window
sendPrefixed("^{F4}", "k")
Return
;; Copy+Paste
^Space::
if (markSet) {
tooltip("mark unset")
markSet = 0
} else {
tooltip("mark set")
markSet = 1
}
Return
^y::
global markSet
Send ^v
markSet = 0
Return
^w::
global markSet
Send ^x
markSet = 0
Return
!w::
global markSet
Send ^c
markSet = 0
Return
;; Movement keys
^n::
markSetMovement("{Down}")
Return
^v::
markSetMovement("{PgDn}")
Return
!v::
markSetMovement("{PgUp}")
Return
^p::
markSetMovement("{Up}")
Return
^b::
global cxPrefix
global markSet
if (cxPrefix) {
;; Bold for Word/Powerpoint
Send ^b
cxPrefix = 0
markSet = 0
} else {
markSetMovement("{Left}")
}
Return
^f::
;; "Open file" on "C-x C-f"
sendPrefixedMovement("^o", "{Right}")
Return
^a::
markSetMovement("{Home}")
Return
^e::
markSetMovement("{End}")
Return
!b::
markSetMovement("^{Left}")
Return
!f::
markSetMovement("^{Right}")
Return
!<::
markSetMovement("^{Home}")
Return
!>::
markSetMovement("^{End}")
Return
!}::
markSetMovement("^{Down}")
Return
!{::
markSetMovement("^{Up}")
Return
;; Editing commands
wordCase(type) {
;; Save clipboard
OldClipboard := ClipboardAll
clipboard =
;; Select word and cut
Send +^{Right}
Send ^c
;; Lowercase/uppercase/titlecase text in clipboard
ClipWait, 10
cased :=
if (type = "lower")
{
StringLower, cased, clipboard
}
else if (type = "title")
{
StringLower, cased, clipboard, T
}
else if (type = "upper") {
StringUpper, cased, clipboard
}
Send %cased%
;; Reset clipboard
clipboard := OldClipboard
;; Free clipboard memory
OldClipboard =
}
!l::
wordCase("lower")
Return
!c::
wordCase("title")
Return
!u::
wordCase("upper")
Return
;; Unfortunately, this overlaps with Firefox' "New Tab" key.
;; Transpose two letters
;; ^t::
;; Send +{Right}
;; Send ^x
;; ClipWait, 10
;; Send {Left}
;; Send ^v
;; Return
;; Transpose two words
!t::
;; Beginning of current word.
Send ^{Right}^{Left}
Send ^+{Right}
Send ^x
ClipWait, 10
Send ^{Left}
Send ^v
Send {Space}
Return
!%::
Send ^h
Return
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return
UnsetCx:
if (cxPrefix) {
Send ^x
}
cxPrefix = 0
Return
;; Miscellaneous for compatibility with Word & Powerpoint
;; Unset region on bold, italic and underlined.
^i::
global cxPrefix
global markSet
if (cxPrefix) {
send ^i
markSet = 0
cxPrefix = 0
}
Return
^u::
global cxPrefix
global markSet
if (cxPrefix) {
send ^u
markSet = 0
}
Return
#IfWinActive
;; End of Emacs keys
#s::
if (WinExist("ahk_class mintty")) {
WinActivate
}
Return
#e::
if (WinExist("ahk_class Emacs")) {
WinActivate
}
Return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment