Question

Surround.vim is a nifty vim extension that allows you to surround blocks of text with , brackets, braces, and pretty much any arbitrary "surround" character. It supports paragraph and word surround, but I frequently use it in visual mode. I'm playing around with Emacs and wondering if there's something similar; something that will let me highlight a region and then have the marked region (or rectangle) enclosed with braces, brackets or tags.

Was it helpful?

Solution

Maybe wrap-region is what you need.

smartparens is another excellent option if need to wrap something with delimiters, tags, etc.

OTHER TIPS

I use evil-surround. It emulates vim behaviour but unfortunately might not be what most emacs users want since it requires the evil vim mode. However, it may or may not be right for you since you referenced surround.vim in the first place.

evil-surround seems to support most of the features in Surround.vim, including modifying surroundings.

I don't think there is anything built in for tags, but for parens you can do M-(. For brackets/braces/quotes you could do:

(global-set-key (kbd "M-[") 'insert-pair)
(global-set-key (kbd "M-{") 'insert-pair)
(global-set-key (kbd "M-\"") 'insert-pair)

Note that if you don't have a region highlighted, it will just insert the pair of whatevers and put the cursor in between them. Also handy for deleting matching whatevers is

(global-set-key (kbd "M-)") 'delete-pair)

If you want to insert tag pairs, it's some simple elisp:

(defun my-insert-tags (tag)
  (interactive "sTag: ")
  (if (region-active-p)
      (let ((beg (region-beginning)))
        (save-excursion
          (goto-char (region-end))
          (insert "</" tag ">")
          (goto-char beg)
          (insert "<" tag ">")))
    (insert "<" tag ">")
    (save-excursion
      (insert "</" tag ">"))))

Don't know of any way of doing that in Emacs, not even with a module.

My Elisp is a little rusty, buy here's a simple function that will enclose the current region (marked text) or word with quotes ("):

(defun insert-quotes ()
  "Inserts quotes (\") around the current region or work."
  (interactive)
  (let (start end bounds)
    (if (and transient-mark-mode mark-active)
        (setq start (region-beginning) 
              end (region-end))
      (progn
        (setq bounds (bounds-of-thing-at-point 'symbol))
        (setq start (car bounds) 
              end (cdr bounds))))
    (goto-char start)
    (insert "\"")
    (goto-char (+ end 1))
    (insert "\"")))

Yes, there is a clone of surround.vim, as of 1 week ago: http://github.com/timcharper/vimpulse-surround.el

It requires vimpulse, which requires vim. It implements much of surround.vim's functionality.

maybe evil-surround is what you are looking for.

thanks.

So you want to select a region or similar and then make a box around it like a various modes do for comments? I believe emacs-wiki (http://www.emacswiki.org/) has some ascii-line art (and a figlet tool as well) that will do that. Searching for box, quite, line art ...

############################
#                           #
# I AM REGION, WE ARE  MANY #
#                           #
############################
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top