ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Emacs: Keys to Navigate Brackets

Xah Lee, , …,

Here are some convenient commands to move cursor by brackets. Very useful in coding C, Java, Perl, Python, HTML, or other languages.

Commands to Navigate Brackets

(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

Setup Alt and Arrow Keys

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.

Emacs's Keys to Navigate Nested Brackets as Tree

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:

Built-in Keys to Navigate Lisp Code

The keys to navigate lisp code are:

Highlighting Matching Brackets

Emacs 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.

Select Code Between Matching Brackets

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.

Built-in Keys for Navigating Matching HTML Tags

For HTML, when in html-mode, it has:

For more productivity tips with HTML, see: Emacs HTML Tips.

Insert Brackets by Pair

Emacs: Insert Brackets by Pair

paredit

“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

blog comments powered by Disqus