Copy selected text into Emacs
I don't know if there is a standard emacs command for this, but this should do the trick:
(defun surround-brackets () "Surround current region with brackets" (interactive) (when (use-region-p) (save-excursion (let ((beg (region-beginning)) (end (region-end))) (goto-char end) (insert "]") (goto-char beg) (insert "[")))))
EDIT As noted in the comments, this question suggests the following solution, which is much simpler, but requires the use of a key binding ending in[
(global-set-key (kbd "M-[") 'insert-pair)
source to share
The most structured and flexible way to do this is with autopair . I will say that this has become the canonical solution to insert paired delimiters and wrap content in paired delimiters.
Joao ( autopair
author) also wrote yasnippet , which is a very popular template library in Emacs. yasnippet
can be used to wrap an area in free text.
Both libraries model their behavior in Textmate.
source to share