Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add / remove parenthesis around a marked region in Emacs?

Tags:

emacs

I often find it is needed to add / remove parenthesis / bracket around a marked region in Emacs.

Currently I do the adding manually by:

  1. Move the cursor to one end of the intended region, type the opening deliminator;
  2. Move the cursor to the other end of the intended region, type the closing deliminator;

And the removal in the other way around.

However, this seems cumbersome and error prone because of the movement of cursor. From a safety point of view, if I delete parenthesis in pairs, I feel safer, because it is an atomic operation

Is there a built-in or hand-crafted function in Emacs to do it with respect to marked region?

like image 914
modeller Avatar asked Dec 25 '22 06:12

modeller


1 Answers

Removing a pair of surrounding characters (parens, brackets, whatever) with the delete-pair function: You don't have to mark the region.

  • Before a pair: M-x delete-pair
  • Inside a pair: C-M-u for backing out of the pair and then M-x delete-pair.
  • Behind a pair: M-- (negative argument) and then M-x delete-pair

Enclosing a region with parens:

  • Marking the region and M-(.
like image 86
Dieter.Wilhelm Avatar answered Mar 06 '23 11:03

Dieter.Wilhelm