Here are some convenient commands to move cursor by brackets. Very useful in coding C, Java, Perl, Python, HTML, or other languages.
(defun ergoemacs-forward-open-bracket () "Move cursor to the next occurrence of left bracket or quotation mark." (interactive) (forward-char 1) (search-forward-regexp (regexp-opt '("\"" "(" "{" "[" "<" "〔" "【" "〖" "〈" "《" "「" "『" "“" "‘" "‹" "«")) nil t) ;; (search-forward-regexp "\\s(\\|\\s\"\\|<\\|“\\|‘\\|‹") ; using syntax table (backward-char 1)) (defun ergoemacs-backward-open-bracket () "Move cursor to the previous occurrence of left bracket or quotation mark.." (interactive) (search-backward-regexp (regexp-opt '("\"" "(" "{" "[" "<" "〔" "【" "〖" "〈" "《" "「" "『" "“" "‘" "‹" "«")) nil t)) (defun ergoemacs-forward-close-bracket () "Move cursor to the next occurrence of right bracket or quotation mark." (interactive) (search-forward-regexp (regexp-opt '("\"" ")" "\\]" "}" ">" "〕" "】" "〗" "〉" "》" "」" "』" "”" "’" "›" "»")) nil t) ;; (search-forward-regexp "\\s)\\|\\s\"\\|>\\|”\\|’\\|›") ;using syntax table ) (defun ergoemacs-backward-close-bracket () "Move cursor to the previous occurrence of right bracket or quotation mark." (interactive) (backward-char 1) (search-backward-regexp (regexp-opt '("\"" ")" "\\]" "}" ">" "〕" "】" "〗" "〉" "》" "」" "』" "”" "’" "›" "»")) nil t) (forward-char 1))
Moving by block.
(defun ergoemacs-forward-block () "Move cursor forward to the beginning of next text block. A text block is separated by 2 empty lines (or line with just whitespace). In most major modes, this is similar to `forward-paragraph', but this command's behavior is the same regardless of syntax table." (interactive) (if (search-forward-regexp "\n[[:blank:]\n]*\n+" nil "NOERROR") (progn (backward-char)) (progn (goto-char (point-max)) ) ) ) (defun ergoemacs-backward-block () "Move cursor backward to previous text block. See: `ergoemacs-forward-block'" (interactive) (if (search-backward-regexp "\n[\t\n ]*\n+" nil "NOERROR") (progn (skip-chars-backward "\n\t ") (forward-char 1) ) (progn (goto-char (point-min)) ) ) )
These commands lets you move cursor around all the most used brackets. 〔☛ Matching Brackets in Unicode〕
You can assign them keys with Alt and arrow, like this:
(global-set-key (kbd "<M-left>") 'backward-open-bracket) ; Alt+← (global-set-key (kbd "<M-right>") 'forward-close-bracket) ; Alt+→
If you want to use {⇞ Page △, ⇟ Page ▽} or other special keys, see: Emacs: How to Define Keys.
If you want {Super, Hyper}, see: Emacs: How to define Super & Hyper Keys.
The above commands move cursor to any of the brackets. If you want to navigate brackets as tree (⁖ jump to parent, skip siblings), you can use the following built-in commands:
The keys to navigate lisp code are:
backward-sexpforward-sexpbackward-up-listdown-listEmacs will also highlight matching brackets. When your cursor is on a matching pair, emacs will highlight the matching brackets, or, the text 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】sgml-skip-tag-forward 【Ctrl+c Ctrl+f】For more productivity tips with HTML, see: Emacs HTML Tips.
Emacs: Insert Brackets by Pair
“paredit” is a minor mode designed for editing lisp code. Many lisp hacker type love it. You might try it. 〔☛ A Guide on Emacs Package System〕