Emacs: Insert Brackets by Pair

Perm URL with updates: http://xahlee.org/emacs/emacs_insert_brackets_by_pair.html

Some emacs commands that lets you insert brackets in pairs always.

(defun insert-bracket-pair (leftBracket rightBracket)
  "Insert a matching bracket and place the cursor between them."
  (interactive)
  (when (region-active-p)
    (delete-region (region-beginning) (region-end) ) )
  (insert leftBracket rightBracket)
  (backward-char 1) )

(defun insert-pair-paren () (interactive) (insert-bracket-pair "(" ")") )
(defun insert-pair-brace () (interactive) (insert-bracket-pair "{" "}") )
(defun insert-pair-bracket () (interactive) (insert-bracket-pair "[" "]") )
(defun insert-pair-single-angle-quote () (interactive) (insert-bracket-pair "‹" "›") )
(defun insert-pair-double-angle-quote () (interactive) (insert-bracket-pair "«" "»") )
(defun insert-pair-double-curly-quote () (interactive) (insert-bracket-pair "“" "”") )
(defun insert-pair-single-curly-quote () (interactive) (insert-bracket-pair "‘" "’") )
(defun insert-pair-double-straight-quote () (interactive) (insert-bracket-pair "\"" "\"") )
(defun insert-pair-single-straight-quote () (interactive) (insert-bracket-pair "'" "'") )

(defun insert-pair-corner-bracket () (interactive) (insert-bracket-pair "「" "」") )
(defun insert-pair-white-corner-bracket () (interactive) (insert-bracket-pair "『" "』") )
(defun insert-pair-angle-bracket () (interactive) (insert-bracket-pair "〈" "〉") )
(defun insert-pair-double-angle-bracket () (interactive) (insert-bracket-pair "《" "》") )
(defun insert-pair-white-lenticular-bracket () (interactive) (insert-bracket-pair "〖" "〗") )
(defun insert-pair-black-lenticular-bracket () (interactive) (insert-bracket-pair "【" "】") )
(defun insert-pair-tortoise-shell-bracket () (interactive) (insert-bracket-pair "〔" "〕") )

(defun insert-pair-fullwith-paren () (interactive) (insert-bracket-pair "(" ")") )
(defun insert-pair-fullwith-bracket () (interactive) (insert-bracket-pair "[" "]") )
(defun insert-pair-fullwith-brace () (interactive) (insert-bracket-pair "{" "}") )

(defun insert-pair-white-paren () (interactive) (insert-bracket-pair "⦅" "⦆") )
(defun insert-pair-white-bracket () (interactive) (insert-bracket-pair "〚" "〛") )
(defun insert-pair-white-brace () (interactive) (insert-bracket-pair "⦃" "⦄") )

Here's the key setup, using 【Hyper+‹letter›】:

(global-set-key (kbd "H-e") 'insert-pair-paren)              ;()
(global-set-key (kbd "H-u") 'insert-pair-brace)              ;{}
(global-set-key (kbd "H-o") 'insert-pair-bracket)            ;[]
(global-set-key (kbd "H-i") 'insert-pair-single-angle-quote) ;‹›
(global-set-key (kbd "H-I") 'insert-pair-double-angle-quote) ;«»
(global-set-key (kbd "H-a") 'insert-pair-double-curly-quote) ;“”
(global-set-key (kbd "H-A") 'insert-pair-single-curly-quote) ;‘’
(define-key key-translation-map (kbd "H-.") (kbd "=")) ; equal
(define-key key-translation-map (kbd "H-,") (kbd "+")) ; plus

(global-set-key (kbd "H-p") 'insert-pair-double-straight-quote)
(global-set-key (kbd "H-y") 'insert-pair-single-straight-quote)

(global-set-key (kbd "H-k") 'insert-pair-corner-bracket) ;「」
(global-set-key (kbd "H-K") 'insert-pair-white-corner-bracket) ;『』
(global-set-key (kbd "H-j") 'insert-pair-angle-bracket)        ;〈〉
(global-set-key (kbd "H-J") 'insert-pair-double-angle-bracket) ;《》
(global-set-key (kbd "H-q") 'insert-pair-black-lenticular-bracket) ;【】
(global-set-key (kbd "H-Q") 'insert-pair-white-lenticular-bracket) ;〖〗
(global-set-key (kbd "H-;") 'insert-pair-tortoise-shell-bracket)   ;〔〕

Here's the setup for Hyper. On Windows, the Menu key is set to it. On the Mac it's Option:

(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
  ;; Carbon Emacs variables
  ;;    (setq mac-command-key-is-meta t)
  ;;    (setq mac-option-key-is-meta nil)

  ;; 2009-10-01, 2011-05-31 works for emacs 23.1 built for ns
  (setq mac-control-modifier 'control)
  (setq mac-command-modifier 'meta)
  (setq mac-option-modifier 'hyper)
  ;; (setq mac-option-modifier 'super)

  ;; Carbon emacs. disable the Mac shortcut convention of cmd-h to hide current application.
  (setq mac-pass-command-to-system nil)
  )

 ((string-equal system-type "gnu/linux")
  nil ; do nothing. You should set Super and Hyper from your OS
  )
 )

You probably want to adjust the keys used above. This setup is for Dvorak Keyboard Layout, Windows, the 【Menu+‹letter›】 key. The letter key is chosen so that the most frequently needed are placed on the home row, based on my own usage patterns of the brackets. For example, the [](){} are all left hand home row while right thumb holds down the menu key.

Advantage of Using Your Own Commands

With these commands, you can type brackets without relying on special modes or settings, and you can type them while in minibuffer, and in any mode.

Also, the keys are more ergonomic than traditional positions. They are under the home row, and using thumb instead of pinky for Shift.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs