AHK模拟emacs快捷键

182 阅读3分钟

个人很喜欢emacs的这套快捷键,mac默认也是这套。在windows使用 AutoHotKey 来模拟。

安装好 AHK 主程序后,将下面的脚本下载下来,右键,启动脚本即可。

Emacs 风格的光标移动
Ctrl+Space 选取文本

c+a: 光标首行
c+e: 光标尾行
c+p: 光标下一行
c+n: 光标上一行
c+b: 光标后退一个字母
c+f: 光标前进一个字母

c+d: 删除后一个单词
c+h: 删除前一个单词
c+k: 删除光标到最后一个单词

alt+f: 光标移动到下个单词
alt+b: 光标移动到上个单词
alt+h:向左删除单词
alt+d:向右删除单词

capslock 代替 ctrl
c: Ctrl

AHK 脚本参考这里 emacs.ahk

; docs  https://ahkcn.github.io/docs/misc/Clipboard.htm
; AHK 脚本参考这里: https://github.com/shiman/dotfiles/blob/master/ahk/emacs.ahk

; WIN   的符号是  #
; Ctrl  的符号是  ^
; Alt   的符号是  !
; Shift 的符号是  +

; CapsLock 映射 Ctrl 键
CapsLock::Ctrl

; 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

; Applications you want to disable emacs-like keybindings
; (Please comment out applications you don't use)
is_target()
{
  IfWinActive,ahk_exe Explorer.exe
    Return 1
  IfWinActive,ahk_exe WindowsTerminal.exe
    Return 1
  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
    Return 1
  IfWinActive,ahk_class mintty
    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
}

delete_word()
{
  Send ^{Del}
  global is_pre_spc = 0
  Return
}
delete_backward_word()
{
  Send ^{Backspace}
  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 {HOME}{Enter}{Up}
  global is_pre_spc = 0
  Return
}
quit()
{
  Send {Right}
  global is_pre_spc = 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
  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
}

forward_word()
{
  global
  if is_pre_spc
    Send +^{Right}
  Else
    Send ^{Right}
  Return
}

backward_word()
{
  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
}

;$^{Space}::
;^vk20sc039::
^vk20::
   If is_pre_spc
      is_pre_spc = 0
    Else
      is_pre_spc = 1
  Return

^f::
   forward_char()
  Return  

^d::
    delete_char()
  Return

^h::
    delete_backward_char()
  Return

^k::
    kill_line()
  Return

^a::
    move_beginning_of_line()
  Return

^e::
    move_end_of_line()
  Return

^p::
    previous_line()
  Return

^n::
    next_line()
  Return

^b::
    backward_char()
  Return

; 光标移动到下个单词
!f::
    forward_word()
return

; 光标移动到上个单词
!b::
    backward_word()
return

; 向左删除单词
!h::
    delete_backward_word()
return

; 向右删除单词
!d::
    delete_word()
return