ShannonMax:一个利用信息论优化Emacs键位绑定的库
ShannonMax: A Library to Optimize Emacs Keybindings with Information Theory

原始链接: https://github.com/sstraust/shannonmax

## ShannonMax:优化你的 Emacs 键绑定 ShannonMax 是一款利用信息论分析你的 Emacs 使用情况并建议改进键绑定的工具,以提高效率。它会识别出可以通过更短的键序列显著减少输入工作量的命令,并标记那些使用频率低的命令,这些命令的绑定可能过于简短,可以考虑取消绑定。 **工作原理:** ShannonMax 会记录你的按键操作,然后根据每个命令的使用频率计算其键绑定的“理论长度”。常用命令应该有更短的绑定。 **安装需要:** Java/JVM,下载 `shannon-max.el` 和一个 `.jar` 文件,将目录添加到你的 Emacs 加载路径,并通过 `(shannon-max-start-logger)` 启动按键记录器。分析可以通过 `M-x shannon-max-analyze` 运行。 **主要功能包括:** 可定制的按键成本,命令过滤(例如 self-insert),以及全局重新绑定的选项(Emacs 29+)。该项目正在积极开发中,计划包括更简单的安装、改进的 UI 以及更好的主模式支持。 **注意:** 使用按键记录器时,请注意记录敏感信息。 **在此处查找:** [https://github.com/sstraust/shannonmax](https://github.com/sstraust/shannonmax) & [https://www.youtube.com/watch?v=MytPttbIUOY](https://www.youtube.com/watch?v=MytPttbIUOY)

黑客新闻 新的 | 过去的 | 评论 | 提问 | 展示 | 工作 | 提交 登录 ShannonMax:一个使用信息论优化 Emacs 键绑定的库 (github.com/sstraust) 4 分,sammy0910 1 小时前 | 隐藏 | 过去的 | 收藏 | 1 条评论 帮助 noosphr 3 分钟前 [–] 我曾经做过类似的事情,关于手指拉伤和键盘上的按键位置。问题是不同的语言具有截然不同的分布,以至于 C 和 Lisp 需要完全不同的键盘布局。最终,C 和 Lisp 键盘之间的差异高于它们与 QWERTY 之间的差异,所以那是一个有趣但无果而终的项目。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

ShannonMax uses information theory to analyze your emacs usage and suggest better keybindings.

In particular, it helps you find commands where changing the keybinding can significantly reduce the amount you type.

Example Results

WARNING: When using a keylogger, be careful not to log any sensitive information!

Cite This:
Straus, S. (2024). Better Keybindings with Information Theory [Computer software]. https://github.com/sstraust/shannonmax

Original Talk: https://www.youtube.com/watch?v=MytPttbIUOY

  1. Download the files shannon-max.el, and target/emacskeys-0.1.0-SNAPSHOT-standalone.jar. Put them in the same directory, somewhere in your emacs path. So the structure should look like:

    ~/.emacs.d/custom/shannon-max.el
    ~/.emacs.d/custom/target/emacskeys-0.1.0-SNAPSHOT-standalone.jar
    

    And you should have something in your .emacs that looks like this:

    (add-to-list 'load-path "~/.emacs.d/custom")
    (require 'shannon-max)
    (setq shannon-max-jar-file
      (expand-file-name "~/.emacs.d/custom/target/emacskeys-0.1.0-SNAPSHOT-standalone.jar"))
    
    

    You'll also need to have java/the jvm available on your machine and chmod +x the jar file to make it executable.

  2. Start collecting data.

    Add (shannon-max-start-logger) to your .emacs configuration file.

    After a few minutes, check the contents of ~/emacs-logged-keys to verify the logger is working correctly.

  3. Once you have enough data, call M-x shannon-max-analyze to see the results!

At any time, you can call M-x shannon-max-analyze to see the results.

In the results buffer:
C-c C-n to scroll down a page of results
C-c C-p to scroll up a page of results
C-c C-e to call keymap-global-set to globally bind a keysequence (emacs version 29 only)

Shannonmax compares the actual length of your keybinding (e.g. "x e" has length 2), to the theoretical length of your keybinding (how long it should be, given how much you actually use it). We use information theory on the logged data to determine a keybinding's theoretical length.

Commands you use more often should have shorter keybindings, so in general, the strategy is to rebind "Keybindings that are too long" to something that's shorter and more convenient. If you run out of keys on your keyboard, you can free up space by unbinding infrequently used "Keybindings that are too short".

You can learn more about it by watching the video here: https://www.youtube.com/watch?v=MytPttbIUOY

shannon-max-custom-keypress-cost

By default, shannon-max assumes every keypress costs 1. Sequences with control characters are given an additional cost of 1 per control character.

So for example:

  • "a" Has a cost of 1
  • "a b" Has a cost of 2
  • "C-a" Has a cost of 2
  • "C-M-a" Has a cost of 3
  • "C-x C-s" Has a cost of 4

If you have a different view of the world, you can write your own cost function.

If you create your own cost function, you'll also need to modify alphabet-size to the corresponding value.

shannon-max-alphabet-size

This represents "How much can I type with a single keypress?". It's used to compute the theoretical length of a given key command.

For example, if you only have two keys on your keyboard, then in theory your keybindings must be very long, if you are to represent every possible command with just 2 keys. But if you have 100 keys on your keyboard, your keybindings can be very short.

By default, we assume you have 52 keys on your keyboard, and every keypress costs 1.

If you change your cost function, you need to calculate the correct alphabet size for your new cost-weighted "keyboard". The right way to do this is by solving the characteristic function as described here: https://people.math.harvard.edu/~ctm/home/text/others/shannon/entropy/entropy.pdf (page 3). I want to make this calculation more automatic, and it's planned for future work.

If you're too lazy to calculate it out you can fiddle with the value until it seems right, and you should still get somewhat useful/actionable results.

shannon-max-filtered-commands

A list of emacs commands to ignore from the output. By default we also filter all commands matching "lambda", "(", or "[". This is particularly useful for removing things like the self insert command.

shannon-max-filter-commands-fn

Basically does the same thing as shannon-max-filtered-commands, but as a function.

Certain packages mess with the keylogger's ability to correctly log commands. In particular, shannonmax uses real-last-command and this-command-keys-vector inside of post-command-hook in order to determine what keys/commands to log. Certain packages (like ido-mode) interfere with our ability to correctly set these values. We use post(rather than pre)-command-hook so that logged keys are correctly set in god-mode.

If you have feedback on how to get around this, I'd love your thoughts and advice!

Better Support For Major Modes

Certain keybindings are only active in major/minor modes. Ideally we'd compute separate keymaps for every major minor mode, but it's tricky/takes a lot of thought and care to handle this correctly in a way that's relevant and useful.

Support for Multi-Command Codings/Arithmetic Codings

To get really optimal output, we'd like to understand how pairs or sequences of commonly used emacs commands can be folded into a single keybinding

  • Easier Installation on MELPA
  • Better Custom UI menus for rebinding keys (rather than keymap-global-set)
  • Better support for capitalized letters

Any other thoughts? Feel free to open an issue on this Repo :D

联系我们 contact @ memedata.com