<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Emacs on NaCNO's magic scroll</title><link>https://nacno-toporiame.github.io/tags/emacs/</link><description>Recent content in Emacs on NaCNO's magic scroll</description><generator>Hugo -- gohugo.io</generator><language>en-dk</language><copyright>NaCNO</copyright><lastBuildDate>Mon, 01 Jun 2026 22:10:49 +0200</lastBuildDate><atom:link href="https://nacno-toporiame.github.io/tags/emacs/index.xml" rel="self" type="application/rss+xml"/><item><title>Integrating some emacs into my system</title><link>https://nacno-toporiame.github.io/os-env/vterm-query-on-exit/</link><pubDate>Mon, 01 Jun 2026 22:10:49 +0200</pubDate><guid>https://nacno-toporiame.github.io/os-env/vterm-query-on-exit/</guid><description>
&lt;p&gt;This post is about some emacs configurations which I use in my work environment.&lt;/p&gt;
&lt;div id="outline-container-headline-1" class="outline-2"&gt;
&lt;h2 id="headline-1"&gt;
Vterm query-on-exit blocks shutdown
&lt;/h2&gt;
&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;
&lt;p&gt;In the default configuration, killing a vterm buffer with an idle shell will query the user whether
they really wants to kill the process. An integration like kitty would account for the shell process when
deciding whether killing a session should be silent. The following achieves a similar result for
vterm&amp;#39;s shell integration.&lt;/p&gt;
&lt;p&gt;
Since my emacs starts as user service, failing to kill the buffer due to the above will stall a power-off. Since
there is no possibility to confirm the query, the user is forced to wait for the systemd user service to
timeout on stopping. This will be an annoyance when you&amp;#39;re not careful with the default setting.&lt;/p&gt;
&lt;p&gt;
The solution is simple: emacs has &lt;code&gt;(set-)process-query-on-exit-flag&lt;/code&gt;, which disables the query when
set to &lt;code&gt;nil&lt;/code&gt;. We can toggle the flag when a prompt is printed or a command starts, using OSC 51 message passing.
This is done using an escape sequence, cf. the use cases of &lt;code&gt;vterm_printf&lt;/code&gt; on &lt;span style="text-decoration: underline;"&gt;&lt;a href="https://github.com/akermu/emacs-libvterm"&gt;the GitHub page of emacs-libvterm&lt;/a&gt;&lt;/span&gt;.
See also the standard &lt;span style="text-decoration: underline;"&gt;&lt;a href="https://invisible-island.net/xterm/ctlseqs/ctlseqs.html"&gt;XTerm control sequences&lt;/a&gt;&lt;/span&gt; and Wikipedia article on &lt;span style="text-decoration: underline;"&gt;&lt;a href="https://en.wikipedia.org/wiki/ANSI_escape_code"&gt;ANSI escape codes&lt;/a&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;
During emacs initialisation/using custom.el, we add the following to the list variable &lt;code&gt;vterm-eval-cmds&lt;/code&gt;:&lt;/p&gt;
&lt;div class="src src-elisp"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-elisp" data-lang="elisp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;set-query-on-exit&amp;#34;&lt;/span&gt; (lambda (flag)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; (&lt;span style="color:#a6e22e"&gt;set-process-query-on-exit-flag&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; (&lt;span style="color:#a6e22e"&gt;get-buffer-process&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;current-buffer&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; (not (string= &lt;span style="color:#e6db74"&gt;&amp;#34;nil&amp;#34;&lt;/span&gt; flag)))))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This allows the escape sequence &lt;code&gt;^[]51;E&amp;lt;message&amp;gt;^G&lt;/code&gt; to evaluate the above designated elisp expression
through vterm&amp;#39;s message passing function.
We also add the following to the shell integration script (e.g. for zsh):&lt;/p&gt;
&lt;div class="src src-zsh"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-zsh" data-lang="zsh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;vterm_query_on_exit_on&lt;span style="color:#f92672"&gt;(){&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; vterm_printf &lt;span style="color:#e6db74"&gt;&amp;#34;51;Eset-query-on-exit t&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;vterm_query_on_exit_off&lt;span style="color:#f92672"&gt;(){&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; vterm_printf &lt;span style="color:#e6db74"&gt;&amp;#34;51;Eset-query-on-exit nil&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;set -A precmd_functions vterm_query_on_exit_off $precmd_functions
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;set -A preexec_functions vterm_query_on_exit_on $preexec_functions&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item></channel></rss>