Xah Lee, 2011-05-06
Here are some convenient keys to move cursor by brackets. Very useful in coding C, Java, Python, HTML, or other languages.
(defun forward-open-bracket () "Move cursor to the next occurrence of one of ( { [ <." (interactive) (forward-char 1) (search-forward-regexp "(\\|{\\|\\[\\|<\\|〔\\|【\\|〖\\|〈\\|「\\|“\\|‘\\|‹\\|«") (backward-char 1) ) (defun backward-open-bracket () "Move cursor to the previous occurrence of one of ( { [ <." (interactive) (search-backward-regexp "(\\|{\\|\\[\\|<\\|〔\\|【\\|〖\\|〈\\|「\\|“\\|‘\\|‹\\|«") ) (defun forward-close-bracket () "Move cursor to the next occurrence of one of ) } ] >." (interactive) (search-forward-regexp ")\\|\\]\\|}\\|>\\|〕\\|】\\|〗\\|〉\\|」\\| ”\\| ’\\| ›\\| »") ) (defun backward-close-bracket () "Move cursor to the next occurrence of one of ) } ] >." (interactive) (backward-char 1) (search-backward-regexp ")\\|\\]\\|}\\|>\\|〕\\|】\\|〗\\|〉\\|」\\| ”\\| ’\\| ›\\| »") (forward-char 1) )
These commands lets you move cursor around any brackets, () {} [] but also < > and few other common unicode matching pairs “” ‘’ ‹› «» 【】 〖〗 〈〉 〔〕 「」. (See: Matching Brackets in Unicode.)
You can assign them keys. See: Emacs: How to Define Keyboard Shortcuts.
Good keys are the arrow keys, with a combination of Alt, Shift, or Win and Menu keys by mapping emacs's Hyper and Super to them. For me, i use the Win key with Super. Here's my code:
(cond ((string-equal system-type "windows-nt") ; Windows ;; setting the PC keyboard's various keys to ;; Super or Hyper, for emacs running on Windows. (setq w32-pass-lwindow-to-system nil w32-pass-rwindow-to-system nil w32-pass-apps-to-system nil w32-lwindow-modifier 'super ;; Left Windows key w32-rwindow-modifier 'hyper ;; Right Windows key w32-apps-modifier 'hyper) ;; Menu key ) ((string-equal system-type "darwin") ; Mac (setq mac-option-modifier 'super) ) ) (global-set-key (kbd "<s-left>") 'backward-open-bracket) (global-set-key (kbd "<s-right>") 'forward-open-bracket) (global-set-key (kbd "<s-up>") 'backward-close-bracket) (global-set-key (kbd "<s-down>") 'forward-close-bracket)
For detail about setting up hyper or super key, see: Emacs: How to define Super & Hyper Keys.
The above commands is a very good complement to emacs built-in keys to navigate trees made of nested matching brackets as in lisp and html code.
The keys to navigate lisp code are:
Emacs will also highlight matching brackets. When your cursor is on a matching pair, emacs will highlight the matching brackets, or, the code between them, depending on your settings. For how to turn it on or set your preferences, see: How to Edit Lisp Code with Emacs.
Also, you can define a command so that when cursor is on a opening bracket, you can press a key to have the whole code enclosed by the matching bracket selected. See the command “extend-selection” at Suggestions on Emacs's mark-word Command.
For html, when in “html-mode”, it has “sgml-skip-tag-backward” 【Ctrl+c Ctrl+b】 and “sgml-skip-tag-forward” 【Ctrl+c Ctrl+f】.
For more productivity tips with html, see: Emacs and HTML Tips.