如何在emacs ess中设置自定义自动字符替换?

How to set up custom automatic character replacement in emacs ess?

ess-mode(Emacs 讲统计)的一个有用特性是自动将下划线 _ 替换为赋值运算符 <-。最近,我一直在使用很多管道(写成 %>%),如果不必为每个管道键入三个字符就好了。

是否可以为管道定义自定义键绑定,类似于将 _ 转换为 -> 的键绑定?

查看 yasnippet. You can use it to define something like "if this sequence of characters is followed by this key (which you can define to whatever you like), then replace them with this other sequence of characters and leave the cursor in this place". There's more to yasnippet than this, but there's plenty of documentation online and even already made recipes similar to the example I gave above that you can try, like yasnippet-ess-mode,例如。

或者,您也可以尝试 abbrev-mode 看看是否适合您。

我个人喜欢 yasnippet 更好,因为您还可以指定扩展后光标离开的位置,但是 abbrev-mode 似乎更容易设置。与 Emacs 世界一样,尝试多种解决方案,不要满足于第一个你动手的解决方案。对别人最有效的方法可能对你无效,反之亦然。

最简单的解决方案是只绑定一个键来插入一个字符串:

(define-key ess-mode-map (kbd "|") "%>%")

您仍然可以插入 |C-q |。我不确定地图的名称;您几乎肯定会希望将键绑定限制为 ess-mode.