10

When cursor on one quote symbol, how to jump to the pairing quote?

2
  • 1
    I don't have Emacs now to try, but I'd imagine skip-syntax-forward would do that.
    – wvxvw
    Sep 22, 2017 at 7:17
  • 1
    I tried (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?".
    – AhLeung
    Sep 28, 2017 at 6:10

3 Answers 3

9

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
3
  • 1
    You should also mention backward-sexp bound to M-C-b.
    – Timm
    Sep 22, 2017 at 10:01
  • @Timm I thought it would be rather obvious that one tries the motion commands with prefix M-C instead of C- if one knows M-C-f. Okay -- I will mention it in the answer.
    – Tobias
    Sep 22, 2017 at 10:04
  • 1
    It seems that forward-sexp and backward-sexp stop at whitespaces inside a quoted string?
    – AhLeung
    Sep 28, 2017 at 6:14
2

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)
0

I suggest using smartparens package for all such purposes. Short introduction is here: https://ebzzry.io/en/emacs-pairs/ .

3
  • 1
    Please 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 and sp-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.
    – AhLeung
    Sep 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
    – Victor
    Sep 29, 2017 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.