Emacs: Move Cursor to Bracket/Quote
Here are commands to move cursor to any open/close brackets. They are extremely useful when working with computer language source code such as lisp, Java, Python, JavaScript, etc.
(defvar xah-brackets nil "string of left/right brackets pairs.") (setq xah-brackets "()[]{}<>()[]{}⦅⦆〚〛⦃⦄“”‘’‹›«»「」〈〉《》【】〔〕⦗⦘『』〖〗〘〙「」⟦⟧⟨⟩⟪⟫⟮⟯⟬⟭⌈⌉⌊⌋⦇⦈⦉⦊❛❜❝❞❨❩❪❫❴❵❬❭❮❯❰❱❲❳〈〉⦑⦒⧼⧽﹙﹚﹛﹜﹝﹞⁽⁾₍₎⦋⦌⦍⦎⦏⦐⁅⁆⸢⸣⸤⸥⟅⟆⦓⦔⦕⦖⸦⸧⸨⸩⦅⦆⧘⧙⧚⧛⸜⸝⸌⸍⸂⸃⸄⸅⸉⸊᚛᚜༺༻༼༽⏜⏝⎴⎵⏞⏟⏠⏡﹁﹂﹃﹄︹︺︻︼︗︘︿﹀︽︾﹇﹈︷︸") (defvar xah-left-brackets '("(" "{" "[" "<" "〔" "【" "〖" "〈" "《" "「" "『" "“" "‘" "‹" "«" ) "List of left bracket chars.") (progn ;; make xah-left-brackets based on xah-brackets (setq xah-left-brackets '()) (dotimes ($x (- (length xah-brackets) 1)) (when (= (% $x 2) 0) (push (char-to-string (elt xah-brackets $x)) xah-left-brackets))) (setq xah-left-brackets (reverse xah-left-brackets))) (defvar xah-right-brackets '(")" "]" "}" ">" "〕" "】" "〗" "〉" "》" "」" "』" "”" "’" "›" "»") "list of right bracket chars.") (progn (setq xah-right-brackets '()) (dotimes ($x (- (length xah-brackets) 1)) (when (= (% $x 2) 1) (push (char-to-string (elt xah-brackets $x)) xah-right-brackets))) (setq xah-right-brackets (reverse xah-right-brackets)))
(defun xah-backward-left-bracket () "Move cursor to the previous occurrence of left bracket. The list of brackets to jump to is defined by `xah-left-brackets'. URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html' Version 2015-10-01" (interactive) (search-backward-regexp (regexp-opt xah-left-brackets) nil t)) (defun xah-forward-right-bracket () "Move cursor to the next occurrence of right bracket. The list of brackets to jump to is defined by `xah-right-brackets'. URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html' Version 2015-10-01" (interactive) (re-search-forward (regexp-opt xah-right-brackets) nil t))
You should assign them keys. I suggest one of the following key pairs:
- Alt+m, Alt+.
- Alt+7, Alt+8
- Ctrl+7, Ctrl+8
- 【Alt+←】, 【Alt+→】
- F11, F12
[see Emacs: How to Define Keys]
Jump to Matching Bracket
(defun xah-goto-matching-bracket () "Move cursor to the matching bracket. If cursor is not on a bracket, call `backward-up-list'. The list of brackets to jump to is defined by `xah-left-brackets' and `xah-right-brackets'. URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html' Version 2016-11-22" (interactive) (if (nth 3 (syntax-ppss)) (backward-up-list 1 'ESCAPE-STRINGS 'NO-SYNTAX-CROSSING) (cond ((eq (char-after) ?\") (forward-sexp)) ((eq (char-before) ?\") (backward-sexp )) ((looking-at (regexp-opt xah-left-brackets)) (forward-sexp)) ((looking-back (regexp-opt xah-right-brackets) (max (- (point) 1) 1)) (backward-sexp)) (t (backward-up-list 1 'ESCAPE-STRINGS 'NO-SYNTAX-CROSSING)))))
Give it a key. [see Emacs: How to Define Keys]
Select Bracket Text
Emacs: Select Line, between Quotes, Extend Selection
Delete Bracket Text
Emacs: Delete Parenthesis/Brackets ()[]{} by Pair
Move Cursor to Quote
The following commands move cursor to the next quoted string. Very convenient for programing.
(defun xah-forward-quote-smart () "Move cursor to the current or next string quote. Place cursor at the position after the left quote. Repeated call will find the next string. URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html' Version 2016-11-22" (interactive) (let (($pos (point))) (if (nth 3 (syntax-ppss)) (progn (backward-up-list 1 'ESCAPE-STRINGS 'NO-SYNTAX-CROSSING) (forward-sexp) (re-search-forward "\\\"" nil t)) (progn (re-search-forward "\\\"" nil t))) (when (<= (point) $pos) (progn (re-search-forward "\\\"" nil t))))) (defun xah-backward-quote () "Move cursor to the previous occurrence of \". If there are consecutive quotes of the same char, keep moving until none. Returns `t' if found, else `nil'. URL `http://ergoemacs.org/emacs/emacs_navigating_keys_for_brackets.html' Version 2016-07-23" (interactive) (if (search-backward-regexp "\\\"+" nil t) (when (char-before) ; isn't nil, at beginning of buffer (while (char-equal (char-before) (char-after)) (left-char) t)) (progn (message "No more quotes before cursor.") nil)))
You should give them a pair of keys for the commands to be useful, such as F11 F12.
Working with Brackets Topic
Liket it? Put $1 at
patreon.
Or Buy Xah Emacs Tutorial. Thanks.