1

With Vim's plugin matchit, you can match not only parens, but also a lot more language structures that come in paris (e.g. , if..end, do..end).

Now I'm using Emacs (for fun!), and I wonder if there is something similar to this plugin?

Thanks very much.

5 Answers 5

2

evil-matchit https://github.com/redguardtoo/evil-matchit

Vim matchit.vim by Benji Fisher ported into Emacs.

When it’s installed, you can press “%” to jump between matched tags in Emacs.

Most modern languages are supported:

HTML
Python
Java
C++/C
Javascript
Fortran
SQL
Perl
Latex
CMake
Org-mode (match tag of org-mode and tags of other languages embedded in org file)
Ruby
Bash
Lua
PHP
Vim script
Emacs email (message-mode)
1

I dont know which plugin matches the language structures, but I do use a simple function which matches simply parenthesis.

Okay I haven't written this function, copied from somewhere

;; goto-matching-paren
;; -------------------
;; If point is sitting on a parenthetic character, jump to its match.
;; This matches the standard parenthesis highlighting for determining which
;; one it is sitting on.
;;
(defun goto-matching-paren ()
  "If point is sitting on a parenthetic character, jump to its match."
  (interactive)
  (cond ((looking-at "\\s\(") (forward-list 1))
        ((progn
           (backward-char 1)
           (looking-at "\\s\)")) (forward-char 1) (backward-list 1))))
(define-key global-map [(control ?c) ?p] 'goto-matching-paren) ; Bind to C-c p
1
  • I'm using the evil-mode, which gives this simple paren-matching for free (bind to % as vim does). But what I need is something more extensive. Thanks anyway.
    – Limbo Peng
    Nov 6, 2011 at 18:13
0

I don't think there is a general facility since it depends heavily on the language being edited. For example GAP mode which I have worked on, does have a function emulating vim's % key, but I'm pretty sure that, for example, cc-mode doesn't. Ruby mode is an example of a mode that has functions for going forward and backward across logical blocks like class ... end but I don't think it has a match. It's not very emacsy to have a match, rather a forward and backward is more typical since it's more powerful.

What mode(s) are you interested in?

0

I think typically the mode will define something like what your after (or were after 2 years ago). C-h m (help mode) in a file in the mode you're working in.

For instance, ruby-mode, has "ruby-beginning-of-block" and "ruby-end-of-block" I think the equivalent of these is usually bound to M-C-P (beginning) and M-C-N (end).

You should be able to come up with an evil mode binding that makes sense. If you were really ambitious you could lisp up and call the ruby versions in ruby mode if % failed.

Hope that helps -- or at least helps the next person to come along looking for a similar answer.

0

I use the following function, which I obtained here, for this purpose:

(defun bounce-paren ()
  "Will bounce between matching parens just like % in vi"
  (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 on a paren, brace, or bracket")))))

Of note is that it only supports single-character pairs, and that modifying it to support multi-character delimiters would require essentially an entire rewrite. I have it bound to F2 (and rarely use it), but you could of course just as well bind it to %.

I commend to your attention the Smartparens mode, which supports arbitrary pairs of single- or multi-character delimiters, including cases where both delimiters are the same string. Once you have your pairs configured, a fairly trivial Lisp function, which depending on context calls one of the navigation functions sp-beginning-of-sexp or sp-end-of-sexp, should give you the behavior you desire.

Consider also highlight-parentheses-mode, which colors braces surrounding point; I find that using this mode gives me 99.4% of what I used to get from bouncing between braces, without needing to move point at all.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .