Ran into the same issue. The solution I found is to:
- Use autopair, which does exactly what you want. Make sure you install it.
- Enable
ruby-electric-mode
but only for |
because the rest is already taken care of.
This leads to the following code in your .emacs
file:
(use-package autopair
:config (autopair-global-mode)
)
(use-package ruby-electric-mode
:init
(setq ruby-electric-expand-delimiters-list (quote (124)))
)
(add-hook ruby-mode-hook ruby-electric-mode)
This code uses use-package package, make sure you installed it (M-X list-packages
, then find use-package
, then i
on the line, then x
and restart emacs).
Also, that might interest people visiting this thread. I added this code to skip closing delimiters with TAB
, it helps jumping over them. Comment out the while
lines (and adjust )
) to have a single TAB
jump over all closing delimiters (taken from emacs board discussion):
(use-package bind-key)
(defun special-tab ()
"Wrapper for tab key invocation.
If point is just before a close delimiter, skip forward until
there is no closed delimiter in front of point. Otherwise, invoke
normal tab command for current mode.
Must be bound to <tab> using bind-key* macro from bind-key package.
Note, this function will not be called if `override-global-mode is
turned off."
(interactive)
(defun next-char-delims (delims)
(let ((char (and (not (equal (point) (point-max)))
(string (char-after (point))))))
(when char (member char delims))))
(let ((open (" " """ ")" "]" "}" "|")))
(if (next-char-delims open)
(progn (forward-char 1))
;;(while (next-char-delims open)
;; (forward-char 1)))
(call-interactively (key-binding (kbd "TAB"))))))
(if (macrop bind-key*)
(bind-key* (kbd "<tab>") special-tab)
(user-error "Must have bind-key from use-package.el to use special-tab function"))
This time, you need the bind-key
package for this snippet to work.