Emacs中是否有类似于surround.vim的扩展或模式?

29

Surround.vim是一个很方便的vim扩展,它可以让你用括号、方括号、花括号以及任何其他指定的字符将文本块包围起来。它支持段落和单词周围环绕,但我经常在可视模式下使用它。 我正在尝试使用Emacs,并想知道是否有类似的东西;一些让我能够突出显示区域然后将标记区域(或矩形)用大括号、方括号或标签框起来的功能。


1
有点相关:https://dev59.com/bHNA5IYBdhLWcg3wSrua - Chow
我最终使用了Vimpulse-surround: https://github.com/timcharper/vimpulse-surround.el - Chow
4
"Evil-surround"是新的vimpulses-surround。 - Chow
7个回答

32

也许您需要的是 wrap-region

smartparens 是另一个很好的选择,如果需要用分隔符、标签等来包装某些内容。


1
Corral(https://github.com/nivekuil/corral)刚刚发布,也可以做到这一点。 - Squidly
1
请注意,Corral的使用模式不同,似乎不支持选择包装。 - Micah Elliott

9
我使用evil-surround。它模拟vim的行为,但不幸的是可能不适合大多数Emacs用户,因为它需要evil vim模式。 然而,由于你首先提到了surround.vim,所以它可能适合你,也可能不适合你。
evil-surround似乎支持Surround.vim中的大多数功能,包括修改环绕内容。

5

我认为没有内置标签的功能,但对于括号,您可以使用M-(。对于方括号/大括号/引号,您可以这样做:

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

请注意,如果您没有选择文本区域,则会插入一堆相同符号并将光标放在它们之间。另外,删除匹配符号也很方便。
(global-set-key (kbd "M-)") 'delete-pair)

如果您想插入标签对,可以使用一些简单的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 ">"))))

1

不知道在Emacs中有任何方法可以做到这一点,甚至没有模块可以实现。

我的Elisp有点生疏,但这里有一个简单的函数,可以将当前区域(标记文本)或单词用引号(")括起来:

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

1

1

-3

所以你想选择一个区域或类似的东西,然后像各种模式对评论做的那样在它周围画一个框?我相信emacs-wiki(http://www.emacswiki.org/)有一些ascii线艺术(以及figlet工具),可以做到这一点。搜索框、安静、线艺术...

############################
#                           #
# I AM REGION, WE ARE  MANY #
#                           #
############################

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接