Lädt...


🔧 Mastering Parentheses in Emacs: Essential Commands


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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
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.

...

🔧 Mastering Parentheses in Emacs: Essential Commands


📈 63.69 Punkte
🔧 Programmierung

🍏 Emacs 28.2 - GNU Emacs editor for OS X.


📈 30.03 Punkte
🍏 iOS / Mac OS

🐧 [Emacs Tutorial] It's semantic how Emacs binds all keys


📈 30.03 Punkte
🐧 Linux Tipps

🐧 [Emacs Tutorial] Emacs definitely easy, it starts everything from "M-x"


📈 30.03 Punkte
🐧 Linux Tipps

📰 Emacs Developers (Including Richard Stallman) Discuss How to Build a More 'Modern' Emacs


📈 30.03 Punkte
📰 IT Security Nachrichten

🐧 Faster GNU Emacs start: The Emacs Daemon


📈 30.03 Punkte
🐧 Linux Tipps

🎥 Top Git Commands | Most Used Git Commands | Git Commands With Examples


📈 28.76 Punkte
🎥 Video | Youtube

🔧 Mastering Bash: Essential Commands for Everyday Use


📈 25.36 Punkte
🔧 Programmierung

🔧 🚀 Mastering DevOps with Essential Linux Commands 💻


📈 25.36 Punkte
🔧 Programmierung

🔧 👨‍💻Mastering Git: A Beginner’s Guide to the 12 Most Essential Commands🫠


📈 25.36 Punkte
🔧 Programmierung

🔧 Mastering Linux Commands on AWS: Essential Tools for Cloud Professionals


📈 25.36 Punkte
🔧 Programmierung

🔧 Mastering the Basics: Essential Linux Commands


📈 25.36 Punkte
🔧 Programmierung

🔧 Mastering GitHub: 30 Essential Commands Every Developer Should Know


📈 25.36 Punkte
🔧 Programmierung

📰 RHCSA Series: Mastering Essential Commands & System Documentation – Part 2


📈 25.36 Punkte
🐧 Unix Server

🔧 Mastering Git: Essential Commands, Workflows, and Their Uses Explained


📈 25.36 Punkte
🔧 Programmierung

🔧 Mastering Essential Linux Commands: A Complete Guide for Beginners


📈 25.36 Punkte
🔧 Programmierung

🔧 Mastering Git: Essential Commands Every Developer Should Know


📈 25.36 Punkte
🔧 Programmierung

🔧 Mastering Linux: Essential Commands for System Management and Shell


📈 25.36 Punkte
🔧 Programmierung

🐧 On OS X you can use Emacs movement commands globally. Is there some equivalent option in Linux?


📈 24.6 Punkte
🐧 Linux Tipps

🔧 20. Valid Parentheses


📈 23.32 Punkte
🔧 Programmierung

🔧 Valid Parentheses (Leetcode 20)


📈 23.32 Punkte
🔧 Programmierung

🔧 A solution for leetcode problem 20 "valid parentheses"


📈 23.32 Punkte
🔧 Programmierung

🔧 1190. Reverse Substrings Between Each Pair of Parentheses


📈 23.32 Punkte
🔧 Programmierung

🔧 Leetcode Day 5: Valid Parentheses Explained


📈 23.32 Punkte
🔧 Programmierung

🔧 Leetcode Day 5: Valid Parentheses Explained


📈 23.32 Punkte
🔧 Programmierung

🔧 I Got Tired of the Way I Type Parentheses on a QWERTY Keyboard


📈 23.32 Punkte
🔧 Programmierung

🔧 Javascript functions with and without parentheses


📈 23.32 Punkte
🔧 Programmierung

🔧 Valid Parentheses | LeetCode | Java


📈 23.32 Punkte
🔧 Programmierung

🔧 2116. Check if a Parentheses String Can Be Valid


📈 23.32 Punkte
🔧 Programmierung

🔧 1249. Minimum Remove to Make Valid Parentheses


📈 23.32 Punkte
🔧 Programmierung

🔧 Daily JavaScript Challenge #JS-58: Determine if a String has Balanced Parentheses


📈 23.32 Punkte
🔧 Programmierung

🔧 1614. Maximum Nesting Depth of the Parentheses


📈 23.32 Punkte
🔧 Programmierung

🔧 20. Valid Parentheses


📈 23.32 Punkte
🔧 Programmierung

🔧 Is ActiveRecord right in omitting parentheses in queries? (and how ChatGPT lies again)


📈 23.32 Punkte
🔧 Programmierung

matomo