Introduction
This article is translated from the original Japanese version.
Emacs provides several convenient commands for efficient operations involving parentheses. In this article, I would like to introduce these commands. Additionally, I will also introduce an advanced package called smartparens for extending these functionalities.
Basic Features
This section introduces features included with Emacs by default. I confirmed them on Emacs 29.4, but they should also work with older versions.
Highlight Parentheses with show-paren-mode
show-paren-mode is a well-known minor mode. Enabling it highlights matching opening and closing parentheses. Here’s an example:
(show-paren-mode t)
(setq show-paren-style 'mixed)
There are three styles: parenthesis
, expression
, and mixed
.
parenthesis | expression |
---|---|
By default, the style is set to parenthesis
, which highlights only the parentheses. expression
highlights the entire range enclosed by the parentheses. mixed
behaves like parenthesis
in most cases but switches to expression
when the range enclosed by the parentheses is long and extends off-screen.
Jump Over Parentheses with forward-sexp
and backward-sexp
The command forward-sexp
, bound to C-M-f
by default, allows jumping to the next symbolic expression (S-expression) in Emacs. S-expression, short for Symbolic Expression, is essentially a Lisp expression. Here are some examples, with ► representing the cursor position:
Before | After | Explanation |
---|---|---|
►(message "hello world") |
(message "hello world")► |
Jumps over (...) . |
(►message "hello world") |
(message► "hello world") |
Jumps over message . |
(message ►"hello world") |
(message "hello world"►) |
Jumps over "..." . |
As shown, forward-sexp
is useful for navigating across parentheses-aware regions. While smartparens
provides specialized commands for finding matching parentheses, forward-sexp
alone is quite effective. Its counterpart, backward-sexp
, which performs the reverse operation, is bound to C-M-b
.
Jump Up to the Next Parentheses Level with up-list
up-list
is one of the commands for manipulating S-expressions. Executing this command moves the cursor to the next higher level of parentheses. Think of it as searching for a closing parenthesis to jump to. Here’s an example:
Before | After |
---|---|
(message► "hello world") |
(message "hello world")► |
(message "hello► world") |
(message "hello world"►) |
Delete Parentheses and Contents with kill-sexp
The command kill-sexp
, bound to C-M-k
by default, deletes the next S-expression. For example, it can delete everything from the opening to the closing parentheses. Here are some examples:
Before | After |
---|---|
►(message "hello world") |
► |
(message ►"hello world") |
(message ►) |
smartparens
smartparens is a package that extends operations related to parentheses. It provides features such as automatically inserting matching parentheses and more. This article introduces only a few commands, so refer to the smartparens documentation for details and installation instructions.
Delete the Contents Inside Parentheses with sp-change-inner
sp-change-inner
searches for the next parentheses from the cursor position and deletes its contents. Here’s an example:
Before | After |
---|---|
(message "hello ►world") |
(message "") |
(mess►age "hello world") |
() |
Remove Parentheses with sp-unwrap-sexp
sp-unwrap-sexp
searches for the next parentheses from the cursor position and removes it. Here’s an example:
Before | After |
---|---|
(message "hello ►world") |
(message hello world) |
►(message "hello world") |
message "hello world" |
Replace Parentheses with sp-rewrap-sexp
sp-rewrap-sexp
searches for the next parentheses from the cursor position and replaces it with a new type of parentheses. After executing the command, you will be prompted to specify the new type of parentheses. Although not frequently used, it can be handy for tasks like converting "
to '
and vice versa.
Conclusion
This article introduced various commands related to parentheses in Emacs. By combining these commands, you can program much faster compared to manipulating text one character at a time. While these operations may not occur frequently, you might find it useful to assign key bindings based on your preferences.
Another package related to parentheses is electric-pair-mode, but due to my lack of knowledge, I have omitted it here. If you are interested, please feel free to explore it further.
Top comments (0)