在 Emacs 中将选择的文本包裹在封闭字符中

发布于 2024-09-03 22:34:40 字数 157 浏览 8 评论 0原文

在 Textmate 中,我可以通过选择文本并点击开头字符来将封闭字符('('、'['、'"' 等)包裹在文本周围。例如,如果我选择 word 并点击(,它将变成 (word)。Emacs 将此功能称为什么以及如何启用它?

In Textmate I can wrap enclosing characters ('(', '[', '"', etc.) around text by selecting it and hitting the opening character. For example, if I select word and hit (, it will become (word). What does Emacs call this feature and how do I enable it?

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

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

发布评论

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

评论(9

电影里的梦 2024-09-10 22:34:41

如果您使用 smartparens,只需选择文本,然后输入该对。 Smartparens wiki:包装

If you use smartparens just select the text and then type the pair. Smartparens wiki: Wrapping

oО清风挽发oО 2024-09-10 22:34:40

对于括号,您可以执行 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)

编辑:

关于覆盖backward-paragraph的评论很好,您可以将其绑定到C-{< /kbd>,这可能会干扰主要模式中的某些内容,并查找最后一个键并查找要插入的对,因此如果您不想将其绑定到。 something-{ 你可以绑定到这个函数:

(defun my-insert-braces ()
  (interactive)
  (if (region-active-p)
      (insert-pair 1 ?{ ?})
    (insert "{}")
    (backward-char)))

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)

EDIT:

Good point in the comments about overriding backward-paragraph. You could bind it to C-{, which might interfere with something in a major mode. insert-pair takes the last key and does a lookup to see what pair to insert, so if you don't want to bind it to something-{ you could bind to this function instead:

(defun my-insert-braces ()
  (interactive)
  (if (region-active-p)
      (insert-pair 1 ?{ ?})
    (insert "{}")
    (backward-char)))

自 Emacs 24.1(2012 年 6 月发布)起。
将其放入您的 emacs init 中:(electric-pair-mode 1)。
现在,如果你选择一个单词并点击 (,它将变成 (单词)。“、[、{ 等”也一样。

Since Emacs 24.1(released 2012-06).
Put this in your emacs init: (electric-pair-mode 1).

Now If you select a word and hit (, it will become (word). Same for ", [, { etc.

狼性发作 2024-09-10 22:34:40

我使用 http://www.emacswiki.org/emacs/ParEdit。 M-( 正是这样做的。

I use http://www.emacswiki.org/emacs/ParEdit. M-( does exactly this.

如歌彻婉言 2024-09-10 22:34:40

Autopair 是这些工具中最好的一个

Autopair is the best one of these tools

何以笙箫默 2024-09-10 22:34:40

您可以查看 wrap-region

You can have a look at wrap-region.

美人如玉 2024-09-10 22:34:40

我也会看看骨架模式
http://ggorjan.blogspot.com/2007/05 /sculpture-pair-mode-in-emacs.html

它非常灵活,特别是对于括号。

I'd take a look also at skeleton-mode
http://ggorjan.blogspot.com/2007/05/skeleton-pair-mode-in-emacs.html

It's very flexible expecially for parentheses.

萌面超妹 2024-09-10 22:34:40

有文本伙伴模式。

来自 Emacswiki:

请参阅 textmate-mode 以尝试对括号和引号使用 TextMate 行为(自动关闭、覆盖、智能删除)。

http://code.google.com/p/emacs-textmate/

There is textmate-mode.

From Emacswiki:

See textmate-mode for an attempt of having the TextMate behaviour for parenthesis and quotes (auto-closing, overwriting, smart delete).

http://code.google.com/p/emacs-textmate/

黒涩兲箜 2024-09-10 22:34:40

现在还有Corral。它的“按我的意思做”行为使得这个过程比手动选择文本并按键要快得多。

(免责声明:我是作者)

There's now Corral as well. Its "do what I mean" behavior makes this process a lot faster than manually selecting the text and hitting the key.

(disclaimer: I'm the author)

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