retrot
retrot

Reputation: 331

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 ->?

Upvotes: 0

Views: 295

Answers (2)

jpkotta
jpkotta

Reputation: 9437

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.

Upvotes: 2

mihai
mihai

Reputation: 4742

Check out 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, for example.

Alternatively, you can also try abbrev-mode and see if that works for you.

I, for one, like yasnippet better, since you can also specify where to leave the cursor after the expansion, but abbrev-mode seems to be easier to set up. As always in Emacs world, try multiple solutions, don't settle for the first one you put your hands on. What works best for others might not work for you, and vice-versa.

Upvotes: 0

Related Questions