POST
Emulate vi's % command (jump to matching parenthesis) in Emacs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; One of my recent favorite pieces of Emacs configuration. The % command was one of the things I missed the | |
; most in Emacs, until I found this little gem. I modified it from its original source by adding | |
; previous-line and next-line to the list of commands. | |
; From http://www.emacswiki.org/emacs/ParenthesisMatching#toc4 | |
(defun goto-match-paren (arg) | |
"Go to the matching parenthesis if on parenthesis AND last command is a movement command, otherwise insert %. | |
vi style of % jumping to matching brace." | |
(interactive "p") | |
(message "%s" last-command) | |
(if (not (memq last-command '( | |
set-mark | |
cua-set-mark | |
goto-match-paren | |
down-list | |
up-list | |
end-of-defun | |
beginning-of-defun | |
backward-sexp | |
forward-sexp | |
backward-up-list | |
forward-paragraph | |
backward-paragraph | |
end-of-buffer | |
beginning-of-buffer | |
backward-word | |
forward-word | |
mwheel-scroll | |
backward-word | |
forward-word | |
mouse-start-secondary | |
mouse-yank-secondary | |
mouse-secondary-save-then-kill | |
move-end-of-line | |
move-beginning-of-line | |
backward-char | |
forward-char | |
scroll-up | |
scroll-down | |
scroll-left | |
scroll-right | |
mouse-set-point | |
next-buffer | |
previous-buffer | |
previous-line | |
next-line | |
) | |
)) | |
(self-insert-command (or arg 1)) | |
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1)) | |
((looking-at "\\s\)") (forward-char 1) (backward-list 1)) | |
(t (self-insert-command (or arg 1)))))) | |
(global-set-key (kbd "%") 'goto-match-paren) |
- Related:
- My blogging setup with Emacs, Org mode, ox-hugo, Hugo, GitLab and Netlify
- How to easily create and use human-readable IDs in Org mode and Doom Emacs
- New release of Publishing with Emacs, Org-mode and Leanpub
- My Doom Emacs configuration, with commentary
- How to insert screenshots in Org documents on macOS
- New book: Publishing with Emacs, Org-mode and Leanpub
- Beautifying Org Mode in Emacs
- My Emacs Configuration, With Commentary
- Spotlight: use-package, a declarative configuration tool - Mastering Emacs
- Fixing Emacs problems with the fish shell
- Remapping Mac modifier keys on Emacs 23.3
- Installing AucTeX on EmacsW32 on Win7/64bit