Wrapping selecting text in enclosing characters in

2019-02-01 07:40发布

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?

8条回答
再贱就再见
2楼-- · 2019-02-01 07:56

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)))
查看更多
【Aperson】
3楼-- · 2019-02-01 08:00

You can have a look at wrap-region.

查看更多
成全新的幸福
4楼-- · 2019-02-01 08:06

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

查看更多
一纸荒年 Trace。
5楼-- · 2019-02-01 08:10

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.

查看更多
做个烂人
6楼-- · 2019-02-01 08:13

Autopair is the best one of these tools

https://github.com/capitaomorte/autopair

查看更多
神经病院院长
7楼-- · 2019-02-01 08:15

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)

查看更多
登录 后发表回答