Skip to content

Instantly share code, notes, and snippets.

@mxl00474
Created December 13, 2020 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxl00474/5289c8ed9e05ba6b535307ee8207042d to your computer and use it in GitHub Desktop.
Save mxl00474/5289c8ed9e05ba6b535307ee8207042d to your computer and use it in GitHub Desktop.
;;
;; An autohotkey script that provides emacs-like keybinding on Windows
;;
#InstallKeybdHook
#UseHook
; The following line is a contribution of NTEmacs wiki http://www49.atwiki.jp/ntemacs/pages/20.html
SetKeyDelay 0
; turns to be 1 when ctrl-x is pressed
is_pre_x = 0
; turns to be 1 when ctrl-space is pressed
is_pre_spc = 0
; turns to be 1 when ctlr-[ is pressed
is_pre_esc = 0
; Applications you want to disable emacs-like keybindings
; (Please comment out applications you don't use)
is_target()
{
IfWinActive,ahk_class ConsoleWindowClass ; Cygwin
Return 1
IfWinActive,ahk_class MEADOW ; Meadow
Return 1
IfWinActive,ahk_class cygwin/x X rl-xterm-XTerm-0
Return 1
IfWinActive,ahk_class MozillaUIWindowClass ; keysnail on Firefox
Return 1
; Avoid VMwareUnity with AutoHotkey
IfWinActive,ahk_class VMwareUnityHostWndClass
Return 1
IfWinActive,ahk_class Vim ; GVIM
Return 1
IfWinActive,ahk_exe Code.exe ; VS Code
Return 1
IfWinActive,ahk_class CASCADIA_HOSTING_WINDOW_CLASS ; Windows Terminal
Return 1
IfWinActive,ahk_class CabinetWClass ; Explorer
Return 1
If is_jupyter() ; Jupyter tab on Chrome
Return 1
; Debug
; WinGetTitle, CurrentWindowTitle, ahk_class Chrome_WidgetWin_1
; MsgBox, % CurrentWindowTitle
Return 0
}
is_onenote()
{
;If WinActive(,"OneNote")
; Return 1
IfWinActive, ahk_class Framework::CFrame
Return 1
Return 0
}
is_jupyter()
{
IfWinActive,ahk_exe chrome.exe ; chrome
{
WinGetTitle, CurrentWindowTitle, ahk_class Chrome_WidgetWin_1
If (CurrentWindowTitle == "JupyterLab - Google Chrome") ; Only deactivate when Jupyter lab tab is open
Return 1
}
Return 0
}
delete_char()
{
Send {Del}
global is_pre_spc = 0
Return
}
delete_backward_char()
{
Send {BS}
global is_pre_spc = 0
Return
}
kill_line()
{
Send {ShiftDown}{END}{SHIFTUP}
Sleep 50 ;[ms] this value depends on your environment
Send ^x
global is_pre_spc = 0
Return
}
open_line()
{
Send {END}{Enter}{Up}
global is_pre_spc = 0
Return
}
quit()
{
Send {ESC}
global is_pre_spc = 0
global is_pre_esc = 0
global is_pre_x = 0
Return
}
newline()
{
Send {Enter}
global is_pre_spc = 0
Return
}
indent_for_tab_command()
{
Send {Tab}
global is_pre_spc = 0
Return
}
newline_and_indent()
{
Send {Enter}{Tab}
global is_pre_spc = 0
Return
}
isearch_forward()
{
Send ^f
global is_pre_spc = 0
Return
}
isearch_backward()
{
Send ^f
global is_pre_spc = 0
Return
}
kill_region()
{
Send ^x
global is_pre_spc = 0
Return
}
kill_ring_save()
{
Send ^c
global is_pre_spc = 0
Return
}
yank()
{
Send ^v
global is_pre_spc = 0
Return
}
undo()
{
Send ^z
global is_pre_spc = 0
global is_pre_x = 0
Return
}
find_file()
{
Send ^o
global is_pre_x = 0
Return
}
save_buffer()
{
Send, ^s
global is_pre_x = 0
Return
}
kill_emacs()
{
Send !{F4}
global is_pre_x = 0
Return
}
move_beginning_of_line()
{
global
if is_pre_spc
Send +{HOME}
Else
Send {HOME}
Return
}
move_end_of_line()
{
global
if is_pre_spc
Send +{END}
Else
Send {END}
Return
}
previous_line()
{
global
if is_pre_spc
Send +{Up}
Else
Send {Up}
Return
}
next_line()
{
global
if is_pre_spc
Send +{Down}
Else
Send {Down}
Return
}
forward_char()
{
global
if is_pre_spc
Send +{Right}
Else
Send {Right}
Return
}
backward_char()
{
global
if is_pre_spc
Send +{Left}
Else
Send {Left}
Return
}
scroll_up()
{
global
if is_pre_spc
Send +{PgUp}
Else
Send {PgUp}
Return
}
scroll_down()
{
global
if is_pre_spc
Send +{PgDn}
Else
Send {PgDn}
Return
}
^x::
If is_target()
Send %A_ThisHotkey%
Else
is_pre_x = 1
Return
^[::
If is_target()
Send %A_ThisHotkey%
Else
is_pre_esc = 1
Return
u::
If is_target()
Send %A_ThisHotkey%
Else
{
If is_pre_x
undo()
Else
send u
}
Return
w::
If is_target()
Send %A_ThisHotkey%
Else
{
If is_pre_esc
{
global is_pre_esc=0
kill_ring_save()
}
Else
send w
}
Return
^f::
If is_target()
Send %A_ThisHotkey%
Else
{
If is_pre_x
find_file()
Else
forward_char()
}
Return
^c::
If is_target()
Send %A_ThisHotkey%
Else
{
If is_pre_x
kill_emacs()
}
Return
^d::
If is_target()
Send %A_ThisHotkey%
Else
delete_char()
Return
^h::
If is_target()
Send %A_ThisHotkey%
Else
delete_backward_char()
Return
^k::
If is_target()
Send %A_ThisHotkey%
Else
kill_line()
Return
;; ^o::
;; If is_target()
;; Send %A_ThisHotkey%
;; Else
;; open_line()
;; Return
^g::
If is_target()
Send %A_ThisHotkey%
Else
quit()
Return
;; ^j::
;; If is_target()
;; Send %A_ThisHotkey%
;; Else
;; newline_and_indent()
;; Return
^m::
If is_target()
Send %A_ThisHotkey%
Else
newline()
Return
;^i::
; If is_target()
; Send %A_ThisHotkey%
; Else
; indent_for_tab_command()
; Return
^s::
If is_target()
Send %A_ThisHotkey%
Else
{
If is_pre_x
save_buffer()
Else
isearch_forward()
}
Return
^r::
If is_target()
Send %A_ThisHotkey%
Else
isearch_backward()
Return
^w::
If is_jupyter()
{
;MsgBox, % CurrentWindowTitle
Send ^l
}
Else If is_target()
Send %A_ThisHotkey%
Else
kill_region()
Return
!w::
If is_target()
Send %A_ThisHotkey%
Else
kill_ring_save()
Return
^y::
If is_target()
Send %A_ThisHotkey%
Else
yank()
Return
^/::
If is_target()
Send %A_ThisHotkey%
Else
undo()
Return
;$^{Space}::
;^vk20sc039::
^vk20::
If is_target()
Send {CtrlDown}{Space}{CtrlUp}
Else
{
If is_pre_spc
is_pre_spc = 0
Else
is_pre_spc = 1
}
Return
^@::
If is_target()
Send %A_ThisHotkey%
Else
{
If is_pre_spc
is_pre_spc = 0
Else
is_pre_spc = 1
}
Return
^a::
If is_target()
Send %A_ThisHotkey%
Else
move_beginning_of_line()
Return
^e::
If is_target()
Send %A_ThisHotkey%
Else
move_end_of_line()
Return
^p::
If is_target()
Send %A_ThisHotkey%
Else If is_onenote()
dllcall("keybd_event", int, 0x26, int, 0, int, 1, int, 0) ;Up
Else
previous_line()
Return
^n::
;
; Ctrl-n is always assign to new line to remove the block by Chrome etc.
;
; If is_target()
; Send %A_ThisHotkey%
; Else If is_onenote()
If is_onenote()
dllcall("keybd_event", int, 0x28, int, 0, int, 1, int, 0) ;Down
Else
next_line()
Return
^b::
If is_target()
Send %A_ThisHotkey%
Else
backward_char()
Return
^v::
If is_target()
Send %A_ThisHotkey%
Else
scroll_down()
Return
!v::
If is_target()
Send %A_ThisHotkey%
Else
scroll_up()
Return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment