<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/atom.xsl" type="text/xsl"?><feed
	xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en-US"
	><title>Sacha Chua - tag - read-lisp</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/tag/read-lisp/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/tag/read-lisp" />
  <id>https://sachachua.com/blog/tag/read-lisp/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2014-06-05T12:00:24Z</updated>
<entry>
		<title type="html">Read Lisp, Tweak Emacs [Beginner 1/4]: How to try Emacs Lisp</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2014/06/read-lisp-tweak-emacs-beginner-14-try-emacs-lisp/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2014-06-05T00:29:07Z</updated>
    <published>2014-06-05T12:00:24Z</published>
    <category term="emacs" />
<category term="elisp" />
		<id>https://sachachua.com/blog/?p=27273</id>
		<content type="html"><![CDATA[<div class="conventions">
<p>Okay! People have given lots of great feedback on the e-mail course. It's time to post the course content on my blog so that you can read it here too (or search for it in case you want). =) I'll post these one section at a time, but you can read the current draft at <a href="http://emacslife.com/how-to-read-emacs-lisp.html">http://emacslife.com/how-to-read-emacs-lisp.html</a> if you want to. Any updates will probably be over there too!</p>
<p>Some conventions we'll use:</p>
<ul class="org-ul">
<li>Inline code will be <code>boxed and monospace</code> in the HTML version and generally surrounded by <code>equal signs</code> in plain text.</li>
<li>Code samples will be monospace and in boxes in the HTML version, and enclosed in <code>#+begin_src</code> … <code>#+end_src</code> in plain text. Example:
<div class="org-src-container">
<pre class="src src-emacs-lisp">(message <span class="org-string">"Hello world"</span>)
</pre>
</div>
</li>
</ul>
</div>
<div class="objectives">
<p>After this section, you should be able to</p>
<ul class="org-ul">
<li>find Emacs Lisp code to study and use</li>
<li>try Emacs Lisp code before saving it to your configuration (that way, you don't have to keep restarting Emacs)</li>
<li>add code to your configuration so that it runs whenever Emacs starts</li>
</ul>
</div>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Finding Emacs Lisp code</h2>
<div id="text-1" class="outline-text-2">
<p>It's easier to learn how to read Emacs Lisp when you start with simple examples that will help you use Emacs more effectively. Here are some useful sources:</p>
</div>
<div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1">Emacs documentation</h3>
<div id="text-1-1" class="outline-text-3">
<p>Manuals and FAQs for Emacs-related tools often include code snippets. For example, the <a href="https://www.gnu.org/software/emacs/manual/efaq.html">Emacs FAQ</a> has an entry like this:</p>
<pre class="example">5.47 How can I tell Emacs to fill paragraphs with a single space after each period?
===================================================================================

Add the following line to your `.emacs' file:

     (setq sentence-end-double-space nil)
</pre>
<p>You can read the Emacs manual by typing <code>C-h i</code> (<code>info</code>) and choosing the <b>Emacs</b> item. If you're on a terminal that doesn't understand the <code>C-h</code> key, use <code>F1</code> or <code>M-x help</code> whenever you see a reference to <code>C-h</code>, or use <code>M-x</code> to call the command by name (ex: <code>F1 i</code>, or <code>M-x info</code>). To jump directly to the Emacs manual, you can use <code>C-h r</code> (<code>info-emacs-manual</code>). You can also find the Emacs Manual at <a href="http://www.gnu.org/software/emacs/manual/emacs.html">http://www.gnu.org/software/emacs/manual/emacs.html</a> .</p>
</div>
</div>
<div id="outline-container-sec-1-2" class="outline-3">
<h3 id="sec-1-2">Packages</h3>
<div id="text-1-2" class="outline-text-3">
<p>Emacs has lots of packages in different repositories, many of which require a little extra code in order to be used to full effect. You can use <code>M-x package-list-packages</code> to list the packages that Emacs knows about by default. You will need an Internet connection for that.</p>
<p>If you're new to Emacs, try getting used to Emacs without packages first. There's plenty of functionality already built in. When you come across a gap, chances are that someone has written a package to make Emacs behave the way you want it to. Since there are lots of packages that do similar things, you might want to look for recommendations or ask people which ones you should start with.</p>
<p>In addition to the default package repository, there are other community-supported repositories. See <a href="http://emacslife.com/how-to-read-emacs-lisp.html#installing-packages">Installing packages</a> if you would like to install a package from a different repository.</p>
<p>If you install a package, check out the README, description, documentation, or source code comments for interesting packages to find suggested code to add to your Emacs configuration.</p>
<p>Here are some packages that might be interesting:</p>
<ul class="org-ul">
<li>company: adds text completion</li>
<li>yasnippet: snippets and templates</li>
<li>undo-tree: visualize your undo/redo history</li>
</ul>
<p>You will need to be connected to the Internet in order to view and install packages. You can use <code>M-x package-list-packages</code> to show the available packages and read the descriptions for the packages above.</p>
</div>
</div>
<div id="outline-container-sec-1-3" class="outline-3">
<h3 id="sec-1-3">Webpages, blog posts, and the Emacs Wiki</h3>
<div id="text-1-3" class="outline-text-3">
<p>While searching for information related to Emacs, you'll probably come across lots of Emacs Lisp snippets. The <a href="http://www.emacswiki.org/">EmacsWiki</a> has lots of code, too. Since this is a community-maintained wiki, you may come across code that is out of date or that refers to packages that you don't have. I've included common errors in this guide to help you figure things out &#8211; see <a href="http://emacslife.com/how-to-read-emacs-lisp.html#errors">&#8220;Oh no! I have an error!&#8221;</a></p>
<p>Here are some sites you may want to check out:</p>
<ul class="org-ul">
<li>EmacsWiki: CategoryDotEmacs &#8211; <a href="http://www.emacswiki.org/emacs/CategoryDotEmacs">http://www.emacswiki.org/emacs/CategoryDotEmacs</a></li>
<li>Emacs Redux &#8211; <a href="http://emacsredux.com">http://emacsredux.com</a></li>
<li>What the Emacs.d?! &#8211; <a href="http://whattheemacsd.com/">http://whattheemacsd.com/</a></li>
<li>The very unofficial .emacs home &#8211; <a href="http://www.dotemacs.de/localfiles.html">http://www.dotemacs.de/localfiles.html</a></li>
</ul>
</div>
</div>
<div id="outline-container-sec-1-4" class="outline-3">
<h3 id="sec-1-4">Mailing lists, newsgroups, and Q&amp;A sites</h3>
<div id="text-1-4" class="outline-text-3">
<p>There are many places where you can ask for help with Emacs. gnu.emacs.help is available as a <a href="https://lists.gnu.org/mailman/listinfo/help-gnu-emacs">mailing list</a> or as a newsgroup &#8211; check your favourite Usenet server or use <a href="http://dir.gmane.org/gmane.emacs.help">Gmane</a>. <a href="http://stackoverflow.com/questions/tagged/emacs">StackOverflow</a> and <a href="http://www.quora.com/Emacs">Quora</a> are popular as well. If you ask questions there, you might get answers in the form of Emacs Lisp code. You'll also come across Emacs Lisp code while searching for answers.</p>
<p>Find a snippet of Emacs Lisp code you want to understand more deeply, or look at the examples in the sections below.</p>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Trying out code</h2>
<div id="text-2" class="outline-text-2">
<p>It's easier to understand code if you can experiment with it. Emacs Lisp code is made up of <b>symbolic expressions</b> (known as S-expressions, expressions, or sexp for short). Expressions are usually enclosed in pairs of parentheses. There are several ways you can try Emacs Lisp expressions before saving them in your configuration.</p>
<p>Note: As you experiment with Emacs Lisp, you might run into errors. Check out <a href="http://emacslife.com/how-to-read-emacs-lisp.html#errors">&#8220;Oh no! I have an error!&#8221;</a> for some common errors and what to do about them.</p>
<p>Here are some ways you can run Emacs Lisp code. I'll explain them in more detail below.</p>
<ul class="org-ul">
<li><code>M-x ielm</code> (Inferior Emacs Lisp Mode) &#8211; evaluates expressions after you press <code>RET</code>, which is handy for pasting in code</li>
<li>The <code>*scratch*</code> buffer and Emacs Lisp files &#8211; makes it easy to edit and re-evaluate expressions</li>
<li><code>M-:</code> (<code>eval-expression</code>) &#8211; good for quickly evaluating Emacs Lisp code while you're doing something else</li>
<li><code>C-x C-e</code> (<code>eval-last-sexp</code>) &#8211; handy when reading expressions in manuals or other text</li>
</ul>
</div>
<div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1">M-x ielm (Inferior Emacs Lisp Mode)</h3>
<div id="text-2-1" class="outline-text-3">
<p>The Inferior Emacs Lisp Mode gives you a prompt where you can type or paste in Emacs Lisp code. Start it with <code>M-x ielm</code>. Press <code>RET</code> after you enter code, and the results will be displayed. &#8220;Inferior&#8221; is a technical term referring to how it's run, not a comment on the simplicity of the tool or the code you want to try. You can go to previously-executed code, change things, and press <code>RET</code> to run (or &#8220;evaluate&#8221;) it again.</p>
<p>If you're copying or typing code, make sure your parentheses are all matched &#8211; every &#8220;<code>(</code>&#8221; should have a &#8220;<code>)</code>&#8220;. IELM won't run the code unless it sees the closing parenthesis. So the following code is incomplete:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(message <span class="org-string">"Hello</span>
</pre>
</div>
<p>but this will work:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(message <span class="org-string">"Hello world"</span>)
</pre>
</div>
<p>You can use <code>M-p</code> (<code>comint-previous-input</code>) to go through the previously-typed expressions. <code>M-n</code> (<code>comint-next-input</code>) goes forward.</p>
<p>Tip: When you're trying out an unfamiliar mode, use <code>C-h m</code> (<code>describe-mode</code>) to learn more about the commands that are available in that mode.</p>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3">
<h3 id="sec-2-2">The <code>*scratch*</code> buffer and Emacs Lisp <code>.el</code> files</h3>
<div id="text-2-2" class="outline-text-3">
<p>When Emacs starts, it creates a buffer called <code>*scratch*</code> with the following contents:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-comment-delimiter">;; </span><span class="org-comment">This buffer is for notes you don't want to save, and for Lisp evaluation.</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">If you want to create a file, visit that file with C-x C-f,</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">then enter the text in that file's own buffer.</span>
</pre>
</div>
<p>You can add code to the end.</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-comment-delimiter">;; </span><span class="org-comment">This buffer is for notes you don't want to save, and for Lisp evaluation.</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">If you want to create a file, visit that file with C-x C-f,</span>
<span class="org-comment-delimiter">;; </span><span class="org-comment">then enter the text in that file's own buffer.</span>

(message <span class="org-string">"Hello world"</span>)
</pre>
</div>
<p>Note: <code>;</code> is the comment character. Anything after the comment character is considered part of the comment. Make sure you add your code on a new line, not in the comment. <code>;;</code> above is how we usually start comments that take up the entire line.</p>
<p>To run code (&#8220;evaluate&#8221; it, in Emacs terms), you can use the following commands based on what you want to run:</p>
<ul class="org-ul">
<li><code>M-x eval-buffer</code> runs all the code in the current file or buffer.</li>
<li><code>M-x eval-region</code> runs the code in the selected region. You can select a region of text by using the mouse. Alternatively, you can type <code>C-SPC</code> to mark the start of the region, then move your cursor to the end of the region. If you want to learn more about selecting regions, check out the Emacs tutorial (<code>C-h t</code>, <code>C-h t</code>, or <code>M-x help-with-tutorial</code>).</li>
<li><code>C-x C-e</code> (<code>eval-last-sexp</code>) runs the expression (S-expression, or sexp) before the cursor. NOTE: Your cursor should be after the closing parenthesis, not on it.</li>
</ul>
<p>In the <code>*scratch*</code> buffer, you can also press <code>C-j</code> (<code>eval-print-last-sexp</code>) after an expression in order to evaluate it and display the results in the buffer.</p>
<p>The <code>*scratch*</code> buffer is not automatically saved. If you would like to save your code for future use, you can create a file with an <code>.el</code> ending. <code>el</code> stands for Emacs Lisp, and Emacs will open these files in Emacs Lisp mode. The commands listed above work in Emacs Lisp files.</p>
</div>
</div>
<div id="outline-container-sec-2-3" class="outline-3">
<h3 id="sec-2-3">M-: (eval-expression)</h3>
<div id="text-2-3" class="outline-text-3">
<p>If you want to quickly try a single expression, you can use <code>M-:</code> (<code>eval-expression</code>). It will display the results in the echo area near the bottom of your screen. If you want to insert the results into your buffer, call it with <code>C-u M-:</code> instead. For example, you can use <code>C-u M-: (* 18 2)</code> to multiply 18 and 2 quickly. To review previous results, you can switch to the <code>*Messages*</code> buffer.</p>
</div>
</div>
<div id="outline-container-sec-2-4" class="outline-3">
<h3 id="sec-2-4">C-x C-e (eval-last-sexp)</h3>
<div id="text-2-4" class="outline-text-3">
<p><code>C-x C-e</code> (<code>eval-last-sexp</code>) runs the expression (S-expression, or sexp) before the cursor. NOTE: Your cursor should be after the closing parenthesis, not on it. <code>C-x C-e</code> (<code>eval-last-sexp</code>) works in lots of buffers, not just in Emacs Lisp ones. You can use it to quickly try expressions while reading manual pages or other documentation.</p>
<p>You can get the text file for this lesson from <a href="http://emacslife.com/read-lisp-tweak-emacs/beginner-1-try-emacs-lisp.txt">http://emacslife.com/read-lisp-tweak-emacs/beginner-1-try-emacs-lisp.txt</a> . If you're reading this lesson in Emacs, try putting your cursor after the closing <code>)</code> and calling <code>C-x C-e</code> (<code>eval-last-sexp</code>) on the following line:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(menu-bar-mode -1)
</pre>
</div>
<p>This turns off the menu bar along the top of your Emacs window. If you like the menu bar, you can turn it on again by evaluating:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(menu-bar-mode 1)
</pre>
</div>
<p>As with <code>M-:</code> (<code>eval-expression</code>), you can use the <code>C-u</code> prefix to insert the results instead of displaying them. For example, try using <code>C-u C-x C-e</code> (<code>eval-last-sexp</code>) to evaluate the following expression:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(* 6 7)
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-5" class="outline-3">
<h3 id="sec-2-5">If you want that code to run every time you start Emacs…</h3>
<div id="text-2-5" class="outline-text-3">
<p>… <b>then</b> add it to your <code>~/.emacs.d/init.el</code> file. You can generally add new code at the end. If the code has something to do with <code>add-to-list</code> and <code>load-path</code>, it might be good to add it to the beginning instead.</p>
<p>Note: The Emacs configuration file used to be <code>~/.emacs</code>, and most webpages refer to that. <code>~/.emacs</code> still works &#8211; in fact, if you have that, it may stop Emacs from loading <code>~/.emacs.d/init.el</code>. On the other hand, if you use <code>~/.emacs.d/init.el</code> (and move your <code>~/.emacs</code> code to that file instead), then you have one less hidden file in your home directory (<code>~</code>). If you're adding code to your config and it's not getting loaded, make sure you have either <code>~/.emacs</code> or <code>~/.emacs.d/init.el</code>, but not both.</p>
<p>When you're starting out, it's a good idea to keep your configuration in one file. Later on, you can split it up into multiple files if you want.</p>
</div>
</div>
<div id="outline-container-sec-2-6" class="outline-3">
<h3 id="sec-2-6">Practice</h3>
<div id="text-2-6" class="outline-text-3">
<p>Try this out:</p>
<p>Next module: <a href="http://emacslife.com/read-lisp-tweak-emacs/beginner-2-understand-emacs-lisp.html">&#8220;How can I understand what Emacs Lisp code does?&#8221;</a></p>
<div class="practice">
<ol class="org-ol">
<li>Browse through <a href="http://www.emacswiki.org/emacs/CategoryDotEmacs">http://www.emacswiki.org/emacs/CategoryDotEmacs</a> for some code that looks like it will make Emacs work more like the way you want it to. If you need help figuring it out, send me a copy of the code (sacha@sachachua.com).</li>
<li>Use <code>M-x ielm</code> to evaluate this expression interactively:
<div class="org-src-container">
<pre class="src src-emacs-lisp">system-type
</pre>
</div>
<p>This shows you the value of the <code>system-type</code> variable. Note: Variables can be evaluated without the parentheses, but functions can't. Use You can use <code>ielm</code> to call functions like this, though:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(max 2 6 10 5)
</pre>
</div>
</li>
<li>Use <code>M-:</code> (<code>eval-expression</code>) buffer to evaluate the following expression:
<div class="org-src-container">
<pre class="src src-emacs-lisp">(* 21 2)
</pre>
</div>
<p><code>M-:</code> is handy for quick calculations. Remember, you can use it with<br>
<code>C-u</code> (that is, <code>C-u M-:</code>) to <b>insert</b> the result into the buffer.<br>
Try it now: <code>C-u M-: (* 21 2)</code></p></li>
<li>Use <code>C-x C-e</code> (<code>eval-last-sexp</code>) to evaluate the expression below. If you are reading this in Emacs, you can evaluate it by putting your cursor after the last <code>)</code> and calling <code>C-x C-e</code> (<code>eval-last-sexp</code>). If you are reading this outside Emacs, you can copy that text into any buffer and then use <code>C-x C-e</code> (<code>eval-last-sexp</code>) to evaluate it.
<div class="org-src-container">
<pre class="src src-emacs-lisp">(* (+ 1 2) 3)
</pre>
</div>
</li>
<li>Add <code>(message "Hello, world!")</code> to the end of your <code>~/.emacs.d/init.el</code> (or <code>~/.emacs</code>, if you're using that file instead). Use <code>M-x eval-buffer</code> to load your config. It should display the message near the bottom of your screen. If you restart Emacs, you should also see that message briefly.</li>
<li>Call <code>M-x visual-line-mode</code> to tell Emacs to visually wrap lines without actually changing the text. If you like this, add <code>(visual-line-mode)</code> to your <code>~/.emacs.d/init.el</code>.</li>
</ol>
</div>
</div>
</div>
</div>
]]></content>
		</entry>
</feed>