Emulating vi’s % command in Emacs

In vi/vim, The % command will match any parenthesis/bracket for its counterpart. The same thing can be achieved in emacs by one of the following ways:

  1. In all language modes, the following keys work:
       M-C-b          : Goto starting bracket ({, (, < etc.)
       M-C-f          : Goto ending bracket (}, ), > etc.)
       M-C-a          : Goto beginning of function
       M-C-e          : Goto end of function
       
  2. If you want the exact behavior of vi “%” command, matching all parens, use the following elisp code:
    (defun match-parenthesis ()
      "Go to the matching parenthesis if on parenthesis else insert %."
      (interactive)
      (cond ((looking-at "[([{]") (forward-sexp 1) (backward-char))
    		  ((looking-at "[])}]") (forward-char) (backward-sexp 1))
    		  (t (self-insert-command 1))))
    (global-set-key "%" 'match-parenthesis)
        

    It matches the parenthesis if the point is on a parenthesis, otherwise inserts the character ‘%’.