Chow
Chow

Reputation: 475

Is there an extension or mode in Emacs similar to surround.vim?

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.

Upvotes: 29

Views: 10688

Answers (7)

jiahut
jiahut

Reputation: 1531

maybe evil-surround is what you are looking for.

thanks.

Upvotes: 1

Bozhidar Batsov
Bozhidar Batsov

Reputation: 56665

Maybe wrap-region is what you need.

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

Upvotes: 32

sp3ctum
sp3ctum

Reputation: 465

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.

Upvotes: 9

Tim Harper
Tim Harper

Reputation: 2621

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.

Upvotes: 1

scottfrazer
scottfrazer

Reputation: 17337

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 ">"))))

Upvotes: 5

slu
slu

Reputation: 1325

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 "\"")))

Upvotes: 1

hpavc
hpavc

Reputation: 1335

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 #
#                           #
############################

Upvotes: -3

Related Questions