When cursor on one quote symbol, how to jump to the pairing quote?
3 Answers
M-C-f (or M-C-right) bound to forward-sexp
should do that.
I suggest you try all well-known motion commands with the prefix M-C-
instead of C-
.
- M-C-b (or M-C-left) gives
backward-sexp
- M-C-u (or M-C-up) gives
backward-up-list
- M-C-n (or M-C-down) gives
forward-list
-
1
-
@Timm I thought it would be rather obvious that one tries the motion commands with prefix
M-C
instead ofC-
if one knowsM-C-f
. Okay -- I will mention it in the answer.– TobiasSep 22, 2017 at 10:04 -
1It seems that
forward-sexp
andbackward-sexp
stop at whitespaces inside a quoted string?– AhLeungSep 28, 2017 at 6:14
I always have trouble remembering the bindings for forward-sexp
and backward-sexp
, and I wanted something that worked more like %
does in Vim's command mode. At some point, I added this to my config (the docstring says parens but it works for any of sort of bracket or quote), and now I'm satisfied:
;;; PAREN-BOUNCE
;;;; originally ganked from <http://elfs.livejournal.com/1216037.html>
(defun genehack/paren-bounce ()
"Bounce from one paren to the matching paren."
(interactive)
(let ((prev-char (char-to-string (preceding-char)))
(next-char (char-to-string (following-char))))
(cond ((string-match "[[{(<\"']" next-char) (forward-sexp 1))
((string-match "[\]})>\"']" prev-char) (backward-sexp 1))
(t (error "%s" "Not an expression boundary.")))))
;;;; bindings
(global-set-key (kbd "C-%") 'genehack/paren-bounce)
(global-set-key (kbd "C-5") 'genehack/paren-bounce)
I suggest using smartparens package for all such purposes. Short introduction is here: https://ebzzry.io/en/emacs-pairs/ .
-
1Please provide a complete answer in your post. Link-only responses are okay for comments, but not for answers.– Dan ♦Sep 27, 2017 at 14:06
-
sp-beginning-of-sexp
andsp-end-of-sexp
are very close to what I want. I hope that they can be combined into one function (e.g.,sp-matching-sexp
) so that I only need to remember one keybinding. Maybe I will try to write an elisp function. Let me know if anyone did that before. Thanks.– AhLeungSep 28, 2017 at 6:18 -
ebzzry.io/en/emacs-pairs/#keys ("C-M-f" . sp-forward-sexp) ("C-M-b" . sp-backward-sexp) But you can set it different– VictorSep 29, 2017 at 8:51
skip-syntax-forward
would do that.(skip-syntax-forward "^\"")
which can jump to the next double quote symbol, but it doesn't skip escaped symbol, e.g., "string \" cannot be skipped?".