开发工具 让你的 OSX 拥有完整的 Emacs Binding.

ruohanc · 2013年07月13日 · 最后由 ruohanc 回复于 2013年08月26日 · 9310 次阅读

搜了半天终于找到,找这个的原因是我打算试试看 Sublime Text, 而在这之前我一直用的是 Vim. 在尝试使用 Sublime Text 的时候总觉的不能快速的 page up 和 page down 十分的不爽。

OSX 拥有一部分 Emacs Binding: c-f, c-h, c-n ,c-b, 但是以 meta 键开头的 binding 不知道为啥变成了 œ∑†¥®øπåƒ√ 这些奇怪字符。结果就变成了可以用 c-v 下翻一页,但是却不能用 m-f 上翻一页,让我十分纠结。今天下决心找了一下如何在 OSX 下搞定完整的 Binding, 终于找到了 superUser 上的这个答案。

原理上原帖看吧,这里把代码复制一下:

mkdir ~/Library/KeyBindings
cat > ~/Library/KeyBindings/DefaultKeyBinding.dict <<EOF
{
/* Keybindings for emacs emulation.  Compiled by Jacob Rus.
 *
 * This is a pretty good set, especially considering that many emacs bindings
 * such as C-o, C-a, C-e, C-k, C-y, C-v, C-f, C-b, C-p, C-n, C-t, and
 * perhaps a few more, are already built into the system.
 *
 * BEWARE:
 * This file uses the Option key as a meta key.  This has the side-effect
 * of overriding Mac OS keybindings for the option key, which generally
 * make common symbols and non-english letters.
 */

    /* Ctrl shortcuts */
    "^l"        = "centerSelectionInVisibleArea:";  /* C-l          Recenter */
    "^/"        = "undo:";                          /* C-/          Undo */
    "^_"        = "undo:";                          /* C-_          Undo */
    "^ "        = "setMark:";                       /* C-Spc        Set mark */
    "^\@"       = "setMark:";                       /* C-@          Set mark */
    "^w"        = "deleteToMark:";                  /* C-w          Delete to mark */


    /* Incremental search. */
/* Uncomment these lines If Incremental Search IM is installed */
/*  "^s"        = "ISIM_incrementalSearch:";        /* C-s          Incremental search */
/*  "^r"        = "ISIM_reverseIncrementalSearch:"; /* C-r          Reverse incremental search */
/*  "^g"        = "abort:";                         /* C-g          Abort */


    /* Meta shortcuts */
    "~f"        = "moveWordForward:";               /* M-f          Move forward word */
    "~b"        = "moveWordBackward:";              /* M-b          Move backward word */
    "~<"        = "moveToBeginningOfDocument:";     /* M-<          Move to beginning of document */
    "~>"        = "moveToEndOfDocument:";           /* M->          Move to end of document */
    "~v"        = "pageUp:";                        /* M-v          Page Up */
    "~/"        = "complete:";                      /* M-/          Complete */
    "~c"        = ( "capitalizeWord:",              /* M-c          Capitalize */
                    "moveForward:",
                    "moveForward:");                                
    "~u"        = ( "uppercaseWord:",               /* M-u          Uppercase */
                    "moveForward:",
                    "moveForward:");
    "~l"        = ( "lowercaseWord:",               /* M-l          Lowercase */
                    "moveForward:",
                    "moveForward:");
    "~d"        = "deleteWordForward:";             /* M-d          Delete word forward */
    "^~h"       = "deleteWordBackward:";            /* M-C-h        Delete word backward */
    "~\U007F"   = "deleteWordBackward:";            /* M-Bksp       Delete word backward */
    "~t"        = "transposeWords:";                /* M-t          Transpose words */
    "~\@"       = ( "setMark:",                     /* M-@          Mark word */
                    "moveWordForward:",
                    "swapWithMark");
    "~h"        = ( "setMark:",                     /* M-h          Mark paragraph */
                    "moveToEndOfParagraph:",
                    "swapWithMark");

    /* C-x shortcuts */
    "^x" = {
        "u"     = "undo:";                          /* C-x u        Undo */
        "k"     = "performClose:";                  /* C-x k        Close */
        "^f"    = "openDocument:";                  /* C-x C-f      Open (find file) */
        "^x"    = "swapWithMark:";                  /* C-x C-x      Swap with mark */
        "^m"    = "selectToMark:";                  /* C-x C-m      Select to mark*/
        "^s"    = "saveDocument:";                  /* C-x C-s      Save */
        "^w"    = "saveDocumentAs:";                /* C-x C-w      Save as */
    };

}
EOF

PS: 修改完配置后要重启应用才能生效,比如你想在 subl 里面生效那就要重启 subl

原来 NSTextView 原生支持二段快捷键...

果断收藏。

收藏,感谢分享

#1 楼 @luikore 哥你的槽点我又没看懂...

把差不多所有 Option + 字母的组合都拿来做全局快捷键了,今天看了这个,又折腾着空出了 f, b, v。其它用得不多就不绑定了。普通键盘布局,Option 都很难按,不大适用于常用快捷键。

#1 楼 @luikore Mac OS X 本身要是能支持多段快捷键就好了

#4 楼 @ruohanc 这个 keymap 是把快捷键和文本输入框的 action (objc 里的 method) 联系起来,对应的控件就是 NSTextView ...

果断 mark 谢谢

#6 楼 @doitian 你试试输入 option+u,再按 a 貌似是支持多阶段快捷键的?

#9 楼 @steveLTN ascent 是特例了,比如快捷键设置的时候没法用多段的快捷键

虽然很多东西都没有(如 register、kmacro、compose-mailtranspose-chars等),但还是蛮实用的,thx~

#11 楼 @xuefuqiao transpose-chars 默认就是 c-t,原生就有的

#12 楼 @ranmocy 我这里怎么不管用……OS X 10.8.4。

#13 楼 @xuefuqiao 奇怪,不过我是 10.9

NSTextView 很强大的,原生支持列选择和多重选择,可惜没有多重光标,我也是最近写一个 Xcode 的插件才发现的。

后来使用中发现一个问题,比如上面占用了 之后, 这个快捷键 (paste without style) 就用不了了....只得还原了..

需要 登录 后方可回复, 如果你还没有账号请 注册新账号