Logo

dev-resources.site

for different kinds of informations.

Mastering Parentheses in Emacs: Essential Commands

Published at
12/22/2024
Categories
emacs
Author
eggc0
Categories
1 categories in total
emacs
open
Author
5 person written this
eggc0
open
Mastering Parentheses in Emacs: Essential Commands

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)
Enter fullscreen mode Exit fullscreen mode

There are three styles: parenthesis, expression, and mixed.

parenthesis expression
the image when the show-paren-style is parenthesis the image when the show-paren-style is 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.

emacs Article's
30 articles in total
Favicon
Emacs 2024 Changes
Favicon
emacs error Failed to verify signature archive-contents.sig
Favicon
Mastering Parentheses in Emacs: Essential Commands
Favicon
C Development with GNU Emacs
Favicon
Mastering Golang Debugging in Emacs
Favicon
Emacs for Python and Poetry Using `basedpyright-langserver`
Favicon
Explorando org-babel en emacs
Favicon
(Game)Dev with Emacs - Because it's not Already Hard Enough Without it
Favicon
The Power of Tries, Data Structure Optimization in Emacs
Favicon
Creating an Emacs major mode - because why not?
Favicon
Learning to Like Neovim
Favicon
Emacs, a simple tour
Favicon
Learning Lisp
Favicon
Managing multiple terminals in Emacs
Favicon
Vim-style repeatable key bindings for navigating windows in Emacs
Favicon
Mermaid preview using xwidget browser
Favicon
Setting up Doom Emacs for Astro Development
Favicon
Moving to Emacs Tree Sitter Modes
Favicon
Mastering JSX Editing in Emacs with Tree-sitter
Favicon
Exploring Syntax Trees in Emacs with Tree-sitter
Favicon
Chinese Zodiac Time for Emacs
Favicon
Using Jmespath in Emacs
Favicon
From Doom to Vanilla Emacs
Favicon
Tell runtime/cgo to not thread warnings as errors
Favicon
Creating dynamic task runners for your npm scripts in Emacs
Favicon
5 ways to get text from an Emacs buffer
Favicon
Using age with org-journal
Favicon
Once again I wanted to have Emacs be my XML Notepad 2006
Favicon
Dia 9
Favicon
Emacs is More Like a Terminal Than an Editor

Featured ones: