Mastering Parentheses in Emacs: Essential Commands
December 22, 2024

Mastering Parentheses in Emacs: Essential Commands


introduce

This article is translated from Japanese original.

Emacs provides several convenient commands for efficient operations involving parentheses. In this article I would like to introduce these instructions. Additionally, I will introduce an advanced package called smart parents used to extend these functions.


Basic features

This section introduces the features included in the Emacs defaults. I confirmed them on Emacs 29.4, but they should work on older versions as well.


Highlight brackets show-paren-mode

Show couple fashion is a well-known minor mode. Enabling it will highlight matching opening and closing brackets. Here is an example:

(show-paren-mode t)
(setq show-paren-style 'mixed)
Enter full screen mode

Exit full screen mode

There are three styles: parenthesis, expressionand mixed.

parenthesis Express

By default, the style is set to parenthesisonly the parentheses are highlighted. expression Inverse the entire range within white brackets. mixed act like parenthesis In most cases, but switching to expression When the bracketed range is long and extends off the screen.


skip parentheses forward-sexp and backward-sexp

Order forward-sexpbound to C-M-f By default, jumping to the next symbolic expression (S-expression) in Emacs is allowed. S-expression is the abbreviation of symbolic expression, which is essentially a Lisp expression. Here are some examples, where ► represents the cursor position:

forward back explain
►(message "hello world") (message "hello world")► jump over (...).
(►message "hello world") (message► "hello world") jump over message.
(message ►"hello world") (message "hello world"►) jump over "...".

As shown in the figure, forward-sexp Useful for navigating across bracket-aware regions. although smartparens Provides specialized commands to find matching brackets, forward-sexp Used alone it is quite effective. its counterpart, backward-sexpperforming the opposite operation must be C-M-b.


Jump to next bracket level up-list

up-list It is one of the commands to operate S expression. Executing this command moves the cursor to the next higher-level bracket. Think of it as finding the closing bracket to jump to. Here is an example:

forward back
(message► "hello world") (message "hello world")►
(message "hello► world") (message "hello world"►)


Remove brackets and content kill-sexp

Order kill-sexpbound to C-M-k By default, the next S-expression is deleted. For example, it can remove everything from the opening bracket to the closing bracket. Here are some examples:

forward back
►(message "hello world")
(message ►"hello world") (message ►)


smart parents

smart parents is a suite that extends operations related to parentheses. It provides functions such as automatic insertion of matching brackets. This article only covers a few commands, so see the smartparens documentation for details and installation instructions.


Delete content in brackets sp-change-inner

sp-change-inner Searches for the next bracket from the cursor position and deletes its contents. Here is an example:

forward back
(message "hello ►world") (message "")
(mess►age "hello world") ()


remove brackets sp-unwrap-sexp

sp-unwrap-sexp Searches for the next bracket from the cursor position and removes it. Here is an example:

forward back
(message "hello ►world") (message hello world)
►(message "hello world") message "hello world"


Replace the brackets with sp-rewrap-sexp

sp-rewrap-sexp Searches for the next bracket from the cursor position and replaces it with a new type of bracket. After executing the command, you will be prompted to specify the new bracket type. Although not used often, it is very convenient for tasks such as conversion " arrive ' vice versa.


in conclusion

This article introduces various bracket-related instructions in Emacs. By combining these commands, you can program faster than if you manipulate text one character at a time. While these operations may not happen often, you may find it useful to assign keybindings according to your preferences.

Another bracket-related package is Electric pair modebut I omit it here due to my lack of knowledge. If you’re interested, feel free to explore further.

2024-12-22 04:10:23

Leave a Reply

Your email address will not be published. Required fields are marked *