1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta content="pandoc" name="generator"/>
<meta content="Zhiming Wang" name="author"/>
<meta content="2014-12-14T10:06:02-0800" name="date"/>
<title>Speeding up Emacs with emacsclient</title>
<link href="/img/apple-touch-icon-152.png" rel="apple-touch-icon-precomposed"/>
<meta content="#FFFFFF" name="msapplication-TileColor"/>
<meta content="/img/favicon-144.png" name="msapplication-TileImage"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<link href="/css/normalize.min.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/css/theme.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/css/highlight.css" media="all" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="archival-notice">This blog has been archived.<br/>Visit my home page at <a href="https://zhimingwang.org">zhimingwang.org</a>.</div>
<nav class="nav">
<a class="nav-icon" href="/" title="Home"><!--blog icon--></a>
<a class="nav-title" href="/"><!--blog title--></a>
<a class="nav-author" href="https://github.com/zmwangx" target="_blank"><!--blog author--></a>
</nav>
<article class="content">
<header class="article-header">
<h1 class="article-title">Speeding up Emacs with emacsclient</h1>
<div class="article-metadata">
<time class="article-timestamp" datetime="2014-12-14T10:06:02-0800">December 14, 2014</time>
</div>
</header>
<p>Emacs is notorious for its loading time. For me, this is especially annoying when I'm editing LaTeX files — AUCTeX takes about five seconds to load, and once I exit Emacs (especially after a quick edit), all that work is wasted, and next time I want to do some quick editing with that same LaTeX file — sorry, another five seconds.</p>
<p>This problem can be solved by "using that same Emacs", i.e., running Emacs in server mode, then connecting to the server via <code>emacsclient</code>. Below is my script, which I call <code>emc</code>, to make <code>emacsclient</code> more user-friendly. <code>emc</code> opens a file (given as <code>$1</code>) on the server, launching one on its way if none is detected. Note that I used <code>-cqta=</code> for <code>emacsclient</code>. The <code>-c</code> option is <code>--create-frame</code>, i.e., create a new frame (in the current tty, for instance) instead of using the existing frame (in another tty, for instance); this allows for multiple frames accross different ttys. The <code>-q</code> option is for <code>--quiet</code>, suppressing messages like "Waiting for Emacs..." The <code>-t</code> option is for <code>--tty</code>, or equivalently, the familiar <code>-nw</code> option of <code>emacs</code>. The <code>-a=</code> options is <code>--alternate-editor=</code>; according to the manpage, <code>-a, --alternate-editor=EDITOR</code> has the following effect:</p>
<blockquote>
<p>if the Emacs server is not running, run the specified editor instead. This can also be specified via the `ALTERNATE_EDITOR' environment variable. If the value of EDITOR is the empty string, run `emacs --daemon' to start Emacs in daemon mode, and try to connect to it.</p>
</blockquote>
<p>Note that <code>emacsclient</code> requires a filename, so my script prompts for one if <code>$1</code> is empty.</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co">#!/usr/bin/env bash</span>
<span class="kw">if [[</span> <span class="ot">-n</span> <span class="va">$1</span><span class="kw"> ]]</span>; <span class="kw">then</span>
<span class="va">file=$1</span>
<span class="kw">else</span>
<span class="kw">while [[</span> <span class="ot">-z</span> <span class="va">${file}</span><span class="kw"> ]]</span>; <span class="kw">do</span>
<span class="bu">read</span> -p <span class="st">'filename: '</span> <span class="va">file</span>
<span class="kw">done</span>
<span class="kw">fi</span>
<span class="ex">emacsclient</span> -cqta= <span class="st">"</span><span class="va">${file}</span><span class="st">"</span></code></pre></div>
<p>Note that using <code>emacsclient</code> has the additional benefit that the same buffer is simultaneously updated accross different ttys (See screenshot, where I opened the current post in two different ttys). This way, you won't face the nasty "file changed on disk" problem when you accidentally edited the same file in another tty session.</p>
<div class="figure">
<a href="https://i.imgur.com/9KxEWKq.png" target="_blank"><img alt="screen shot of multiple copies of the same buffer" src="https://i.imgur.com/9KxEWKq.png"/></a>
<p class="caption">screen shot of multiple copies of the same buffer</p>
</div>
<p>By the way, remember to re-configure your other programs that uses an external editor. For instance, change <code>$EDITOR</code> to <code>emacsclient -cqta=</code> in your <code>env</code>, and <code>core.editor</code> to <code>emacsclient -cqta=</code> in your <code>~/.gitconfig</code>.</p>
<p><em>Note</em>: if you use <code>emacsclient</code> to edit git commit messages in Git Commit Mode, remember to use <code>C-c C-c</code> (<code>git-commit-commit</code>) to save the commit message rather than using <code>server-edit</code> or <code>C-x C-c</code> to exit Emacs. Otherwise, the <code>COMMIT_EDITMSG</code> buffer will persist in the Emacs server, and you'll be prompted to revert buffer the next time you edit another commit message, which is pretty annoying.</p>
<p>I just started using <code>emacsclient</code>, so the above script might be buggy in certain edge cases. I'll report when I run into issues.</p>
</article>
<hr class="content-separator"/>
<footer class="footer">
<span class="rfooter">
<a class="rss-icon" href="/rss.xml" target="_blank" title="RSS feed"><!--RSS feed icon--></a><a class="atom-icon" href="/atom.xml" target="_blank" title="Atom feed"><!--Atom feed icon--></a><a class="cc-icon" href="https://creativecommons.org/licenses/by/4.0/" target="_blank" title="Released under the Creative Commons Attribution 4.0 International license."><!--CC icon--></a>
<a href="https://github.com/zmwangx" target="_blank">Zhiming Wang</a>
</span>
</footer>
</body>
</html>
|