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

发布于 2024-08-31 02:32:59 字数 243 浏览 9 评论 0原文

Surround.vim 是一个漂亮的 vim 扩展,可以让你围绕块带有 、方括号、大括号和几乎任何任意“环绕”字符的文本。它支持段落和文字环绕,但我经常在视觉模式下使用它。 我正在玩 Emacs,想知道是否有类似的东西;可以让我突出显示一个区域,然后将标记的区域(或矩形)用大括号、方括号或标签括起来。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寄与心 2024-09-07 02:32:59

也许 wrap-region 就是您所需要的。

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

Maybe wrap-region is what you need.

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

尽揽少女心 2024-09-07 02:32:59

我使用 evil-surround。它模拟 vim 行为,但不幸的是可能不是大多数 emacs 用户想要的,因为它需要 evil vim 模式。
然而,它可能适合你,也可能不适合你,因为你首先引用了 around.vim。

evil-surround 似乎支持 Surround.vim 中的大部分功能,包括修改环境。

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.

旧城烟雨 2024-09-07 02:32:59

我认为标签没有内置任何内容,但对于括号,您可以执行 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 ">"))))

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 ">"))))
沩ん囻菔务 2024-09-07 02:32:59

不知道在 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 "\"")))

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 "\"")))
孤独患者 2024-09-07 02:32:59

是的,从 1 周前开始,有 Surround.vim 的克隆版: http://github .com/timcharper/vimpulse-surround.el

它需要 vimpulse,而 vimpulse 需要 vim。它实现了 around.vim 的大部分功能。

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.

你げ笑在眉眼 2024-09-07 02:32:59

也许 evil-surround 就是您正在寻找的。

谢谢。

maybe evil-surround is what you are looking for.

thanks.

鸠书 2024-09-07 02:32:59

所以你想选择一个区域或类似的区域,然后在它周围制作一个框,就像各种评论模式一样?我相信 emacs-wiki (http://www.emacswiki.org/) 有一些 ascii 行艺术(还有一个 Figlet 工具)就可以做到这一点。寻找盒子、相当、线条艺术......

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

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 #
#                           #
############################
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文