<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: eggc</title>
    <description>The latest articles on DEV Community by eggc (@eggc0).</description>
    <link>https://dev.to/eggc0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1869561%2F4da2843a-5ba8-4318-af87-3a076763f001.jpeg</url>
      <title>DEV Community: eggc</title>
      <link>https://dev.to/eggc0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eggc0"/>
    <language>en</language>
    <item>
      <title>Mastering Parentheses in Emacs: Essential Commands</title>
      <dc:creator>eggc</dc:creator>
      <pubDate>Sun, 22 Dec 2024 04:10:23 +0000</pubDate>
      <link>https://dev.to/eggc0/mastering-parentheses-in-emacs-essential-commands-58b0</link>
      <guid>https://dev.to/eggc0/mastering-parentheses-in-emacs-essential-commands-58b0</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article is translated from &lt;a href="https://zenn.dev/eggc/articles/267eae28a67a7c" rel="noopener noreferrer"&gt;the original Japanese version&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://github.com/Fuco1/smartparens/" rel="noopener noreferrer"&gt;smartparens&lt;/a&gt; for extending these functionalities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Features
&lt;/h2&gt;

&lt;p&gt;This section introduces features included with Emacs by default. I confirmed them on Emacs 29.4, but they should also work with older versions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Highlight Parentheses with &lt;code&gt;show-paren-mode&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/emacs-mirror/emacs/blob/master/lisp/paren.el" rel="noopener noreferrer"&gt;show-paren-mode&lt;/a&gt; is a well-known minor mode. Enabling it highlights matching opening and closing parentheses. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight common_lisp"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;show-paren-mode&lt;/span&gt; &lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;setq&lt;/span&gt; &lt;span class="nv"&gt;show-paren-style&lt;/span&gt; &lt;span class="ss"&gt;'mixed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are three styles: &lt;code&gt;parenthesis&lt;/code&gt;, &lt;code&gt;expression&lt;/code&gt;, and &lt;code&gt;mixed&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;parenthesis&lt;/th&gt;
&lt;th&gt;expression&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0wtpvq2ejcgq32u1fw0i.png" alt="the image when the show-paren-style is parenthesis" width="265" height="63"&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foibsr5ju0hxunesi40rb.png" alt="the image when the show-paren-style is expression" width="314" height="56"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By default, the style is set to &lt;code&gt;parenthesis&lt;/code&gt;, which highlights only the parentheses. &lt;code&gt;expression&lt;/code&gt; highlights the entire range enclosed by the parentheses. &lt;code&gt;mixed&lt;/code&gt; behaves like &lt;code&gt;parenthesis&lt;/code&gt; in most cases but switches to &lt;code&gt;expression&lt;/code&gt; when the range enclosed by the parentheses is long and extends off-screen.&lt;/p&gt;

&lt;h3&gt;
  
  
  Jump Over Parentheses with &lt;code&gt;forward-sexp&lt;/code&gt; and &lt;code&gt;backward-sexp&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The command &lt;code&gt;forward-sexp&lt;/code&gt;, bound to &lt;code&gt;C-M-f&lt;/code&gt; 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:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;th&gt;Explanation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;►(message "hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(message "hello world")►&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Jumps over &lt;code&gt;(...)&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(►message "hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(message► "hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Jumps over &lt;code&gt;message&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(message ►"hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(message "hello world"►)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Jumps over &lt;code&gt;"..."&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As shown, &lt;code&gt;forward-sexp&lt;/code&gt; is useful for navigating across parentheses-aware regions. While &lt;code&gt;smartparens&lt;/code&gt; provides specialized commands for finding matching parentheses, &lt;code&gt;forward-sexp&lt;/code&gt; alone is quite effective. Its counterpart, &lt;code&gt;backward-sexp&lt;/code&gt;, which performs the reverse operation, is bound to &lt;code&gt;C-M-b&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Jump Up to the Next Parentheses Level with &lt;code&gt;up-list&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;up-list&lt;/code&gt; 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:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(message► "hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(message "hello world")►&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(message "hello► world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(message "hello world"►)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Delete Parentheses and Contents with &lt;code&gt;kill-sexp&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The command &lt;code&gt;kill-sexp&lt;/code&gt;, bound to &lt;code&gt;C-M-k&lt;/code&gt; by default, deletes the next S-expression. For example, it can delete everything from the opening to the closing parentheses. Here are some examples:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;►(message "hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;►&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(message ►"hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(message ►)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  smartparens
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/Fuco1/smartparens/" rel="noopener noreferrer"&gt;smartparens&lt;/a&gt; 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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Delete the Contents Inside Parentheses with &lt;code&gt;sp-change-inner&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sp-change-inner&lt;/code&gt; searches for the next parentheses from the cursor position and deletes its contents. Here’s an example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(message "hello ►world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(message "")&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(mess►age "hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Remove Parentheses with &lt;code&gt;sp-unwrap-sexp&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sp-unwrap-sexp&lt;/code&gt; searches for the next parentheses from the cursor position and removes it. Here’s an example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;(message "hello ►world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(message hello world)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;►(message "hello world")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;message "hello world"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Replace Parentheses with &lt;code&gt;sp-rewrap-sexp&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sp-rewrap-sexp&lt;/code&gt; 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 &lt;code&gt;"&lt;/code&gt; to &lt;code&gt;'&lt;/code&gt; and vice versa.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Another package related to parentheses is &lt;a href="https://github.com/emacs-mirror/emacs/blob/master/lisp/elec-pair.el" rel="noopener noreferrer"&gt;electric-pair-mode&lt;/a&gt;, but due to my lack of knowledge, I have omitted it here. If you are interested, please feel free to explore it further.&lt;/p&gt;

</description>
      <category>emacs</category>
    </item>
  </channel>
</rss>
