remacsess

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


One of the useful features of ess-mode (Emacs speaks statistics) is to automatically replace the underscore _ with the assignment operator <-. Lately, I have been using a lot of pipes (written as %>%) and it would be great to not have to type three characters for each pipe.

Is it possible to define a custom key binding for the pipe, similar to the one converting _ into ->?


Solution

  • The simplest solution is to just bind a key to insert a string:

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

    You can still insert | with C-q |. I'm not sure about the map's name; you'll almost certainly want to limit the key binding to ess-mode.