<!--page.vmd-->
<div id="content" class="page view">
<div id="link-to-page-fields" class="hidden parameters">
<input type="hidden" id="linkToThisPageHeading" value="Link to this Page">
<input type="hidden" id="linkToThisPageLink" value="Link">
<input type="hidden" id="linkToThisPageTinyLink" value="Tiny Link">
<input type="hidden" id="linkToThisPageWikiMarkup" value="Wiki Markup">
<input type="hidden" id="linkToThisPageClose" value="Close">
</div>
<fieldset class="hidden parameters">
<input type="hidden" title="movePageDialogViewPageTitle" value="Move Page – ‘Emacs on Athena’">
<input type="hidden" title="movePageDialogEditPageTitle" value="Set Page Location">
<input type="hidden" title="movePageDialogMoveButton" value="Move">
<input type="hidden" title="movePageDialogCancelButton" value="Cancel">
<input type="hidden" title="movePageDialogBrowsePanelTip" value="Click to select the new parent page for this page and its children.">
<input type="hidden" title="movePageDialogSearchPanel" value="Search">
<input type="hidden" title="movePageDialogHistoryPanel" value="Recently Viewed">
<input type="hidden" title="movePageDialogHistoryNoResults" value="There were no recently viewed pages found.">
<input type="hidden" title="movePageDialogLocationPanel" value="Known Location">
<input type="hidden" title="movePageDialogLocationNotFound" value="The specified page was not found.">
<input type="hidden" title="movePageDialogBrowsePanel" value="Browse">
<input type="hidden" title="movePageDialogPanelLoadErrorMsg" value="Error reading the panel content from the server.">
<input type="hidden" title="movePageDialogPanelLoadErrorTip" value="You could try reloading the page and launching the dialog again.">
<input type="hidden" title="movePageDialogPanelLoadErrorStatus" value="HTTP Status">
<input type="hidden" title="movePageDialogNoSelectionErrorMsg" value="You must make a selection in the tree before you can move the page.">
<input type="hidden" title="movePageDialogSearchError" value="Failed to retrieve search results from the server.">
<input type="hidden" title="movePageDialogSearchNoResults" value="There were no pages found containing <b>{0}</b>.">
<input type="hidden" title="movePageDialogSearchResultCount" value="Showing <b>{0}</b>-<b>{1}</b> of <b>{2}</b> pages containing <b>{3}</b>.">
<input type="hidden" title="movePageDialogMoveFailed" value="Move failed. There was a problem contacting the server.">
<input type="hidden" title="movePageDialogCannotChangeSpace" value="You cannot move this page to another space because you do not have permission to remove it from this space.">
<input type="hidden" title="pageTitle" value="Emacs on Athena"/>
<input type="hidden" title="parentPageTitle" value=""/>
<input type="hidden" title="fromPageTitle" value=""/>
<input type="hidden" title="spaceKey" value="istcontrib"/>
<input type="hidden" title="spaceName" value="IS&T Contributions"/>
<input type="hidden" title="movePageDialogInvalidLocation" value="You cannot move a page to be underneath itself or its children."/>
<input type="hidden" title="movePageDialogOrderingTitle" value="Page Ordering"/>
<input type="hidden" title="movePageDialogBackButton" value="Back"/>
<input type="hidden" title="movePageDialogMoveAndOrderButton" value="Reorder"/>
<input type="hidden" title="movePageDialogNextButton" value="Move"/>
</fieldset>
<script type="text/x-template" title="movePageDialog">
<div class="row information">
<div class="inner">
<div class="element">
Specify the new parent page for this page and its children by space and title.
</div>
</div>
</div>
<div class="form">
<fieldset>
<legend class="assistive"><span>Change the Parent Page to a Known Page</span></legend>
<div class="row">
<label for="new-space">New space:</label>
<div class="value new-space-value">
<input id="new-space-key" name="new-space-key" type="hidden" value="istcontrib">
<span class="space-input">
<input id="new-space" name="new-space" value="IS&T Contributions" disabled="disabled">
</span>
<span class="description warning">You cannot move this page to another space because you do not have permission to remove it from this space.</span>
<div class="new-space-dropdown aui-dd-parent autocomplete"></div>
</div>
</div>
<div class="row">
<label for="new-parent-page">New parent page:</label>
<div class="value new-parent-page-value">
<span class="page-input">
<input id="new-parent-page" name="new-parent-page" value="">
</span>
<span class="description">Start typing a page title to see a list of suggestions.</span>
<div class="new-parent-page-dropdown aui-dd-parent autocomplete"></div>
</div>
</div>
</fieldset>
</div>
<div class="location-info">
<div class="row">
<label>Current location:</label>
<div class="value breadcrumbs-container">
<div class="breadcrumbs-line">
<ul id="current-parent-breadcrumbs" class="breadcrumbs">
</ul>
</div>
</div>
</div>
<div class="row">
<label>New location:</label>
<div class="value breadcrumbs-container">
<div class="breadcrumbs-line">
<ul id="new-parent-breadcrumbs" class="breadcrumbs">
</ul>
</div>
</div>
</div>
</div>
</script>
<script type="text/x-template" title="movePageErrors">
<div id="move-errors" class="hidden warning"></div>
</script>
<script type="text/x-template" title="movePageBreadcrumb">
<li><a class="{2}" title="{3}" tabindex="-1"><span>{0}</span></a></li>
</script>
<script type="text/x-template" title="movePageBreadcrumbLoading">
<li class="loading"><span>Loading breadcrumbs…</span></li>
</script>
<script type="text/x-template" title="movePageBreadcrumbError">
<li class="warning last"><span>Error retrieving breadcrumbs.</span></li>
</script>
<script type="text/x-template" title="movePageNoMatchingPages">
<ol><li><span class="warning">No matching pages found.</span></li></ol>
</script>
<script type="text/x-template" title="movePageNoMatchingSpaces">
<ol><li><span class="warning">No matching spaces found.</span></li></ol>
</script>
<script type="text/x-template" title="movePageSearchPanel">
<div class="row information">
<div class="inner">
<div class="element">
Search for and select the new parent page for this page and its children.
</div>
</div>
</div>
<div id="move-page-search-container" class="row">
<div class="search-form">
<fieldset>
<legend class="assistive"><span>Search for a New Parent Page</span></legend>
<label for="move-page-search-query" class="assistive">Search keywords</label>
<input class="search-query" id="move-page-search-query">
<label for="move-page-search-space" class="assistive">Search in space</label>
<select id="move-page-search-space" class="search-space" disabled="disabled">
<option value="istcontrib" selected="selected">IS&T Contributions</option>
</select>
<input type="button" value="Search">
<div class="description warning">You cannot move this page to another space because you do not have permission to remove it from this space.</div>
</fieldset>
</div>
<div class="search-results">
</div>
</div>
</script>
<script type="text/x-template" title="movePageSearchResultsLoading">
<div class="searching">Searching…</div>
</script>
<script type="text/x-template" title="movePageHistoryPanel">
<div class="row information">
<div class="inner">
<div class="element">
Select the new parent page for this page and its children from your history.
</div>
</div>
</div>
<div id="move-page-search-container" class="row">
<div class="search-results">
</div>
</div>
</script>
<script type="text/x-template" title="movePageHistoryLoading">
<div class="searching">Loading…</div>
</script>
<script type="text/x-template" title="movePageBrowsePanel">
<div class="row information">
<div class="inner">
<div class="element">
Click to select the new parent page for this page and its children.
</div>
</div>
</div>
<div class="tree"></div>
</script>
<script type="text/x-template" title="movePagePanelLoading">
<span>Loading…</span>
</script>
<script type="text/x-template" title="movePageBrowsePanelSpace">
<ul><li id='tree-root-node-item' class='root-node-list-item'><a class='root-node' href='#'>{0}</a></li></ul>
</script>
<script type="text/x-template" title="orderingPagePanel">
<div id="orderingPlaceHolder"></div>
</script>
<script type="text/x-template" title="reorderCheckbox">
<span id="reorderRequirement"><input id="reorderCheck" type="checkbox" name="reorderFlag" title="Choose the position of this page within the list of child pages."/><label for="reorderCheck" title="Choose the position of this page within the list of child pages.">Reorder</label></span>
</script>
<script type="text/x-template" title="move-help-link">
<div class="dialog-help-link">
<a href="http://docs.atlassian.com/confluence/docs-35/Moving+a+Page" target="_blank">Help</a>
</div>
</script>
<script type="text/x-template" title="searchResultsGrid">
<table>
<thead>
<tr class="header">
<th class="search-result-title">Page Title</th>
<th class="search-result-space">Space</th>
<th class="search-result-date">Updated</th>
</tr>
</thead>
</table>
</script>
<script type="text/x-template" title="searchResultsGridCount">
<p class="search-result-count">{0}</p>
</script>
<script type="text/x-template" title="searchResultsGridRow">
<tr class="search-result">
<th class="search-result-title"><a href="{1}" class="content-type-{2}"><span>{0}</span></a></th>
<td class="search-result-space"><a class="space" href="/confluence/display/{4}/" title="{3}">{3}</a></td>
<td class="search-result-date"><span class="date" title="{6}">{5}</span></td>
</tr>
</script>
<!-- Start restrictions section -->
<script type="text/x-template" title="page-permissions-div">
<div id="page-permissions-div">
<div id="page-permissions-editor-form">
<div id="page-permissions-error-div" class="hidden">
<a href="#" id="permissions-error-div-close">Ok</a>
<div></div>
</div>
<div id="page-permissions-type-radios" class="page-permissions-label-rows">
<div>
<input id="restrictViewRadio" type="radio" checked="checked" name="pagePermissionTypeRadio" value="view"/>
<label for="restrictViewRadio">Restrict viewing of this page</label>
<input id="restrictEditRadio" type="radio" name="pagePermissionTypeRadio" value="edit"/>
<label for="restrictEditRadio">Restrict editing of this page</label>
</div>
</div>
<div id="page-permissions-input" class="page-permissions-label-rows">
<div class="page-permissions-label">To:</div>
<div id="page-permissions-chooser-box">
<span id="page-permissions-choose-user" class="ajs-button">
<a href="#" id='userpicker-popup-link-image' onClick="var picker = window.open('/confluence/spaces/openuserpicker.action?key=istcontrib&startIndex=0&onPopupSubmit=AJS.PagePermissions.addUserPermissions', 'EntitiesPicker', 'status=yes,resizable=yes,top=100,left=200,width=700,height=680,scrollbars=yes'); picker.focus(); return false;"><img src="/confluence/s/en/2171/1/_/images/icons/user_16.gif" height=16 width=16 border=0 align="absmiddle" title="Choose users" /></a>
<a href="#" id='userpicker-popup-link-text' onClick="var picker = window.open('/confluence/spaces/openuserpicker.action?key=istcontrib&startIndex=0&onPopupSubmit=AJS.PagePermissions.addUserPermissions', 'EntitiesPicker', 'status=yes,resizable=yes,top=100,left=200,width=700,height=680,scrollbars=yes'); picker.focus(); return false;">Person...</a>
</span>
<span id="page-permissions-choose-group" class="ajs-button">
<a href="#" id='grouppicker-popup-link-image' onClick="var picker = window.open('/confluence/spaces/opengrouppicker.action?key=istcontrib&startIndex=0&actionName=dosearchgroups.action&onPopupSubmit=AJS.PagePermissions.addGroupPermissions', 'EntitiesPicker', 'status=yes,resizable=yes,top=100,left=200,width=580,height=550,scrollbars=yes'); picker.focus(); return false;"><img src="/confluence/s/en/2171/1/_/images/icons/group_16.gif" height=16 width=16 border=0 align="absmiddle" title="Choose groups" /></a>
<a href="#" id='grouppicker-popup-link-text' onClick="var picker = window.open('/confluence/spaces/opengrouppicker.action?key=istcontrib&startIndex=0&actionName=dosearchgroups.action&onPopupSubmit=AJS.PagePermissions.addGroupPermissions', 'EntitiesPicker', 'status=yes,resizable=yes,top=100,left=200,width=580,height=550,scrollbars=yes'); picker.focus(); return false;">Group...</a>
</span>
</div>
<div id="page-permissions-input-box">
<span>
<input type="text" id="page-permissions-names-input" class="input-placeholder" value="Enter user or group name" name="permissionNames" size="30" autocomplete="off"/>
</span>
<input
type="hidden"
id="page-permissions-names-hidden" /> <img height="16px" width="1px" src="/confluence/s/en/2171/1/_/images/border/spacer.gif"/>
<input type="button" id="add-typed-names" value="Restrict">
</div>
</div>
</div>
<div id="page-permissions-tables">
<div id="page-permissions-table-div">
<table id="page-permissions-table" class="page-permissions-table">
<tr id="page-permissions-no-views" class="marker-row">
<td colspan="3" class="page-permissions-marker-cell"><span>No view restrictions are defined for this page</span></td>
</tr>
<tr id="page-permissions-no-edits" class="marker-row">
<td colspan="3" class="page-permissions-marker-cell"><span>No edit restrictions are defined for this page</span></td>
</tr>
</table>
</div>
<div id="page-inherited-permissions-table-div" class="hidden">
<span id="page-inherited-permissions-table-desc">
<a class="icon twisty-closed">Show/Hide</a>
<a id="toggle-inherited-permissions" title="Click to see inherited restrictions">This page has restricted parent pages. It can only be seen by users who can see those parent pages.</a>
</span>
<div id="page-inherited-permissions-tables" class="hidden page-inheritance-togglable"></div>
</div>
</div>
</div>
</script>
<script type="text/x-template" title="permissions-row-template">
<tr class="permission-row">
<td class="page-permissions-marker-cell" width="20%">
<span>Viewing restricted to:</span>
</td>
<td class="permission-entity" nowrap="true" width="40%">
<span class="entity-container">
<img class="permission-entity-picture"/>
<span class="permission-entity-display-name"></span>
<span class="permission-entity-name-wrap"> (<span class="permission-entity-name"></span>)</span>
</span>
</td>
<td class="permission-detail-column">
<div class="permission-remove-div">
<a href="#" class="remove-permission-link">Remove restriction</a>
</div>
</td>
</tr>
</script>
<script type="text/x-template" title="permissions-username-no-suggestion-template">
<ol>
<li><a href="#" class="message"><span>No matches</span></a></li>
</ol>
</script>
<script type="text/x-template" title="page-inherited-permissions-table-div-template">
<div class="page-inherited-permissions-owner-div">
<div class="page-inherited-permissions-table-desc">Viewing restrictions apply to “<a></a>”. In order to see “<span></span>”, a user must be in the following list of users and groups:</div>
<table class="page-permissions-table"></table>
</div>
</script>
<script type="text/x-template" title="page-restrictions-help-link">
<div class="dialog-help-link">
<a href="http://docs.atlassian.com/confluence/docs-35/Page+Restrictions" target="_blank">Help</a>
</div>
</script>
<!-- End restrictions section -->
<fieldset class="hidden parameters">
<input type="hidden" title="spaceKeyEncoded" value="istcontrib">
<input type="hidden" title="spaceKeyDecoded" value="istcontrib">
</fieldset>
<fieldset class="hidden parameters">
<input type="hidden" title="browsePageTreeMode" value="view">
</fieldset>
<div class="wiki-content">
<!-- wiki content -->
<h1><a name="EmacsonAthena-"></a>Emacs on Athena</h1>
<p>On this page:</p>
<style type='text/css'>/*<![CDATA[*/
div.rbtoc1470067615964 {margin-left: 0px;padding: 0px;}
div.rbtoc1470067615964 ul {list-style: none;margin-left: 0px;}
div.rbtoc1470067615964 li {margin-left: 0px;padding-left: 0px;}
/*]]>*/</style><div class='rbtoc1470067615964'>
<ul>
<li><a href='#EmacsonAthena-Overview'>Overview</a></li>
<li><a href='#EmacsonAthena-GettingStarted'>Getting Started</a></li>
<ul>
<li><a href='#EmacsonAthena-StartingEmacs'>Starting Emacs</a></li>
<li><a href='#EmacsonAthena-PartsoftheEmacsWindow'>Parts of the Emacs Window</a></li>
<li><a href='#EmacsonAthena-UsingEmacsCommands'>Using Emacs Commands</a></li>
<ul>
<li><a href='#EmacsonAthena-CommandSyntax'>Command Syntax</a></li>
<li><a href='#EmacsonAthena-TheMenuBar'>The Menu Bar</a></li>
<li><a href='#EmacsonAthena-WorkingintheMinibuffer'>Working in the Minibuffer</a></li>
<li><a href='#EmacsonAthena-InterruptingandUndoingCommands'>Interrupting and Undoing Commands</a></li>
</ul>
<li><a href='#EmacsonAthena-MovingtheCursor'>Moving the Cursor</a></li>
<li><a href='#EmacsonAthena-SavingYourWork'>Saving Your Work </a></li>
<li><a href='#EmacsonAthena-ExitingEmacs'>Exiting Emacs </a></li>
</ul>
<li><a href='#EmacsonAthena-BasicEditingCommands'>Basic Editing Commands </a></li>
<ul>
<li><a href='#EmacsonAthena-CreatingorFindingaFile'>Creating or Finding a File </a></li>
<li><a href='#EmacsonAthena-InsertingText'>Inserting Text </a></li>
<li><a href='#EmacsonAthena-InsertingFiles'>Inserting Files </a></li>
<li><a href='#EmacsonAthena-AboutBuffers'>About Buffers </a></li>
<li><a href='#EmacsonAthena-TheScratchBuffer'>The Scratch Buffer </a></li>
<li><a href='#EmacsonAthena-CheckingSpelling'>Checking Spelling </a></li>
<li><a href='#EmacsonAthena-FillingandLineWrappingwithinMargins'>Filling and Line Wrapping within Margins </a></li>
<li><a href='#EmacsonAthena-OtherTextManipulationCommands'>Other Text Manipulation Commands </a></li>
</ul>
<li><a href='#EmacsonAthena-Deleting%2CMoving%2CandCopyingText'>Deleting, Moving, and Copying Text </a></li>
<ul>
<li><a href='#EmacsonAthena-DeletingandKillingText'>Deleting and Killing Text </a></li>
<li><a href='#EmacsonAthena-MarkingRegions'>Marking Regions </a></li>
<li><a href='#EmacsonAthena-KillingRegions'>Killing Regions </a></li>
<li><a href='#EmacsonAthena-RestoringText%3ATheYankCommands'>Restoring Text: The Yank Commands </a></li>
<li><a href='#EmacsonAthena-WorkingwithRectangles'>Working with Rectangles </a></li>
</ul>
<li><a href='#EmacsonAthena-SearchingForandReplacingText'>Searching For and Replacing Text </a></li>
<ul>
<li><a href='#EmacsonAthena-UsingSearchCommands'>Using Search Commands </a></li>
<li><a href='#EmacsonAthena-SearchinginaBuffer'>Searching in a Buffer </a></li>
<li><a href='#EmacsonAthena-SearchingandReplacing%28queryreplace%29'>Searching and Replacing (query-replace) </a></li>
<li><a href='#EmacsonAthena-SearchinginSeveralFilesUsinggrep'>Searching in Several Files Using grep </a></li>
<li><a href='#EmacsonAthena-SearchinginPrograms%3AUsingTagTables'>Searching in Programs: Using Tag Tables </a></li>
</ul>
<li><a href='#EmacsonAthena-MultipleBuffersandWindows'>Multiple Buffers and Windows </a></li>
<ul>
<li><a href='#EmacsonAthena-WorkingwithMultipleBuffers'>Working with Multiple Buffers </a></li>
<li><a href='#EmacsonAthena-WorkingwithMultipleWindows'>Working with Multiple Windows </a></li>
<li><a href='#EmacsonAthena-WorkingwithSurpriseBuffersandWindows'>Working with Surprise Buffers and Windows </a></li>
</ul>
<li><a href='#EmacsonAthena-BufferModes'>Buffer Modes </a></li>
<ul>
<li><a href='#EmacsonAthena-MajorModes'>Major Modes </a></li>
<li><a href='#EmacsonAthena-TeXMode'>TeX Mode </a></li>
<li><a href='#EmacsonAthena-EditingDirectories%3ADiredMode'>Editing Directories: Dired Mode </a></li>
<li><a href='#EmacsonAthena-MinorModes'>Minor Modes </a></li>
<li><a href='#EmacsonAthena-UsingAbbreviations%3Aabbrevmode'>Using Abbreviations: abbrev-mode </a></li>
<li><a href='#EmacsonAthena-DefiningAbbreviations'>Defining Abbreviations </a></li>
<li><a href='#EmacsonAthena-UsingAbbreviations'>Using Abbreviations </a></li>
</ul>
<li><a href='#EmacsonAthena-CustomizingYourEmacsEnvironment'>Customizing Your Emacs Environment </a></li>
<ul>
<li><a href='#EmacsonAthena-EmacsVariables'>Emacs Variables </a></li>
<li><a href='#EmacsonAthena-MakingChangesPermanentUsingYour.emacsFile'>Making Changes Permanent Using Your .emacs File </a></li>
</ul>
<li><a href='#EmacsonAthena-AdvancedTechniques'>Advanced Techniques </a></li>
<ul>
<li><a href='#EmacsonAthena-UsinganExistingEmacs%28emacsclient%29'>Using an Existing Emacs (emacsclient) </a></li>
<li><a href='#EmacsonAthena-IssuingSophisticatedCommands'>Issuing Sophisticated Commands </a></li>
<li><a href='#EmacsonAthena-KeyboardMacros'>Keyboard Macros </a></li>
<ul>
<li><a href='#EmacsonAthena-UsingKeyboardMacros'>Using Keyboard Macros </a></li>
<li><a href='#EmacsonAthena-CustomizingCommandKeyBindings'>Customizing Command Key Bindings </a></li>
</ul>
<li><a href='#EmacsonAthena-EditingandCompilingProgramsinEmacs'>Editing and Compiling Programs in Emacs </a></li>
<ul>
<li><a href='#EmacsonAthena-LanguageModes'>Language Modes </a></li>
<li><a href='#EmacsonAthena-CompilingWithinEmacs'>Compiling Within Emacs </a></li>
<li><a href='#EmacsonAthena-FindingCompilationErrors'>Finding Compilation Errors </a></li>
</ul>
<li><a href='#EmacsonAthena-VersionControl'>Version Control </a></li>
<li><a href='#EmacsonAthena-SecondaryCommandsofVC'>Secondary Commands of VC</a></li>
<li><a href='#EmacsonAthena-UsingShellCommandsfromWithinEmacs'>Using Shell Commands from Within Emacs </a></li>
<li><a href='#EmacsonAthena-MoreThingstoDoWithinEmacs'>More Things to Do Within Emacs </a></li>
</ul>
<li><a href='#EmacsonAthena-ErrorRecovery'>Error Recovery </a></li>
<ul>
<li><a href='#EmacsonAthena-DealingwithSystemFailures'>Dealing with System Failures </a></li>
<li><a href='#EmacsonAthena-WorkstationFailure'>Workstation Failure </a></li>
<li><a href='#EmacsonAthena-NetworkandFileServerFailures'>Network and File Server Failures </a></li>
<li><a href='#EmacsonAthena-RecoveringLostFiles'>Recovering Lost Files </a></li>
</ul>
<li><a href='#EmacsonAthena-GettingHelpandMoreInformation'>Getting Help and More Information </a></li>
<li><a href='#EmacsonAthena-RelatedLinks'>Related Links</a></li>
</ul></div>
<h2><a name="EmacsonAthena-Overview"></a>Overview</h2>
<p>Learning how to use a text editor is an important skill you can acquire on any computer system, including Athena. You need to know an editor before you can use many other facilities on Athena, including formatting text and programming.</p>
<p>This document gets you started using the GNU Emacs text editor (Athena's primary supported editor), covering fundamentals of Emacs such as how to start and exit Emacs and how to edit files. It also covers advanced features that help you edit files efficiently and perform multiple tasks (not just text editing) within a single Emacs process. Since Emacs is primarily used for word processing, the emphasis in this documentation is on doing word processing quickly and efficiently, and getting through some of the more common trouble spots.</p>
<p>Emacs is a complex editor with a myriad of features. Choose different topics to learn during different sessions. No one uses every feature of Emacs.</p>
<p>This Emacs documentation assumes that you are familiar with concepts of working with files and directories, using the X Window System to manipulate windows on your workstation screen, and understanding special keys and control characters. For more help, type <b>help</b> at the Athena prompt.</p>
<p>The Consulting Office and Copy Tech also stock the <b>Emacs on Athena Reference Card</b> (AC-43), which lists most of the common Emacs commands.</p>
<p>The definitive reference for Emacs users is the <b>GNU Emacs Manual</b>. This book covers Emacs thoroughly, and is available for reference in the Athena Reference Sets in Hayden and Barker libraries, and on the <a href="http://www.gnu.org/software/emacs/manual/html_mono/emacs.html" class="external-link" rel="nofollow">Web</a>.</p>
<h2><a name="EmacsonAthena-GettingStarted"></a>Getting Started</h2>
<p>Here, we tell you the commands which you need to run Emacs at a basic level.</p>
<h3><a name="EmacsonAthena-StartingEmacs"></a>Starting Emacs</h3>
<p>To start the Emacs editor, you enter a form of the <b>emacs</b> command at the Athena prompt.</p>
<p>If you are using a workstation with the X Window System, including an & at the end of the line:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>athena% <b>emacs &</b></tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
will allow you to continue working in that window. You can also start Emacs from the Text submenu of the Text/Graphics menu of the Dash menu bar.</p>
<p>If you don't have multiple windows (e.g., via dialup), don't include an & at the end of the line:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>athena% <b>emacs</b></tt></p>
<p>When you enter the <b>emacs</b> command without any further specifications, the program gives you an empty workspace.</p>
<p>If you want to specify a file when you start Emacs, you can tell Emacs to automatically load one or more files by including the filenames in the command call:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>athena% <b>emacs <em>filename</em> [<em>filename</em> ...]</b></tt></p>
<p>For example, you might enter:</p>
<p><tt>athena% <b>emacs paper.tex &</b></tt></p>
<p>If the file you want to edit exists, but is not in your current directory, change directories or specify the pathname. If there is no file with that name, Emacs creates a new, empty file for you. (See <a href="#EmacsonAthena-findfile">Creating or Finding a File</a> for more information.) If you specify two filenames, both buffers appear on the screen. Specifying more than two causes the last buffer named and a bufferlist to appear on the screen. (See <a href="#EmacsonAthena-multibuffers">Working with Multiple Buffers</a> for more information on editing multiple files; see <a href="#EmacsonAthena-buffers">About Buffers</a> for a definition of buffers.)</p>
<p>You do not have to start up a new Emacs session to edit another file. Many users find it convenient to keep Emacs running on their workstation (or terminal) and specify files as they need them. (See <a href="#EmacsonAthena-findfile">Creating or Finding a File</a> for more information.)</p>
<p>You can specify the window dimensions and location on the screen with the <b>-geometry</b> flag, as with all X programs. The form is:</p>
<p><tt>athena% <b>emacs -geometry <em>width_x height</em>+ <em>xoff</em>+ <em>yoff</em></b></tt></p>
<p>The <em>width</em> and <em>height</em> parts of the geometry specification specify the dimensions of the Emacs window and are measured in characters (width) and lines (height). The <em>xoff</em> and <em>yoff</em> parts are measured in pixels and are used to specify the position of the window relative to the edges of the screen. You can specify positive or negative coordinates for location. For example, (0,0) is in the upper left corner of the screen, (-0,-0) is the lower right corner. Positive coordinates are measured from the left or top of the screen; negative coordinates are measured from the right or bottom. The example in the table below creates a small window in the upper right corner of the screen. Note that this only applies if you are using Emacs on a workstation.</p>
<p>To change the size later, use window manager commands. This is often more convenient than initially specifying the size.<br/>
Here are some examples of a few options to the <b>emacs</b> command:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Option</th>
<th class='confluenceTh'>Example</th>
</tr>
<tr>
<td class='confluenceTd'>multiple filenames</td>
<td class='confluenceTd'>emacs Makefile main.c &</td>
</tr>
<tr>
<td class='confluenceTd'>filenames with wildcards</td>
<td class='confluenceTd'>emacs *.c &</td>
</tr>
<tr>
<td class='confluenceTd'>size and position</td>
<td class='confluenceTd'>emacs -geometry 80x24-0+0 &</td>
</tr>
<tr>
<td class='confluenceTd'>reverse video</td>
<td class='confluenceTd'>emacs -r &</td>
</tr>
<tr>
<td class='confluenceTd'>start Emacs in the current xterm (not its own window)</td>
<td class='confluenceTd'>emacs -nw</td>
</tr>
<tr>
<td class='confluenceTd'>ignore ~/.emacs file</td>
<td class='confluenceTd'>emacs -q &</td>
</tr>
<tr>
<td class='confluenceTd'>set foreground color</td>
<td class='confluenceTd'>emacs -fg green &</td>
</tr>
<tr>
<td class='confluenceTd'>set background color</td>
<td class='confluenceTd'>emacs -bg black &</td>
</tr>
<tr>
<td class='confluenceTd'>combination of options</td>
<td class='confluenceTd'>emacs -r paper.tex &</td>
</tr>
</tbody></table>
</div>
<p>Remember not to use & if you are accessing Athena remotely (e.g., via dialup). The order and combination of multiple options does not usually matter. For information about other command line options, type <b>man emacs</b> at the Athena prompt.</p>
<h3><a name="EmacsonAthena-PartsoftheEmacsWindow"></a>Parts of the Emacs Window</h3>
<p>The Emacs window is divided into four zones:</p>
<ol>
<li>The <b>menu bar</b> is at the very top of the window. Here, Emacs lists possible options in a drag-down menu format. (Click on the menu which you wish to access; drag your mouse down to the option you want to choose, and let go. Many options (such as Buffers, Files, Tools, Edit, Search, and Help) are common to all modes. However, some modes have specialized options. (For instance, the HTML mode has an HTML option.) In this picture, the menu bar, shown at the top of the screen shot, has commands from Buffers to Help.</li>
<li>The <b>buffer editing area</b> starts below the menu bar and takes up most of the window's area. When you edit an existing file, its text appears here; if the file doesn't exist yet, and you haven't entered any text, this area shows the GNU Emacs "copyleft" notice, which disappears as soon as you do anything in the Emacs window. (See <a href="#EmacsonAthena-customizing">Customizing Your Emacs Environment</a> for information on how to suppress this notice.) Here, this contains the beginning of the document that you are reading right now.</li>
<li>The <b>bar</b> – usually displayed in reverse video – is the Emacs <b>mode line</b>. Here Emacs displays status information about the editing session, such as the name of the file being edited and the modes that are in effect. (Modes are covered under <a href="#EmacsonAthena-modes">Buffer Modes</a>.) You can customize Emacs to display additional information in the bar, such as the current date and time. (See the sample ~/.emacs file in <a href="#EmacsonAthena-customizing">Customizing Your Emacs Environment</a>.) The name of the file shown here is Emacs.html; you can also see that Emacs is in HTML mode (from the HTML in parentheses) and that the cursor is on line 1, the top of the document. If the cursor were further, this area would display a percentage.</li>
<li>The <b>minibuffer</b> is the area at the bottom of the screen beneath the mode line; the minibuffer is also known as the Emacs <em>echo area</em>. Whenever Emacs wants to tell you something, or ask you a question ("prompt you" for information), it does so here.</li>
</ol>
<h3><a name="EmacsonAthena-UsingEmacsCommands"></a>Using Emacs Commands</h3>
<h4><a name="EmacsonAthena-CommandSyntax"></a>Command Syntax</h4>
<p>Besides typing text into Emacs, you often perform tasks such as deleting text, moving the cursor to another position, or saving text to a file on disk. You accomplish these operations and many others by using Emacs <em>commands</em>.</p>
<p>These commands can be accessed by the menu bar, as mentioned above; they can also be typed in. Emacs must have a way of distinguishing what you type as text from what you enter as an Emacs command. This is accomplished through the use of <em>prefix characters.</em> A prefix character is a special keystroke or a sequence of keystrokes that changes the effect of a regular keyboard character.</p>
<p>Emacs uses several prefix characters:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Name</th>
<th class='confluenceTh'>Abbreviation in Documents</th>
<th class='confluenceTh'>Key(s)</th>
</tr>
<tr>
<td class='confluenceTd'>Control</td>
<td class='confluenceTd'>Ctrl-, C-, ^</td>
<td class='confluenceTd'>Press and hold Ctrl</td>
</tr>
<tr>
<td class='confluenceTd'>Meta</td>
<td class='confluenceTd'>M-, Esc</td>
<td class='confluenceTd'>Press/hold Alt, Compose, or black diamond; <br/>
Press/release Esc; Ctrl-[</td>
</tr>
<tr>
<td class='confluenceTd'>Esc</td>
<td class='confluenceTd'>Esc</td>
<td class='confluenceTd'>Esc</td>
</tr>
<tr>
<td class='confluenceTd'>execute extended command</td>
<td class='confluenceTd'>M-x</td>
<td class='confluenceTd'>M-x</td>
</tr>
</tbody></table>
</div>
<p>For the Meta command, sometimes the other keys don't work over dialup; <b>C-[</b> always works.</p>
<p>For example, to move the cursor to the beginning of the buffer you use the command <b>Meta-<</b> (press and hold the <b>Meta</b> key, then press the <b><</b> key). Similarly, to move the cursor to the beginning of the line, you enter the Emacs command <b>Control-a</b>, (press and hold the <b>Ctrl</b> key, then press the <b>a</b> key).</p>
<p>All Emacs commands have names, such as <b>find-file</b> or <b>beginning-of-buffer</b>. Most of the common commands have shorter key bindings (e.g., <b>beginning-of-buffer</b> is invoked by <b>M-<</b>, mentioned above). Key bindings can be changed, but the command name always works when used with <b>M-x</b>; thus typing <b>M-x find-file</b> is the same as typing <b>C-x C-f</b>.</p>
<p>Notice that <b>C-x C-f</b> is different than <b>C-x f</b>. <b>C-x C-f</b> means to hold down the <b>Ctrl</b> key for both <b>x</b> and <b>f</b>. <b>C-x f</b> ( <b>set-fill-column</b>) means to hold down the <b>Ctrl</b> key while pressing <b>x</b>, but release it before pressing <b>f</b>.</p>
<p>Use <b>C-u <em>#</em></b> to tell Emacs to execute the following command <em>#</em> times. For example, <b>M-d</b> kills the word after the cursor, and <b>C-u 3 M-d</b> kills the next three words. The default numeric argument is 4, so <b>C-u M-d</b> kills the next four words. Similarly, <b>C-u C-u C-u M-d</b> kills the next sixty-four (4^3) words.</p>
<h4><a name="EmacsonAthena-TheMenuBar"></a>The Menu Bar </h4>
<p>Many Emacs commands can be accessed through the Menu Bar in addition through command key sequences. When you are using a window system (ie. not over dialup and other text-only terminals), you can use the mouse to choose a commands from the Menu Bar as is standard in many windows-based programs. When you're first learning Emacs, the Menu Bar is a good way to get acquainted with some of the features without having to learn what keys are bound to what functions.</p>
<p>The menu bar can be toggled on and off with <b>M-x menu-bar-mode</b></p>
<p>These are the menus available in the default (Fundamental) mode and brief descriptions of some of the commands available through them. Other menus are specific to certain modes and become available in those modes only.</p>
<ul>
<li><b>Buffers:</b> The Buffers menu lists all the buffers currently in Emacs. Selecting one of those buffers switches you to that buffer. This menu also has a List All Buffers option which performs the same function as <em>M-x list-buffers</em>. (See the section on <a href="#EmacsonAthena-multibuffers">Working With Multiple Buffers</a> for more information).</li>
<li><b>Files:</b> The Files menu has standard commands for opening, saving, closing, and inserting files, as well as Emacs frame and window management. The Exit Emacs command is also found in this menu.</li>
<li><b>Tools:</b> The Tools menu has commands for printing, version control systems, mail, news, compilation and debugging.</li>
<li><b>Edit:</b> The Edit menu commands include Undo, Cut, Copy, and Paste and allows the user to specify text properties such as faces, colors, justification, and indentation. The Spell sub-menu found here has options to select the language and check any or all of a given buffer.</li>
<li><b>Search:</b> The Search menu has commands for standard and regexp searching and query-replace searching within the buffer. The first set of commands let you specify the type of search and direction while the second set (Repeat Search) lets you find the next forward or backwards match in the buffer. (See the section on <a href="#EmacsonAthena-searching">Searching For and Replacing Text</a> for more information).</li>
<li><b>Mule:</b> MULE refers to "MULti-lingual Enhancement to GNU Emacs." Emacs supports a variety international character sets, and this menu allows you to set the language environment and input method. The details of how to use these character sets is beyond the scope of this document, but more information can be found in Chapter 22 of the <b>GNU Emacs Manual</b>.</li>
<li><b>Help:</b> Besides having an Emacs tutorial, the Help menu has a few useful sub-menus. The Options sub-menu allows the user to set things such as Global Font Lock mode (highlights syntax) and Auto Fill mode (wordwrap). The Manuals sub-menu has access to documentation including a FAQ, the Emacs manual (with ways to search it), and the Emacs man page. Finally, you can use the Describe sub-menu to access detailed information about all emacs commands. Describe Buffer Modes tells you all of the mode-specific commands available. Apropos lets you search for a specific pattern among all commands or variables. List Key Bindings gives a list of what key sequences are bound to what commands. Describe Key/Function/Variable gives a description of what the input key sequence, function, or variable does.</li>
</ul>
<h4><a name="EmacsonAthena-WorkingintheMinibuffer"></a>Working in the Minibuffer </h4>
<p>The <em>minibuffer</em> is the line at the bottom of your screen where Emacs commands appear as they are typed in. When you enter the character sequence <b>C-x C-f</b>, for example, Emacs uses the minibuffer to prompt you for a filename to load into Emacs. Within the minibuffer, you can move around and edit. (In fact, you can use any Emacs command in the minibuffer that doesn't itself need the minibuffer.)</p>
<p>One of the features of the minibuffer is command and filename completion. This means you don't need to know or type the entire name of a command. You only need to type enough for the command to be unique. There are three commands for completion in the minibuffer:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>Space</td>
<td class='confluenceTd'>minibuffer-complete-word – completes as far as the end of the word (in a hyphenated command, a dash marks the end of a word) and shows possible completions.</td>
</tr>
<tr>
<td class='confluenceTd'>Tab</td>
<td class='confluenceTd'>minibuffer-complete – completes as far as possible and shows possible completions.</td>
</tr>
<tr>
<td class='confluenceTd'>Return</td>
<td class='confluenceTd'>minibuffer-complete-and-exit – completes the command as far as possible and then executes the command.</td>
</tr>
<tr>
<td class='confluenceTd'>?</td>
<td class='confluenceTd'>Show possible completions.</td>
</tr>
</tbody></table>
</div>
<p>Sometimes, if you exit the minibuffer without completing the command you started running in it, Emacs starts keeping track of the commands you've left in the minibuffer. This isn't usually a problem, but can cause things to look cluttered and a lot more complicated than they really are. You can use <b>C-x o</b> to get to the minibuffer (see the section on <a href="#EmacsonAthena-multiwindows">Working with Multiple Windows</a>), then get rid of the uncompleted command (by completing it, or by canceling with <b>C-g</b>. Try hitting <b>C-g</b> multiple times, if the first one does not work.)</p>
<p>You can find more information about the minibuffer and completion in Chapter 9 of the <b>GNU Emacs Manual</b>.</p>
<h4><a name="EmacsonAthena-InterruptingandUndoingCommands"></a>Interrupting and Undoing Commands </h4>
<p>Emacs provides ways of undoing or cancelling commands:</p>
<ul>
<li>To <em>undo</em> the command you last entered, use <b>C-x u</b> (the <b>undo</b> command). As you keep typing <b>C-x u</b>, it undoes earlier and earlier operations. (The commands <b>C-?</b>, <b>C-Delete</b>, and <b>C-_</b> do the same thing on most workstations.)</li>
<li>To <em>cancel</em> a command (not yet completed or currently underway), use <b>C-g</b> (the <b>keyboard-quit</b> command). This is the "get me out of here" command. Try this when Emacs doesn't seem to be responding. Look in the minibuffer. If Emacs is prompting you for information, <b>C-g</b> cancels the prompt. It may be necessary to press <b>C-g</b> several times; if you are totally lost, this is often an excellent action of last resort.</li>
<li>To <em>refresh</em> the screen because it's become garbled, use <b>C-l</b> (the <b>recenter</b> command; note that it is the lowercase letter l). This doesn't affect any commands or your text; it cleans the screen from line noise so you can see what's really there. This is especially useful over dialup. Additionally, this works in X windows; its use is not limited to Emacs.</li>
</ul>
<h3><a name="EmacsonAthena-MovingtheCursor"></a>Moving the Cursor </h3>
<p>You can move the input cursor around a buffer by using the mouse, the arrow keys, or keyboard commands:</p>
<ul>
<li><b>Mouse:</b> The easiest way to move the cursor is with the mouse: move the mouse cursor to the desired location, then press the left mouse button to move the input cursor to that spot. This does not work over dialup.</li>
<li><b>Arrow keys:</b> Arrow keys move the cursor up, down, left, and right. On most keyboards, the arrow keys are to the right of the main keyboard. Play with the arrow keys to get used to their behavior, especially in moving from line to line. (Arrow keys may not work in a dialup session, but often this can be fixed. Use <b>olc</b> to ask the Consultants for help, or see the following table for alternatives.)</li>
<li><b>Keyboard commands:</b> Commands that move the cursor include the following:
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Cursor Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-f, right arrow, M-x forward-char</td>
<td class='confluenceTd'>move right one character</td>
</tr>
<tr>
<td class='confluenceTd'>C-b, left arrow, M-x backward-char</td>
<td class='confluenceTd'>move left one character</td>
</tr>
<tr>
<td class='confluenceTd'>M-f, M-x forward-word</td>
<td class='confluenceTd'>move forward one word</td>
</tr>
<tr>
<td class='confluenceTd'>M-b, M-x backward-word</td>
<td class='confluenceTd'>move backward one word</td>
</tr>
<tr>
<td class='confluenceTd'>C-p, up arrow, M-x previous-line</td>
<td class='confluenceTd'>move up one line</td>
</tr>
<tr>
<td class='confluenceTd'>C-n, down arrow, M-x next-line</td>
<td class='confluenceTd'>move down one line</td>
</tr>
<tr>
<td class='confluenceTd'>C-e, M-x end-of-line</td>
<td class='confluenceTd'>jump to end of line</td>
</tr>
<tr>
<td class='confluenceTd'>C-a, M-x beginning-of-line</td>
<td class='confluenceTd'>jump to start of line</td>
</tr>
<tr>
<td class='confluenceTd'>M-e, M-x forward-sentence</td>
<td class='confluenceTd'>move to end of sentence</td>
</tr>
<tr>
<td class='confluenceTd'>M-a, M-x backward-sentence</td>
<td class='confluenceTd'>move to beginning of sentence</td>
</tr>
<tr>
<td class='confluenceTd'>M-}, M-x forward-paragraph</td>
<td class='confluenceTd'>move to end of paragraph</td>
</tr>
<tr>
<td class='confluenceTd'>M-{, M-x backward-paragraph</td>
<td class='confluenceTd'>move to beginning of paragraph</td>
</tr>
<tr>
<td class='confluenceTd'>C-v, M-x scroll-up, PageDown (not over dialup)</td>
<td class='confluenceTd'>move to next screenful of text</td>
</tr>
<tr>
<td class='confluenceTd'>M-v, M-x scroll-down, PageUp (not over dialup)</td>
<td class='confluenceTd'>move to previous screenful of text</td>
</tr>
<tr>
<td class='confluenceTd'>M-<, M-x beginning-of-buffer</td>
<td class='confluenceTd'>jump to start of buffer</td>
</tr>
<tr>
<td class='confluenceTd'>M->, M-x end-of-buffer</td>
<td class='confluenceTd'>jump to end of buffer</td>
</tr>
<tr>
<td class='confluenceTd'>left mouse button</td>
<td class='confluenceTd'>place Emacs input cursor at current mouse cursor position</td>
</tr>
</tbody></table>
</div>
</li>
</ul>
<p>Emacs considers a sentence to end wherever there is a period, question mark, or exclamation point followed by the end of a line or two spaces. Paragraphs are separated by blank lines and text formatter command lines; these lines are not part of any paragraph. Also, an indented line starts a new paragraph.</p>
<h3><a name="EmacsonAthena-SavingYourWork"></a>Saving Your Work <a name="EmacsonAthena-savebuffer"></a></h3>
<p>The <b>save-buffer</b> command writes the contents of the buffer into the file on disk. To enter the command, type <b>C-x C-s</b>. When Emacs performs the write operation, the editor displays a message in the Emacs echo area confirming the operation's success. Similarly, <b>C-x C-w</b> (write-buffer) saves the work under a different name, functioning similarly to the Save As commands of many word processing packages.</p>
<p>If you try to save the buffer and you haven't changed anything in the buffer since the last write, the command does not write the buffer to disk. Instead, it displays the message "(No changes need to be saved)"</p>
<p><b>Warning:</b> <em>Save your work periodically!</em> In fact, you should use the command periodically, perhaps as often as every five or ten minutes. That way you are less likely to lose large amounts of work should the system happen to crash in the middle of your editing session. A good rule of thumb is this: if you've made a change to the file that you wouldn't want to have to make again, or you have written a sentence, paragraph, or series of paragraphs that you wouldn't want to reconstruct, save your work.</p>
<p>As you insert and edit text, Emacs keeps track of the changes and automatically saves them each time you enter or delete 300 characters (by default). It saves the changes in a separate <em>auto-save</em> file, not in the original file. The auto-save file provides another means of saving your work in the event of a system problem.</p>
<p>Each time Emacs saves changes, the editor displays the following message in the echo area:</p>
<p><tt>Auto saving ... done</tt></p>
<p>The auto-save file is stored on the particular workstation you are working on, in the directory /usr/tmp. The file remains on that workstation for three days or until you save the file, as long as some other user does not come along and remove it. (See <a href="#EmacsonAthena-restoringfiles">Recovering Lost Files</a> for information on how to restore your file from this auto-save file.)</p>
<p>To save changes you have made in multiple buffers in Emacs, use the <b>save-some-buffers</b> command. Type <b>C-x s</b>; you are asked to confirm saving each modified buffer.</p>
<p>Sometimes the <b>save-buffer</b> command cannot successfully write the buffer to disk. Usually when this happens, Emacs informs you with a beep and displays an error message in the minibuffer. The message says something like:</p>
<p><tt>I/O Error</tt><br/>
<tt>Opening output file: no space left on device</tt><br/>
<tt>Cannot write to</tt><br/>
<tt><em>filename</em>; saving in %backup%</tt></p>
<p>When this happens, try saving the buffer again. If you still have problems, you might be over quota. Also, confirm that Emacs is trying to save in a directory in which you have write access.</p>
<p>Note that whenever you edit a file, Emacs keeps a copy of the original version under the name <em>filename</em>~. You can use these backup files to undo all the changes you've made by renaming (moving) the backup file to the real filename:</p>
<p><tt>athena% <b>mv <em>filename</em>~ <em>filename</em></b></tt></p>
<p>This command overwrites whatever is in <em>filename</em>, so make sure this is what you want to do.</p>
<h3><a name="EmacsonAthena-ExitingEmacs"></a>Exiting Emacs <a name="EmacsonAthena-exitemacs"></a></h3>
<p>The command to quit Emacs is <b>C-x C-c</b>, or <b>M-x save-buffers-kill-emacs</b>. If you have just used the <b>save-buffer</b> command to write your file to disk, the Emacs window vanishes and the editing session is finished.</p>
<p>However, if you have unsaved changes, Emacs displays a message in the minibuffer that asks if you want to save the changes you have made to that buffer. If you type <b>y</b>, Emacs writes the contents of the buffer to disk, then the Emacs window vanishes. If you type <b>n</b>, Emacs will say: "Modified buffers exist: exit? (yes or no)". You have to type in the entire word to get Emacs to do what you want here.</p>
<p>Emacs never finishes an editing session without first making sure that all the files you have worked on are saved, and it never kills a buffer without asking if you want to save it. You should always use the <b>safe-buffers-kill-emacs</b> command, or make sure all your buffers are saved, before logging out of your workstation; otherwise, you may lose your files.</p>
<h2><a name="EmacsonAthena-BasicEditingCommands"></a>Basic Editing Commands <a name="EmacsonAthena-editing"></a></h2>
<h3><a name="EmacsonAthena-CreatingorFindingaFile"></a>Creating or Finding a File <a name="EmacsonAthena-findfile"></a></h3>
<p>You can edit an existing file or a new file by typing the <b>find-file</b> command, <b>C-x C-f</b>.</p>
<p>When you enter this command, Emacs displays a message in the minibuffer asking you to specify the name of the file you want to find. Enter a filename (including the full path to the file) and press <b>Return</b>. Be sure to check the pathname shown in the minibuffer; Emacs usually assumes the desired file is in the directory from which you started Emacs. Emacs creates a new buffer:</p>
<ul>
<li>If the file you specified does not exist, you are creating a new file. Emacs creates an empty buffer and places the input cursor at the top of the window.</li>
<li>If the file you specified exists, Emacs copies the contents of the existing file into a new buffer and places the input cursor at the beginning of the buffer.</li>
</ul>
<h3><a name="EmacsonAthena-InsertingText"></a>Inserting Text <a name="EmacsonAthena-insertingtext"></a></h3>
<p><b>You insert text into the buffer by typing. When you finish a line, press *Return</b> or <b>Enter</b> to get to the beginning of the next line. Use the <b>arrow</b> keys or the cursor movement commands to position the cursor where you want it.<br/>
When you get to the bottom of the screen, Emacs adjusts the text within the window ("scrolls the screen up"), so that you have more visible space in which to type.</p>
<p>To change what you've typed, use the <b>Delete</b> key, just as you would when typing commands at the Athena prompt. A backslash appearing at the end of a line in Emacs indicates that the text is actually one line even though it looks like more than one. (When you "fill" the paragraph, as explained in the section <a href="#EmacsonAthena-fillwrap">Filling and Line Wrapping Within Margins</a>, the backslashes disappear.)</p>
<p>If you type a closing parenthesis, Emacs hops the cursor over to the corresponding opening parenthesis briefly, then hops it back. This is because Emacs was written by programmers for whom matching them up is a major worry. Emacs also does this with paired brackets and braces.</p>
<h3><a name="EmacsonAthena-InsertingFiles"></a>Inserting Files <a name="EmacsonAthena-insertingfiles"></a></h3>
<p>You can insert the text from another file into your current editing buffer. Do this by moving the input cursor to the location in the buffer where you want the inserted text to begin, then enter the <b>insert-file</b> command <b>C-x i</b>.</p>
<p>When you enter this command, Emacs prompts you in the minibuffer for the name of the file you want inserted. The message looks something like this:</p>
<p><tt>Insert file: ~/</tt></p>
<p>(The ~ character is shorthand for your home directory, /mit/ <em>username</em>.) Type the full pathname of the file (erasing what Emacs has provided if necessary) and press Return. The text of this file is entered into your current editing buffer.</p>
<h3><a name="EmacsonAthena-AboutBuffers"></a>About Buffers <a name="EmacsonAthena-buffers"></a></h3>
<p>When you use Emacs to create a new file, the editor sets up a <em>buffer</em> for you to work in. The buffer is a temporary workspace. The text you type into a buffer isn't permanent until you tell Emacs to write the buffer's contents to permanent storage ("write the buffer to disk"). In fact, the file isn't created until you write the buffer to disk for the first time.</p>
<p>When you use Emacs to edit an existing file, the editor also sets up a buffer for you to work in, and <em>copies</em> the file's contents into the buffer. Any editing you do changes the buffer's contents, not the file's contents. You change the file's contents only when you instruct Emacs to write the buffer to disk.</p>
<p>If you find a second file (with <b>C-x C-f</b>, among other ways), the first file remains inside Emacs. This way you can get quite a number of files inside Emacs, each in its own buffer. You can switch between them with the commands listed in <a href="#EmacsonAthena-bufferswindows">Multiple Buffers and Windows</a>.</p>
<p>Each buffer has a name; if the buffer corresponds to a file, it has a related name. Some buffers do not correspond to files. For example, the buffer named <b>Help</b> does not have any file. It is the buffer which contains help information when you request it with the <b>C-h</b> commands. Any text you see in an Emacs window has to be in some buffer.</p>
<h3><a name="EmacsonAthena-TheScratchBuffer"></a>The Scratch Buffer <a name="EmacsonAthena-scratchbuffer"></a></h3>
<p>Among any other buffers you may be using, Emacs always creates an empty <em>scratch buffer</em> for you. Emacs displays this scratch buffer if you start Emacs without specifying a file. In the Emacs mode line at the bottom of the Emacs window, the buffer is designated like this:</p>
<p><tt>-----Emacs: <b>scratch</b></tt></p>
<p>The scratch buffer is a regular editing buffer: you can type text into it, move the cursor around, and so on. As its name implies, the scratch buffer is the Emacs version of a scratch pad. You can use it to practice things or type temporary notes, but in general you won't do real work in it. Instead, you should create a new file or edit an existing one by creating another editing buffer (see <a href="#EmacsonAthena-findfile">Creating or Finding a File</a>).</p>
<p>If you have modified the scratch buffer and want to exit Emacs, the following message appears in the minibuffer:</p>
<p><tt>Save buffer <b>scratch</b>? (y or n)</tt></p>
<p>If you don't want to save the scratch buffer, enter <b>n</b>, then the window vanishes (all the text in the scratch buffer is lost) and your session is over.</p>
<p>If you do want to save the scratch buffer, type <b>y</b>. Emacs asks you to give a filename in which to write the contents of the buffer. Enter a filename, and press <b>Return</b>. The window vanishes, your session is over, and you have a new file containing the text from that scratch buffer.</p>
<h3><a name="EmacsonAthena-CheckingSpelling"></a>Checking Spelling <a name="EmacsonAthena-spell"></a></h3>
<p>The ispell command checks your spelling within Emacs. There are three ways to invoke ispell; <b>M-x ispell-word</b>, which runs ispell on the selected word; <b>M-x ispell-region</b>, which runs ispell on all words in the marked region in the current buffer; and <b>M-x ispell-buffer</b>, which runs ispell on the entire buffer. When an ispell command finds a word that is not in its dictionary, it responds with a message that says, Enter letter to replace word; space to flush. If there are "near misses" in the dictionary, ispell also displays the incorrect word and a list of alternatives (if any exist). At this point, you can type one of the following characters:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>r</td>
<td class='confluenceTd'>Replace Word; Ispell asks you for a replacement word.</td>
</tr>
<tr>
<td class='confluenceTd'>#</td>
<td class='confluenceTd'>Replace Word with Suggested Word; The suggested alternative spellings are numbered. If you use a word's number, ispell uses this word as a replacement.</td>
</tr>
<tr>
<td class='confluenceTd'>Space</td>
<td class='confluenceTd'>Skip Word; Leave this word as is, but stop at the next occurrence and prompt for an action.</td>
</tr>
<tr>
<td class='confluenceTd'>a</td>
<td class='confluenceTd'>Accept Word; Accept this word as correct for remainder of Emacs session.</td>
</tr>
<tr>
<td class='confluenceTd'>i</td>
<td class='confluenceTd'>Add Word; Add this word to your personal dictionary. This is a file in your home directory called .ispell.words which tells ispell to accept the word as correct every time you use ispell (useful for things like your name).</td>
</tr>
<tr>
<td class='confluenceTd'>C-g</td>
<td class='confluenceTd'>Quit Ispell.</td>
</tr>
</tbody></table>
</div>
<p>You can also use ispell outside of Emacs; type <b>man ispell</b> at the Athena prompt for instructions on how to use it.</p>
<h3><a name="EmacsonAthena-FillingandLineWrappingwithinMargins"></a>Filling and Line Wrapping within Margins <a name="EmacsonAthena-fillwrap"></a></h3>
<p>If you are inserting text into a paragraph, you can use the <b>M-q</b> <b>fill-paragraph</b> command to fix the margins on the paragraph, or you can wrap your lines automatically with auto-fill by typing <b>M-x auto-fill-mode</b>. The <b>auto-fill-mode</b> command is a toggle, so typing it again turns auto-fill back off. You can tell whether it's on or off by looking in the bar; the word Fill appears next to the major mode (e.g., Text or TeX) when auto-fill is on. Here are some commands related to filling text:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>M-x auto-fill</td>
<td class='confluenceTd'>Automatically break lines (at spaces) when they are longer than the desired width.</td>
</tr>
<tr>
<td class='confluenceTd'>M-q, M-x fill-paragraph</td>
<td class='confluenceTd'>Fill paragraph at or after point. With a numeric argument (i.e., preceded by C-u #), justify as well.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x fill-region</td>
<td class='confluenceTd'>Fill each of the paragraphs in the region.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x f, M-x set-fill-column</td>
<td class='confluenceTd'>With C-u as an argument, sets fill-column to the current horizontal position of point. With a numeric argument, uses that as the new fill column.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x ., M-x set-fill-prefix</td>
<td class='confluenceTd'>Make the text to the left of the cursor a prefix for each line in a filled paragraph.</td>
</tr>
</tbody></table>
</div>
<p>For example, if fill-column is 40 and you set the fill prefix to `;; ', then M-q in the following text</p>
<p><tt>;; This is an</tt><br/>
<tt>;; example of a paragraph</tt><br/>
<tt>;; inside a Lisp-style comment.</tt></p>
<p>produces this:</p>
<p><tt>;; This is an example of a paragraph</tt><br/>
<tt>;; inside a Lisp-style comment.</tt></p>
<h3><a name="EmacsonAthena-OtherTextManipulationCommands"></a>Other Text Manipulation Commands <a name="EmacsonAthena-textother"></a></h3>
<p>In addition to the commands for checking the spelling of a file and for basic formatting of the text, Emacs offers other useful text-manipulation commands. The following table summarizes some of these:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>M-u, M-x upcase-word</td>
<td class='confluenceTd'>Make word uppercase</td>
</tr>
<tr>
<td class='confluenceTd'>M-l, M-x downcase-word</td>
<td class='confluenceTd'>Make word lowercase</td>
</tr>
<tr>
<td class='confluenceTd'>M-c, M-x capitalize word</td>
<td class='confluenceTd'>Capitalize word</td>
</tr>
<tr>
<td class='confluenceTd'>M-x capitalize-region</td>
<td class='confluenceTd'>Capitalize every word in region</td>
</tr>
<tr>
<td class='confluenceTd'>C-t, M-x transpose-chars</td>
<td class='confluenceTd'>Transpose preceding two characters</td>
</tr>
<tr>
<td class='confluenceTd'>M-t, M-x transpose-words</td>
<td class='confluenceTd'>Transpose words around cursor</td>
</tr>
<tr>
<td class='confluenceTd'>C-x C-t, M-x transpose-lines</td>
<td class='confluenceTd'>Transpose current line and previous line</td>
</tr>
<tr>
<td class='confluenceTd'>M-x sort-lines</td>
<td class='confluenceTd'>Alphabetize the region by line</td>
</tr>
</tbody></table>
</div>
<p>When changing the case of words, Emacs finds the first word on or after the cursor, ignoring any characters before the cursor and ignoring white space when necessary, operates on that word, and moves the cursor to the end of the word.</p>
<p>When transposing characters, Emacs exchanges the character before the cursor with the character on the cursor, moving the cursor to the position after both the characters. For example, to fix the typo teh, position the cursor on the h, then press <b>C-t</b>. The h and e switch places, and the cursor moves to the position after the.</p>
<h2><a name="EmacsonAthena-Deleting%2CMoving%2CandCopyingText"></a>Deleting, Moving, and Copying Text <a name="EmacsonAthena-deleting"></a></h2>
<h3><a name="EmacsonAthena-DeletingandKillingText"></a>Deleting and Killing Text <a name="EmacsonAthena-killingtext"></a></h3>
<p>In Emacs parlance, <em>deleting</em> something <b>removes it forever</b>. <em>Killing</em> something means that it disappears from the buffer, but it <b>can be yanked back</b>. Here are some common commands related to eliminating text:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>Delete, M-x delete-backward-char, BACKSPACE</td>
<td class='confluenceTd'>Delete the character before the cursor.</td>
</tr>
<tr>
<td class='confluenceTd'>C-d, M-x delete-char</td>
<td class='confluenceTd'>Delete the character the cursor is on.</td>
</tr>
<tr>
<td class='confluenceTd'>M-Delete, M-x backward-kill-word</td>
<td class='confluenceTd'>Kill back to the beginning of a word.</td>
</tr>
<tr>
<td class='confluenceTd'>M-d, M-x kill-word</td>
<td class='confluenceTd'>Kill up to the end of a word.</td>
</tr>
<tr>
<td class='confluenceTd'>C-k, M-x kill-line</td>
<td class='confluenceTd'>Kill the line forward from the cursor.</td>
</tr>
<tr>
<td class='confluenceTd'>C-k C-k</td>
<td class='confluenceTd'>Kill the line and its trailing Return.</td>
</tr>
<tr>
<td class='confluenceTd'>C-u 0 C-k</td>
<td class='confluenceTd'>Kill the line to the left of the cursor.</td>
</tr>
<tr>
<td class='confluenceTd'>M-k, M-x kill-sentence</td>
<td class='confluenceTd'>Kill forward to the end of the sentence.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x Delete, M-x backward-kill-sentence</td>
<td class='confluenceTd'>Kill back to the beginning of the sentence.</td>
</tr>
</tbody></table>
</div>
<p>To yank back the last thing that you killed, enter <b>C-y</b>. (See <a href="#EmacsonAthena-yankcommands">Restoring Text: The Yank Commands</a> for more information.) One way to make a copy of something is to kill it, then yank it back where it originally was, then yank again where you want the copy. (There are other ways to copy a piece of text.)</p>
<p>You can kill, delete, and restore large blocks of text by using the region-marking capabilities of Emacs. For more information, refer to the section on <a href="#EmacsonAthena-killingregions">Killing Regions</a>.</p>
<h3><a name="EmacsonAthena-MarkingRegions"></a>Marking Regions <a name="EmacsonAthena-regions"></a></h3>
<p>A <em>region</em> is the area between <em>point</em> and <em>mark</em>. Point is the current cursor location. Mark is an invisible place marker that Emacs sets up. When you jump large distances in Emacs, such as with <b>M-></b> or <b>C-s</b>, you usually leave a mark behind. You can manually set a mark with the <b>set-mark</b> command, <b>C-@</b> ( <b>Ctrl-Shift-2</b>). On many workstations, <b>C-Space</b> works as well. Alternatively, you can specify the region by simply dragging the mouse and highlighting the appropriate text.</p>
<p>Unfortunately, Emacs does not highlight the mark. The easiest way to find out its location is to use <b>C-x C-x</b>, which is <b>exchange-point-and-mark</b>. Typing the same thing again returns you to your initial position. (A few other commands for moving the mark are listed in Chapter 12 of the <b>GNU Emacs Manual</b>.)</p>
<h3><a name="EmacsonAthena-KillingRegions"></a>Killing Regions <a name="EmacsonAthena-killingregions"></a></h3>
<p>Emacs has a number of commands for deleting, killing, and yanking individual characters, words, and sentences. Often, however, you want to delete, kill, or yank arbitrary regions of text.</p>
<p>To kill text with the mouse, press Mouse-1 at one end and Mouse-3 twice at the other end.</p>
<p>Here are a few of the more murderous commands, for killing almost anything you want:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-w, M-x kill-region</td>
<td class='confluenceTd'>Remove region and place in kill ring.</td>
</tr>
<tr>
<td class='confluenceTd'>M-w, M-x copy-region-as-kill, Drag-Mouse-1</td>
<td class='confluenceTd'>Copy region to kill ring without removing from this buffer.</td>
</tr>
<tr>
<td class='confluenceTd'>C-M-w, M-x append-next-kill</td>
<td class='confluenceTd'>Append the next region killed to the contents of the current kill ring element.</td>
</tr>
</tbody></table>
</div>
<p>Whenever you kill text, it goes into the <em>kill ring.</em> This is a special type of buffer that can hold up to 30 (by default) different killed pieces. (There is only one kill ring within each Emacs process, so you can use it to carry text between buffers, where it can be yanked again.)</p>
<p>Normally, consecutive kills are part of the same segment of the kill ring. For example, if you use <b>C-k</b> several times in sequence, all those lines are kept together as one element in the kill ring, and <b>C-y</b> yanks them together. If you move the cursor or do something else between kills, but you want the pieces linked, use <b>C-M-w.</b> Editing commands from the X Windows system also work when on a workstation running X Windows.</p>
<h3><a name="EmacsonAthena-RestoringText%3ATheYankCommands"></a>Restoring Text: The Yank Commands <a name="EmacsonAthena-yankcommands"></a></h3>
<p>Once you have killed text into the kill ring, you can restore it any number of times. There are three commands for manipulating the kill ring in this manner: <b>C-y</b>, <b>M-x yank</b>, or <b>Mouse-2</b> inserts the text from current segment of kill ring into the buffer at the cursor; and <b>M-y</b>, or <b>M-x yank-pop</b>, replaces the last yanked text with text from the previous segment of kill ring.</p>
<p>When you type <b>C-y</b>, the last block of text that you killed is copied to the cursor location. It remains in the kill ring as well, so you can copy it back out as many times as you like.</p>
<p><b>M-y</b> can only be used immediately after <b>C-y</b> or another <b>M-y</b>. It replaces the yanked text on the screen with text from the previous positions on the kill ring (i.e., earlier and earlier deletions).</p>
<p>For example, suppose you want to copy the following paragraph from the beginning of your file to replace another paragraph at the end:</p>
<p><tt>The quick brown fox jumped over the lazy dog. However, it neglected to take into account the amount of rain that falls on plains in Spain.</tt></p>
<p>To do this, you could:</p>
<ol>
<li>Set the mark at the beginning with <b>C-@</b>, use <b>M-}</b> (or other cursor commands) to move the cursor to the end of the paragraph, then <em>copy</em> the whole region into the kill-ring using <b>M-w</b>. (See the sections on <a href="#EmacsonAthena-movingcursor">Moving the Cursor</a> and <a href="#EmacsonAthena-killingregions">Killing Regions</a> for more on this.)</li>
<li>Move to the end of the file (with <b>M-></b>), to find the paragraph to delete:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>Foxes rarely venture into the drier, mountainous regions.</tt><br/>
<tt>This is despite the fact that hurricanes hardly ever happen in Hartford, Hereford, and Hampshire.</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
<li>Delete this paragraph using the same method as before, except use <b>M-x delete-region</b> to <em>delete</em> this region.</li>
<li>Now type <b>C-y</b> to get back the paragraph about hurricanes (the most recent kill). Since we don't want that, we type <b>M-y</b>. This replaces that paragraph with the original paragraph about lazy dogs, as desired.</li>
</ol>
<h3><a name="EmacsonAthena-WorkingwithRectangles"></a>Working with Rectangles <a name="EmacsonAthena-rectangles"></a></h3>
<p>You can kill and yank rectangular regions as well as arbitrary regions between the mark and cursor. To mark a rectangle, indicate its opposite corners with the mark and cursor. Rectangular cut text is stored in a different buffer from the normally cut text, so you use a special command to retrieve cut text. Here are some of the commands for working with rectangular text regions (there are others):</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-x r k, M-x kill-rectangle</td>
<td class='confluenceTd'>Deletes a rectangular block of text between mark and point and places it in a special kill ring (you can remove columns from a larger block of text easily).</td>
</tr>
<tr>
<td class='confluenceTd'>C-x r d, M-x delete-rectangle</td>
<td class='confluenceTd'>Deletes a rectangular block of text between mark and point without storing it in a kill ring (this text is not recoverable).</td>
</tr>
<tr>
<td class='confluenceTd'>C-x r y, M-x yank-rectangle</td>
<td class='confluenceTd'>Restores last killed rectangle at cursor location, pushing text to the right of that column further over to the right.</td>
</tr>
</tbody></table>
</div>
<p>Note that "killing" a rectangle is not killing in the usual sense; the rectangle is not stored in the kill ring, but in a special place that can only record the most recent rectangle killed. This is because yanking a rectangle is so different from yanking linear text that different yank commands have to be used and yank-popping is hard to make sense of.</p>
<h2><a name="EmacsonAthena-SearchingForandReplacingText"></a>Searching For and Replacing Text <a name="EmacsonAthena-searching"></a></h2>
<h3><a name="EmacsonAthena-UsingSearchCommands"></a>Using Search Commands <a name="EmacsonAthena-searchintro"></a></h3>
<p>When editing large documents, it becomes difficult to keep track of where everything is or to go to a specific place in the document. Emacs provides a variety of options for locating specific strings and substrings without having to scroll through the entire file.</p>
<p>There are two ways to search for a string in a buffer:</p>
<ul>
<li><em>Incremental searches</em> prompt from the minibuffer asking what characters you wish to match. Each time you type in a character, Emacs moves forward or backward to the nearest match in the file.</li>
<li><em>Search-replace</em> asks you for two strings of characters (the <em>from-string</em> and the <em>to-string</em>), then interactively replaces selected occurrences of the <em>from-string</em> with the <em>to-string</em>.</li>
</ul>
<p>For searching more than one buffer, or for searching long buffers quickly, you can use the <b>grep</b> and <b>tags</b> commands. The Emacs <b>grep</b> command is like the operating system <b>grep</b> command: it searches through a number of buffers for a string. The <b>tags</b> command is useful for finding locations of variables, subroutines, subroutine calls, and similar structures in C or Lisp programs.</p>
<h3><a name="EmacsonAthena-SearchinginaBuffer"></a>Searching in a Buffer <a name="EmacsonAthena-searchcommand"></a></h3>
<p>Incremental searches prompt from the minibuffer asking what characters to match. Each time you type in a character, Emacs moves the point forward or backward to the nearest match in the file. The two search commands are C-s, or M-x isearch-forward, which starts or continues a forward search; and C-r, M-x isearch-backward, which starts or continues a backward search. When you use one of these commands, Emacs prompts you in the minibuffer for the characters you want. If you type <b>C-s</b> as the first character, Emacs searches for the string it searched for previously. ( <b>C-s</b> may not work over dialup.)</p>
<p>Commands available in search mode are:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>Delete</td>
<td class='confluenceTd'>Undo the last keystroke. (Usually go to previous match.)</td>
</tr>
<tr>
<td class='confluenceTd'>Return</td>
<td class='confluenceTd'>Terminate the search and leave the cursor at the current location. Most other simple commands ( <b>C-a</b>, <b>C-t</b>, etc.) exit the search and perform their function.</td>
</tr>
<tr>
<td class='confluenceTd'>C-s</td>
<td class='confluenceTd'>Go to next occurrence.</td>
</tr>
<tr>
<td class='confluenceTd'>C-r</td>
<td class='confluenceTd'>Go to previous occurrence.</td>
</tr>
<tr>
<td class='confluenceTd'>C-q</td>
<td class='confluenceTd'>Quote a character to be searched for.</td>
</tr>
<tr>
<td class='confluenceTd'>C-w</td>
<td class='confluenceTd'>Grab the word after the cursor as part of the search string.</td>
</tr>
<tr>
<td class='confluenceTd'>C-y</td>
<td class='confluenceTd'>Grab the line after the cursor as part of the search string.</td>
</tr>
<tr>
<td class='confluenceTd'>C-g</td>
<td class='confluenceTd'>Quit the search.</td>
</tr>
</tbody></table>
</div>
<p>When the incremental search can't find any more examples of your string, it displays the line Failing I-search: <string>. Typing <b>C-s</b> again executes a <em>wrapped</em> search, looking through the file from the beginning to the end; <b>C-r</b> searches from the end to the beginning in the same manner.</p>
<p>Sometimes, <b>C-g</b> does not take you out of an incremental search completely. If you give the search command a string that Emacs can't find in your file, <b>C-g</b> changes the string to the largest substring it could find, but <em>keeps you in the search mode.</em> If you are in a wrapped search, <b>C-g</b> takes you back to a normal search. In any event, multiple uses of <b>C-g</b> quit the search, returning you to the point where you <em>started</em> the search. You can also use most of the cursor movement commands, such as the arrows, to exit a search procedure and stay at the point where you exited it.</p>
<p>Here is an example of an incremental search. Suppose you are editing the file input.c, and you wish to find all the instances of <b>scanf</b>:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre> /* small portion of input.c */
printf("Please enter your first name: ");
scanf("%f", &fname);
printf("Please enter your last name: ");
scanf("%s",&lname);
</pre>
</div></div>
<p>Suppose the cursor is currently at the beginning of the <em>second</em> <b>printf</b> statement. You type <b>C-s</b> to start the search, then enter the letter <b>s</b>. The first thing found is the letter s in the word please. Since that isn't what you're looking for, try to enter <b>c</b>. If you type <b>v</b> by mistake (for example), use <b>Delete</b> to delete the <b>v</b>, and then enter <b>c</b>. Now you arrive at scanf.</p>
<p>Instead of typing the entire word for your search object, use <b>C-w</b> (once you're in search mode) to grab the word scanf into the search buffer. This copies it to the minibuffer without affecting the main buffer in any way. Use <b>C-r</b> to move to the earlier occurrences of scanf in that file. When you are where you want to be, hit <b>Return</b> to leave search mode and leave the cursor at that point.</p>
<h3><a name="EmacsonAthena-SearchingandReplacing%28queryreplace%29"></a>Searching and Replacing (query-replace) <a name="EmacsonAthena-searchreplace"></a></h3>
<p>Sometimes you want to find a string so that you can replace it with a different one. Say you decide to change the name of a variable in a certain module, or perhaps you want to fix a typographical error you are used to making. Rather than going through "by hand" and possibly missing some, you can have Emacs do the work for you. The command for this is M-%, or M-x query-replace, which goes through all occurrences of from-string, and see if the user wants to change any of them to to-string. <b>M-%</b> asks you about each string you want replaced. If you want everything changed, type <b>!</b> at the resulting prompt. (Alternatively, you can use the M-x replace string command, which will do the same thing without querying.) You can also respond with any of the following:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>Space, y</td>
<td class='confluenceTd'>Replace the string and continue to next occurrence.</td>
</tr>
<tr>
<td class='confluenceTd'>.</td>
<td class='confluenceTd'>Replace this string, but then quit.</td>
</tr>
<tr>
<td class='confluenceTd'>Delete, n</td>
<td class='confluenceTd'>Don't replace, but continue to next occurrence.</td>
</tr>
<tr>
<td class='confluenceTd'>Return, q</td>
<td class='confluenceTd'>Don't replace, just quit (abort the query-replace).</td>
</tr>
<tr>
<td class='confluenceTd'>!</td>
<td class='confluenceTd'>Replace all remaining occurrences without asking.</td>
</tr>
<tr>
<td class='confluenceTd'>^</td>
<td class='confluenceTd'>Back up to previous match, but do not select it</td>
</tr>
<tr>
<td class='confluenceTd'>C-h</td>
<td class='confluenceTd'>Display these and all other options.</td>
</tr>
</tbody></table>
</div>
<p>These might seem cryptic, but they are pretty straightforward. <b>Esc</b> aborts the <b>query-replace</b>, just like it does for search. <b>Delete</b> and <b>Space</b> both affect only the current choice. <b>C-h</b> lists your options at any level of the search.</p>
<p>You can do many other wonderful things with query-replace searches. See Section 13.7 of the <b>GNU Emacs Manual.</b></p>
<h3><a name="EmacsonAthena-SearchinginSeveralFilesUsinggrep"></a>Searching in Several Files Using grep <a name="EmacsonAthena-searchgrep"></a></h3>
<p>Sometimes you need to find which files contain a certain variable or word. The Emacs <b>grep</b> command searches more than one file or buffer, creating a second window called <em>compile</em> and running the operating system <b>grep</b> command. It behaves the same as the Emacs <b>compile</b> command, described in the section <a href="#EmacsonAthena-compilecommand">Compiling within Emacs</a>. (For example, you can use <b>C-x `</b> to find the various matches.)</p>
<p>Grep stands for <em>General Regular Expression Pattern</em> matching, and searches for regular expressions or simple strings. To use it, type M-x grep. (The grep command is not limited exclusively to Emacs; you can use it anywhere on Athena as well.) The minibuffer prompts you for a pattern and one or more filenames (the files don't have to be loaded into Emacs):</p>
<p><tt>Run grep (like this): grep -n</tt></p>
<p>Specify the pattern followed by the filename(s) to search (wildcard characters are allowed in filenames):</p>
<p><tt><em>pattern</em></tt><br/>
<tt><em>file1</em></tt> <br/>
<tt><em>file2</em> ...</tt></p>
<p>For instance, suppose you want to know which module contains variable indata. Use *.c to specify all the C source code files (providing you are in the proper directory). To do so, type:</p>
<p><tt><b>M-x grep</b></tt><br/>
<tt>Run grep (like this): grep -n</tt><br/>
<tt><b>indata *.c</b></tt></p>
<p>The -n argument shows matches and the line numbers on which they are found. You can use other arguments to <b>grep</b>, such as <b><del>i</b> to consider upper</del> and lowercase letters indistinguishable. For more information on the <b>grep</b> command, type <b>man grep</b> at the Athena prompt.</p>
<h3><a name="EmacsonAthena-SearchinginPrograms%3AUsingTagTables"></a>Searching in Programs: Using Tag Tables <a name="EmacsonAthena-searchtags"></a></h3>
<p>You can use <em>tags</em> to find all incidences of a variable or a string in a long program without taking the time to do an incremental search. Tags are a powerful tool for finding variables in C and Lisp programs quickly and efficiently. Tags are references telling Emacs where your variables, subroutines, and functions are defined, so Emacs can go directly to those locations without searching through the entire file.</p>
<p>To use tags, create a <em>tag table,</em> then use the Emacs tag commands to use it.</p>
<ol>
<li>To create the table, use the command <b>etags</b> at the Athena prompt to include the source code files. This creates a file called TAGS containing the new tag table. For a C program, you could type something like:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>athena% <b>etags *.c</b></tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
The next time you edit the file, the positions of the tagged items in the file will probably change. However, Emacs is intelligent enough to search out the new location, so you only really need to do this after major revisions, or when you add new functions.</li>
<li>Once you have the TAGS file, you can use it from within Emacs. The default file is TAGS in the current directory. To specify a different file, use the <b>M-x visit-tags-table</b> command. Once Emacs knows where to look, use the tag commands to locate various functions. Commands for this purpose are based on the period ( <b>.</b>) and include:
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>M-., M-x find-tag</td>
<td class='confluenceTd'>Asks for a tag, then moves you to its location. With a numeric argument, (i.e., when prefixed with <b>C-u #</b>) finds the next definition that fits the same substring.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x 4 ., M-x find-tag-other-window</td>
<td class='confluenceTd'>Finds tag and places it in the other window.</td>
</tr>
</tbody></table>
</div>
</li>
</ol>
<p>Tags are covered more fully in Section 21.11 of the <b>GNU Emacs Manual.</b></p>
<h2><a name="EmacsonAthena-MultipleBuffersandWindows"></a>Multiple Buffers and Windows <a name="EmacsonAthena-bufferswindows"></a></h2>
<h3><a name="EmacsonAthena-WorkingwithMultipleBuffers"></a>Working with Multiple Buffers <a name="EmacsonAthena-multibuffers"></a></h3>
<p>Each time you load a file into Emacs, you create a buffer. There is one buffer for each file. Emacs also has its own buffers, like <b>scratch</b> and <b>Help</b>. You can use multiple buffers to work on several files simultaneously, which is very convenient for multifile programs and documents. Some of the commands for working with buffers are:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-x C-f, M-x find-file</td>
<td class='confluenceTd'>Read the specified file into a buffer of the same name.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x C-v, M-x find-alternate-file</td>
<td class='confluenceTd'>Read the specified file into a buffer of the same name, and kill the current buffer.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x C-s, M-x save-buffer</td>
<td class='confluenceTd'>Write the contents of this buffer back to the file.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x s, M-x save-some-buffers</td>
<td class='confluenceTd'>Interactively save each modified buffer.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x C-w, M-x write file</td>
<td class='confluenceTd'>Write the current buffer into a different file.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x C-b, M-x list-buffers</td>
<td class='confluenceTd'>Show the name and status of the various buffers.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x b, M-x switch-to-buffer</td>
<td class='confluenceTd'>Switch to a new buffer (default=previous).</td>
</tr>
<tr>
<td class='confluenceTd'>C-x k, M-x kill-buffer</td>
<td class='confluenceTd'>Delete a buffer (default=current). If the buffer has been modified, Emacs asks for confirmation before killing.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x bury-buffer</td>
<td class='confluenceTd'>Place the current buffer on the bottom of the "stack," displaying the buffer underneath it (useful for cycling between many different buffers quickly).</td>
</tr>
<tr>
<td class='confluenceTd'>M-~, M-x not-modified</td>
<td class='confluenceTd'>Forget that the current buffer has been changed.</td>
</tr>
</tbody></table>
</div>
<p>Use <b>C-x C-f</b> to read a file into Emacs.</p>
<ul>
<li>If a buffer doesn't exist with that name, a buffer with the same name as the file is created, and the file is read into that buffer.</li>
<li>If a buffer already exists, you are moved into that buffer.</li>
<li>If a different buffer with the same name as the file already exists (usually a file with the same name, but in a different directory), the new buffer is given the same name with a <2>, <3>, etc, appended to it. The actual file name is not changed.</li>
</ul>
<p>Save the file using <b>C-x C-s</b> periodically and when you are finished. You can leave the buffer version of the file contents in Emacs when you move on to other tasks, or you can use <b>C-x k</b> to clear it out.</p>
<p>Use <b>C-x C-b</b> to show all the buffers currently in Emacs. The display looks something like this:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre> MR Buffer Size Mode File
-- ------ ---- ---- ----
.* area.c 1403 C /afs/athena/user/j/r/jruser/Cprogs/area.c
intro.c 4063 C /afs/athena/user/j/r/jruser/Cprogs/intro.c
% Makefile 198 Text /afs/athena/user/j/r/jruser/Cprogs/Makefile
% Makefile<2> 201 Text /afs/athena/user/j/r/jruser/Fortran/Makefile
*scratch* 0 Fundamental
*Buffer List* 193 Buffer Menu
</pre>
</div></div>
<p>For each buffer:</p>
<ul>
<li>If the buffer is the current buffer, it is marked by a period in the first column.</li>
<li>An asterisk (*) under the R means the buffer has been modified, and a percent sign (%) under the R means the buffer is read-only. (These symbols also appear in the bar of the buffer itself.)</li>
<li>The following two columns tell the name and size (in characters) of the buffer.</li>
<li>The next column lists the major mode for that buffer. (Major modes are discussed in the section <a href="#EmacsonAthena-majormode">Major Modes</a>, and also in Chapter 23 of the <b>GNU Emacs Manual</b>.)</li>
<li>The final column is the name of the actual file the buffer corresponds to, if any. If the file name would make the column wider than your Emacs window, the end of the filename is replaced by a dollar sign ($).</li>
</ul>
<p>In the example, the first four buffers are user files, <b>scratch</b> is a buffer kept by Emacs, and <b>Buffer List</b> is the buffer containing this listing. The buffer area.c has been modified, but none of the others have.</p>
<p>To go to a buffer from the buffer list, move the cursor to the line for that buffer and type <b>f</b> to go to that buffer. You can also use <b>C-x b</b> to switch to another buffer. Hit <b>Return</b> to go to the default buffer (usually the previous buffer) or type in the name of the buffer. You can use completion commands as usual, except that <b>Return</b> doesn't complete; it goes to whatever buffer is explicitly named, creating it if necessary.</p>
<p>For more information about buffers, consult Chapter 19 of the <b>GNU Emacs Manual.</b></p>
<h3><a name="EmacsonAthena-WorkingwithMultipleWindows"></a>Working with Multiple Windows <a name="EmacsonAthena-multiwindows"></a></h3>
<p>Emacs <em>windows</em> are subdivisions of the actual X window that the Emacs process creates. Emacs windows are not the same as X windows. X windows create a whole new pseudo-terminal, and are created from the Athena prompt. They only work on workstations. Emacs windows are created from inside Emacs and simply partition the existing Emacs window. They work on all terminals and split the current Emacs window (screen), so you can view more than one buffer at time. You can create varying sizes and numbers of Emacs windows, and even split them horizontally so that you have side-by-side Emacs windows (though this is rarely done). Emacs often creates a second window when you use the <b>C-h</b> Help facility. Here are the basic commands for working with Emacs windows:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>left mouse button</td>
<td class='confluenceTd'>Place Emacs cursor at location of mouse (in whatever Emacs window that happens to be).</td>
</tr>
<tr>
<td class='confluenceTd'>C-x o ("oh"), M-x other-window</td>
<td class='confluenceTd'>Move cursor to other Emacs window.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x 0 (zero), M-x delete-window</td>
<td class='confluenceTd'>Remove this Emacs window from screen (make the current Emacs window "nothing") and move to other Emacs window.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x 1 (one), M-x delete-other-windows</td>
</tr>
<tr>
<td class='confluenceTd'>Remove the other Emacs window from screen (make current Emacs window the only "one").</td>
</tr>
<tr>
<td class='confluenceTd'>C-x 2, M-x split-window-vertically</td>
<td class='confluenceTd'>Split the Emacs window the cursor is in into two Emacs windows, one above the other.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x 3, M-x split-window-horizontally</td>
<td class='confluenceTd'>Split the Emacs window the cursor is in into two Emacs windows, beside each other.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x 4 b, M-x switch-to-buffer-other-window</td>
<td class='confluenceTd'>Create an Emacs window, then place specified buffer there.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x 4 f, M-x find-file-other-window</td>
<td class='confluenceTd'>Create an Emacs window then do a find-file there.</td>
</tr>
</tbody></table>
</div>
<p>The <b>C-x 4</b> commands select another window, splitting the current Emacs window into two windows if there is only one, prompt you for the file or buffer to put in the new window, and place the cursor there. Most help commands create a new Emacs window but leave the cursor where it was. The cursor location determines where text is entered and commands executed. You can switch between Emacs windows by using <b>C-x o</b> ("oh"). On a workstation, clicking the LEFT button moves the input cursor to the point that the mouse cursor is over, placing you in that Emacs window.</p>
<p>For more information about Emacs windows, see Chapter 20 of the <b>GNU Emacs Manual</b>.</p>
<h3><a name="EmacsonAthena-WorkingwithSurpriseBuffersandWindows"></a>Working with Surprise Buffers and Windows <a name="EmacsonAthena-surprise"></a></h3>
<p>One of the most powerful aspects of Emacs is that you can use it to edit more than one buffer at a time. Unfortunately, this aspect of the editor sometimes makes Emacs a very confusing program to work with. Sometimes the editor creates a second buffer for you without your consent.</p>
<p>For instance, when Emacs wants to show you something that is too long to fit in the single-line minibuffer, it puts the text into a new buffer and displays the new buffer by dividing the screen into two Emacs windows. Many Emacs commands do this, most notably the help command <b>C-h.</b></p>
<p>Sometimes Emacs won't even bother to split the window into two buffer areas. Instead, the editor <em>overlays</em> your editing buffer area with the new buffer area. When this happens, it's natural to think that you have lost your file.</p>
<p>However, you haven't. Emacs never "throws away" a buffer without explicit instructions to do so. Your original editing buffer is still there, and the minibuffer displays a message about how to restore the previous buffer. This can always be accomplished by using the appropriate Emacs buffer or window command. These are covered in the sections <a href="#EmacsonAthena-multibuffers">Working with Multiple Buffers</a> and <a href="#EmacsonAthena-multiwindows">Working with Multiple Windows</a>.</p>
<p>The most important thing to notice is where the Emacs input cursor is. Return to your working buffer as follows:</p>
<ul>
<li>If your working buffer is showing on the screen, just make sure the input cursor is in that buffer window. If it isn't, then move it there using <b>C-x o</b> ("oh"), repeatedly if necessary. Then type <b>C-x 1</b> to return to one-window mode.</li>
<li>If your working buffer isn't showing on the screen, you must get it on the screen. Type <b>C-x b</b> for <b>switch-to-buffer</b>. Emacs prompts:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>switch to buffer: (default <em>buffername</em>)</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
If <em>buffername</em> is the name of your working buffer, press <b>Return</b>. If it isn't, type the name of your working buffer and press <b>Return</b>.</li>
<li>If you can't remember the name of your working file, <b>C-x C-b</b> shows you a list of all the buffers. (This command splits the Emacs window in two and displays the list in the lower area.) Now type <b>C-x b</b> to switch to your working buffer (or type <b>f</b> or <b>Return</b> on the line in the buffer list).</li>
</ul>
<h2><a name="EmacsonAthena-BufferModes"></a>Buffer Modes <a name="EmacsonAthena-modes"></a></h2>
<h3><a name="EmacsonAthena-MajorModes"></a>Major Modes <a name="EmacsonAthena-majormodes"></a></h3>
<p>The <em>major mode</em> of a buffer indicates the nature of the file it is editing. Each buffer has a single major mode associated with it. The mode is determined by the suffix on the filename, with the default mode being Fundamental. Fundamental mode has no mode-specific redefinitions or variable settings; each Emacs command and option uses the default behavior. Editing text in a human language (as opposed to a programming language) should be done in Text mode.</p>
<p>You can also invoke a major mode by typing <b>M-x <em>mode-name</em></b>. Some modes, like Buffer Menu, only work on special buffers. The current major mode is listed in parentheses in the bar.</p>
<p>Major modes can serve a wide variety of purposes. The mail mode allows you to read mail and use MH commands within Emacs. The picture mode lets you move to arbitrary points on the screen using the mouse and arrow keys. Each mode redefines certain keys and commands to behave in an appropriate manner. You can obtain a complete description of the current major mode by typing <b>C-h m</b>, which lists the mode-specific commands for that buffer.</p>
<p>There are far more major modes than can be discussed here. The special ones most often used are TeX mode and the various language modes. They provide convenient abbreviations for often-used commands and ways to format text more easily. Two of these are described below.</p>
<h3><a name="EmacsonAthena-TeXMode"></a>TeX Mode <a name="EmacsonAthena-texmode"></a></h3>
<p>You are placed in TeX mode anytime you have a filename ending in .tex. TeX mode is much like Text mode, with numerous time-saving features.</p>
<p>Some of the special features include the following:</p>
<ul>
<li>Emacs informs you of matches on the following pairs:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>$ $ { } [ ] ( )</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
<li>The <b>M-x validate-tex-buffer</b> command checks a buffer for paragraphs containing mismatched $'s or braces.</li>
<li>Typing a double quotation mark, <b>"</b>, inserts two single left quotes, <b>``</b>, when it seems to be the beginning of a quotation, and two single right quotes, <b>''</b>, when it appears to be the end. (Emacs inserts an actual double quotation mark only after a backslash.)</li>
<li>The <b>C-c {</b> command inserts both the left and right curly braces, leaving your cursor between them.</li>
</ul>
<p>For a complete listing of the special features of TeX major mode, type <b>C-h m</b> from an Emacs window in which you are editing a .tex file.</p>
<h3><a name="EmacsonAthena-EditingDirectories%3ADiredMode"></a>Editing Directories: Dired Mode <a name="EmacsonAthena-diredmode"></a></h3>
<p>You can use your Emacs window to rename, copy, move, delete, and look at files in a directory by using the <b>C-x d</b> command. <b>C-x d</b> works similarly to <b>C-x C-f</b> (the <b>find-file</b> command) except that you give it the name of a directory. Emacs displays the current listing of all files in that directory, and enters Dired mode.</p>
<p>(You can also use <b>C-x C-f</b> to enter Dired mode, by trying to "find" a directory. If this doesn't work when you are trying to find the directory you are working in, delete the final slash in the file name.)</p>
<p>You can type <b>C-h m</b> to get a list of the special commands available in this mode. Some of the most useful include the following:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>f</td>
<td class='confluenceTd'>Load the file the cursor is on.</td>
</tr>
<tr>
<td class='confluenceTd'>d</td>
<td class='confluenceTd'>Flag the file the cursor is on for deletion.</td>
</tr>
<tr>
<td class='confluenceTd'>~</td>
<td class='confluenceTd'>Flag all backup (~) files for deletion.</td>
</tr>
<tr>
<td class='confluenceTd'>R</td>
<td class='confluenceTd'>Prompt for a new filename and rename the file.</td>
</tr>
<tr>
<td class='confluenceTd'>C</td>
<td class='confluenceTd'>Prompt for a new filename and copy the file to that name.</td>
</tr>
<tr>
<td class='confluenceTd'>x</td>
<td class='confluenceTd'>Remove all files flagged for deletion.</td>
</tr>
<tr>
<td class='confluenceTd'>g</td>
<td class='confluenceTd'>Update the displayed directory, removing all flags in the process.</td>
</tr>
</tbody></table>
</div>
<h3><a name="EmacsonAthena-MinorModes"></a>Minor Modes <a name="EmacsonAthena-minormode"></a></h3>
<p>Unlike major modes, you can have as many or as few active <em>minor modes</em> as you want in each buffer, and each minor mode is active in the buffer only if you explicitly activate it. You can toggle each minor mode by giving its name as an argument to <b>M-x</b>. Active minor modes are listed on the mode line immediately after the current major mode. Some of the more useful minor modes are auto-fill-mode and abbrev-mode.</p>
<p>In auto-fill-mode, after you pass a certain column, inserting a space breaks the line at a previous space, "wrapping" the text. This is very useful in text and Fortran to prevent long lines. Abbrev-mode enables expansion of abbreviations (see the section <a href="#EmacsonAthena-abbrevmode">Using Abbreviations</a>).</p>
<p>For example, in a buffer that has C mode, auto-fill-mode, and abbrev-mode, the mode line looks something like this:</p>
<p><tt>--**-Emacs: area.c (C Fill Abbrev)------20%-----------</tt></p>
<p>A full description of minor modes is given in Section 28.1 of the <b>GNU Emacs Manual</b>.</p>
<h3><a name="EmacsonAthena-UsingAbbreviations%3Aabbrevmode"></a>Using Abbreviations: abbrev-mode <a name="EmacsonAthena-abbrevmode"></a></h3>
<p>When you are programming, you should use long, descriptive names for your variables, so others can understand them; however, such names can be difficult to type. There are also a few words that are frequently misspelled or mistyped. To solve these, as well as other related problems, you can use <em>word abbreviations</em>.</p>
<p>Word abbreviations work by defining a short word or group of letters to stand for a much larger string. For example, you can set "mit" to stand for "the Massachusetts Institute of Technology." From then on, whenever you type <b>mit</b>, Emacs automatically replaces it with the longer string.</p>
<h3><a name="EmacsonAthena-DefiningAbbreviations"></a>Defining Abbreviations <a name="EmacsonAthena-abbrevdefining"></a></h3>
<p>There are several ways to define abbreviations. Here are two:</p>
<ul>
<li><b>Identify the expansion first, then name the abbreviation</b>. For instance, in the file calc.c, the variable surface_area_of_object is used a lot. To enter saoo as an abbreviation for surface_area_of_object, place the cursor after the word "object" in the buffer and type:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt><b>C-u 4 C-x a g saoo</b></tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<b>C-u 4</b> indicates that the abbreviation refers to the previous four words (ignoring punctuation).</li>
<li><b>Enter the abbreviation first, then then define the expansion</b>. This avoids the necessity of having to count the number of words, and helps when you are abbreviating words with punctuation inside of them. To do the same thing as in our previous example, place the cursor after the abbreviation in the buffer and type:<br/>
<tt><b>C-x a i g surface_area_of_object</b></tt></li>
</ul>
<h3><a name="EmacsonAthena-UsingAbbreviations"></a>Using Abbreviations <a name="EmacsonAthena-abbrevusing"></a></h3>
<p>Once you have defined your abbreviations, you must be in abbrev-mode to use them; enter the mode by typing <b>M-x abbrev-mode</b>. In abbrev-mode, Emacs automatically expands your abbreviations when you type them. (You can use the <b>C-x '</b> command to expand the abbreviation before the input cursor if you're not in abbrev-mode, or to expand an abbreviation you typed before entering abbrev-mode.) If you have no further need of abbreviations at all, type <b>M-x abbrev-mode</b> again to turn automatic expansion off.</p>
<p>Abbreviations are very useful to automatically correct common misspellings. Let's say you commonly mistype "the" as "teh." You can fix this by entering abbrev-mode, and defining "teh" as an abbreviation for "the." Now whenever you type <b>teh</b> Emacs will automatically correct it. (Be careful about doing this with common letter clusters, or you could end up in more trouble than you started with.)</p>
<p>Use <b>M-x unexpand-abbrev</b> to undo the most recently defined abbreviation. For example, if you define "mit" as the longer string given above, Emacs dutifully turns the letters "mit" in words like "admit" and "submit" into their expanded versions, which may not be your intention.</p>
<p>To list what abbreviations you have defined, use <b>M-x edit-abbrevs</b>. This produces a listing of the various abbreviations in a new buffer, and allows you to modify them. Use <b>C-c C-c</b> to save your modifications, then <b>C-x b</b> to return to the buffer you were previously editing.</p>
<p>To change the definition of an abbrev, just define a new definition. When the abbrev has a prior definition, the abbrev definition commands ask for confirmation for replacing it. To remove an abbrev definition, give a negative argument to the abbrev definition command:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt><b>C-u - C-x a g</b></tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<b>M-x kill-all-abbrevs</b> removes all the abbrev definitions there are. Here is a listing of the most useful abbreviation commands:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-x a g, M-x add-global-abbrev</td>
<td class='confluenceTd'>Define an abbreviation for word before the cursor location. Numeric argument specifies how many words to be abbreviated; you are prompted for the abbreviation.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x a i g, M-x inverse-add-global-abbrev</td>
<td class='confluenceTd'>This prompts you to specify an expansion for the abbreviation before the cursor location.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x unexpand-abbrev</td>
<td class='confluenceTd'>Undo last abbreviation expansion.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x ', M-x expand-abbrev</td>
<td class='confluenceTd'>Expand an abbreviation even if not in abbrev-mode.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x edit-abbrevs</td>
<td class='confluenceTd'>Show abbreviation table, and allow you to modify it.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x read-abbrev-file</td>
<td class='confluenceTd'>Read in a file of abbreviations.</td>
</tr>
</tbody></table>
</div>
<p>The sample ~/.emacs file in the section <a href="#EmacsonAthena-emacsfile">Making Changes Permanent Using Your .emacs File</a> has an example of how to read in abbreviation files and set abbrev-mode when in text-mode. For more information about abbreviations, see Chapter 28 of the <b>GNU Emacs Manual</b>.</p>
<h2><a name="EmacsonAthena-CustomizingYourEmacsEnvironment"></a>Customizing Your Emacs Environment <a name="EmacsonAthena-customizing"></a></h2>
<h3><a name="EmacsonAthena-EmacsVariables"></a>Emacs Variables <a name="EmacsonAthena-variables"></a></h3>
<p>Human nature being what it is, you can never please all the people all of the time. That's why Emacs has variables that you can change to suit your own tastes. There are really only two commands you need to know for using variables, with a third for the curious:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>M-x list-options</td>
<td class='confluenceTd'>Show all user-accessible variables, with value and description.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x set-variable</td>
<td class='confluenceTd'>Enter the name of a variable and set it to specified value (for this Emacs session only).</td>
</tr>
<tr>
<td class='confluenceTd'>C-h v, M-x describe-variable</td>
<td class='confluenceTd'>Give the documentation on specified variable.</td>
</tr>
</tbody></table>
</div>
<p>There are many uses for variables. The <b>list-options</b> command has a complete list of anything you are likely to use, so it is rather long. Here are just a few, with the default value shown in brackets:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Variable</th>
<th class='confluenceTh'>Description</th>
</tr>
<tr>
<td class='confluenceTd'>c-indent-level [2]</td>
<td class='confluenceTd'>For each open brace in C mode, number of additional spaces to indent next line.</td>
</tr>
<tr>
<td class='confluenceTd'>fill-column [70]</td>
<td class='confluenceTd'>In auto-fill-mode, the column after which a word is broken.</td>
</tr>
<tr>
<td class='confluenceTd'>kill-ring-max [30]</td>
<td class='confluenceTd'>The number of different regions saved in the kill ring buffer.</td>
</tr>
<tr>
<td class='confluenceTd'>lpr-command ["lpr"]</td>
<td class='confluenceTd'>Shell command for printing a file.</td>
</tr>
</tbody></table>
</div>
<p>There are too many variables to list here. To find a variable relating to a specific task, use the <b>C-h a</b> command. For more information about how to manipulate variables, see Section 28.2 of the <b>GNU Emacs Manual</b>.</p>
<h3><a name="EmacsonAthena-MakingChangesPermanentUsingYour.emacsFile"></a>Making Changes Permanent Using Your .emacs File <a name="EmacsonAthena-emacsfile"></a></h3>
<p>With Emacs variables and keyboard macros (covered under <a href="#EmacsonAthena-advanced">Advanced Techniques</a>), there are many alterations you can make in your Emacs environment. Instead of trying to type in all those variables and definitions each time you need them, you can create a file called ~/.emacs in your home directory that is read each time you start Emacs.</p>
<p>This file is quite powerful, so be careful not to do anything that radically changes your Emacs. If something goes wrong, use the <b>-q</b> command line option with the <b>emacs</b> command to enter Emacs without reading the ~/.emacs file.</p>
<p>The ~/.emacs file is written in Emacs Lisp, which makes it simple to customize your Emacs if you know Lisp. Looking at other people's ~/.emacs files is probably the best way to learn more about them. Here is a sample ~/.emacs file:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre> ;;; Read in the file of word abbreviations
;;; If the file does not exist, this command will not work!
(read-abbrev-file abbrev-file-name) ;Standard Abbreviations
;Default=~/.abbrev-defs
;;; Access one emacs window from multiple programs (use emacsclient)
(server-start)
;;; Prevent appearance of copyleft on Emacs startup
(setq inhibit-startup-message t)
;;; Don't need to check completions with these endings
;;; in find-file and similar commands
(setq completion-ignored-extensions
(append completion-ignored-extensions
(quote
(".dvi" ".PS" ".aux" ".otl" ".bak" ".err" ".o"))))
;;; Set Major Mode Hooks
;;; Set mode according to filename extension
(setq auto-mode-alist
'(("\\.text$" . text-mode)
("\\.c$" . c-mode)
("\\.h$" . c-mode)
("\\.tex$" . TeX-mode)
("\\.el$" . emacs-lisp-mode)
("\\.scm$" . scheme-mode)
("\\.l$" . lisp-mode)
("\\.lisp$" . lisp-mode)
("\\.f$" . fortran-mode)))
;;; If no other mode indicated, use Text as Major Mode
(setq default-major-mode 'text-mode) ;default mode
;;; Set auto-fill and abbreviation for Text Mode
(setq text-mode-hook
'(lambda nil
(setq fill-column 72)
(auto-fill-mode 1)
(abbrev-mode 1)))
;;; Set abbreviations and customized indenting for C Mode
(setq c-mode-hook
'(lambda nil (progn
(setq c-brace-offset 2)
(setq c-auto-newline 1)
(abbrev-mode 1))))
</pre>
</div></div>
<p>Emacs Lisp is very similar to Common Lisp, using parentheses as delimiters and semicolons (;) as comment indicators. There are a few other things you should note in this file:</p>
<ul>
<li>You use <b>setq</b> to set Emacs variables (see the section <a href="#EmacsonAthena-variables">Emacs Variables</a>):
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>(setq</tt><br/>
<tt><em>variable</em></tt> <br/>
<tt><em>value</em>)</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
<li>Emacs allows you to specify <em>hooks</em> to customize a particular <em>major mode,</em> rather than all of Emacs or the buffer you are in when you start up. There is no hook for fundamental-mode. To make your initial buffer a mode other than fundamental-mode (so you can customize it), you should use:<br/>
<tt><b>(setq default-major-mode 'text-mode)</b></tt><br/>
This makes your default mode text-mode. You can then hook a series of commands to activate whenever you enter that mode. The syntax is:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>(setq</tt><br/>
<tt><em>major-mode</em>-hook '(lambda nil</tt><br/>
<tt>(</tt><br/>
<tt><em>progn</em></tt><br/>
<tt><em>command(s)</em>)))</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
Thus, the example from the above ~/.emacs file to set auto-fill mode is:</li>
</ul>
<p><tt><b>(setq text-mode-hook '(lambda nil (setq fill-column 72) (auto-fill-mode 1)))</b></tt> </p>
<p>You don't have to know Emacs Lisp to use hooks. Just know what mode you are working with, what commands you want to execute or variables you want to set, and make sure you have all your parentheses balanced. The quote is necessary.</p>
<h2><a name="EmacsonAthena-AdvancedTechniques"></a>Advanced Techniques <a name="EmacsonAthena-advanced"></a></h2>
<h3><a name="EmacsonAthena-UsinganExistingEmacs%28emacsclient%29"></a>Using an Existing Emacs (emacsclient) <a name="EmacsonAthena-emacsclient"></a></h3>
<p>The <b>emacsclient</b> program tells programs like <b>comp</b> to use an existing Emacs window rather than starting their own. Note that if you tell programs to do this, they <em>always</em> try to find an existing Emacs. They fail if you don't have an Emacs running, or if you are in a tty session; they will not work until you create an Emacs and try again. Make sure you know what you are doing before you effect the conversion. There are three steps:</p>
<ol>
<li>Set the EDITOR and VISUAL environment variables in your ~/.environment file:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>setenv EDITOR emacsclient # Default editor</tt><br/>
<tt>setenv VISUAL emacsclient # Screen editor</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
(If you don't have EDITOR and VISUAL set in your ~/.environment file, just insert these lines. If you don't have a ~/.environment file, create it.)</li>
<li>Modify your ~/.mh_profile so that <b>comp</b> and <b>repl</b> know where to look:<br/>
<tt>Editor: emacsclient</tt></li>
<li>Add the following line to your ~/.emacs file (create it if you don't have it):<br/>
<tt>(server-start)</tt></li>
</ol>
<p>The next time you log in, everything is set up to use the Emacs server (you still type <b>emacs &</b> to call up the editor to begin with). Of course, you can change everything back to the way it was by substituting emacs for emacsclient in ~/.environment and ~/.mh_profile.</p>
<h3><a name="EmacsonAthena-IssuingSophisticatedCommands"></a>Issuing Sophisticated Commands <a name="EmacsonAthena-sophcommands"></a></h3>
<p>You can issue commands with arguments, or reissue a complex command you already issued. The following commands help you do this.</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-u # <em>command</em>, M-x universal-argument</td>
<td class='confluenceTd'>Do <em>command</em> # times. The default value is 4, and typing <b>C-u</b> again quadruples the previous value of the argument. You type this before commands that require a numeric argument.</td>
</tr>
<tr>
<td class='confluenceTd'>M-#</td>
<td class='confluenceTd'>Same as <b>C-u #</b>, without the default.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x Esc Esc, M-x repeat-complex-command</td>
<td class='confluenceTd'>Display the last command of more than two keystrokes, and allow you to edit and execute it. A numeric argument calls up a list of earlier commands. While inside this list, you can use <b>M-p</b> and <b>M-n</b> to move up and down.</td>
</tr>
</tbody></table>
</div>
<p>For example, <b>C-u M-d</b> deletes the next four words; either <b>C-u 3 M-d</b> or <b>M-3 M-d</b> deletes the next three words.</p>
<h3><a name="EmacsonAthena-KeyboardMacros"></a>Keyboard Macros <a name="EmacsonAthena-macros"></a></h3>
<h4><a name="EmacsonAthena-UsingKeyboardMacros"></a>Using Keyboard Macros <a name="EmacsonAthena-macrosusing"></a></h4>
<p>Keyboard macros allow you to define a new command to execute a specific group of several commands, and even name and save the new command so that you can continue to use it.</p>
<p>For example, you might want to delete a word, move down one line, and replace the word there, all with one command – but unfortunately, Emacs doesn't have a <b>move-word-down-line</b> command.</p>
<p>Here are the basic commands for working with keyboard macros:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-x (, M-x start-kbd-macro</td>
<td class='confluenceTd'>Begin the definition process.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x ), M-x end-kbd-macro</td>
<td class='confluenceTd'>Conclude the definition process.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x e, M-x call-last-kbd-macro</td>
<td class='confluenceTd'>Execute the last defined keyboard macro.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x name-last-kbd-macro</td>
<td class='confluenceTd'>Give a name to the current macro for the duration of this session.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x insert-kbd-macro</td>
<td class='confluenceTd'>Insert a named macro into the current file. (Useful for saving a macro definition in your ~/.emacs file.)</td>
</tr>
<tr>
<td class='confluenceTd'>M-x global-set-key</td>
<td class='confluenceTd'>Assign a keybinding to an Emacs command or named macro.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x global-unset-key</td>
<td class='confluenceTd'>Remove a keybinding from the Emacs command or named macro to it is assigned</td>
</tr>
</tbody></table>
</div>
<p>C-x ( starts the macro process, and the word Def is added to the mode line. From then on, everything you type is executed normally <em>and</em> stored in the macro. This continues until you type <b>C-x )</b>. To have that exact sequence repeated over again, starting from the current location of the cursor, type <b>C-x e.</b> Whenever you define a new macro, the old one is lost unless it has a name. All macros, named or not, only last until you exit Emacs.</p>
<p>To make a macro more permanent, give it a name (using <b>name-last-kbd-macro</b>) and then add it to a file (using <b>insert-kbd-macro</b>) for later use. In particular, if you put macro definitions into your ~/.emacs file (see the section on <a href="#EmacsonAthena-emacsfile">Making Changes Permanent Using Your .emacs File</a>), you can have those commands available to you whenever you enter Emacs.</p>
<p>Suppose you wanted (for some inexplicable reason) to delete the last word on every line that had the word "happy" in it. You need to start the macro ( <b>C-x (*), find "happy" ( *C-s happy</b>), exit the search, move to the end of the line ( <b>C-e</b>), delete the word behind the cursor ( <b>M-Delete</b>), and end the definition ( <b>C-x )</b>). To do this, type:</p>
<p><tt><b>C-x ( C-s happy C-e M-Delete C-x )</b></tt></p>
<p>Don't forget that all commands are executed while you type them into the macro. To make this new command more permanent, use <b>name-last-kbd-macro</b> to assign it the cheerful sounding name of <b>destroy-happiness</b>. Having named it, you can now execute this macro by typing <b>M-x destroy-happiness</b>, and you can define another macro without losing this one, until you exit Emacs.</p>
<p>To preserve this macro and this name, go to (or create) your ~/.emacs file and type:</p>
<p><tt><b>M-x insert-kbd-macro</b></tt></p>
<p>Insert kbd macro (name): <b>destroy-happiness</b></p>
<p>This prints into your ~/.emacs file the code for the commands listed above. (You don't have to know Emacs Lisp or understand this code, but that's okay, Emacs understands them.)</p>
<p>Of course, there are more constructive uses for keyboard macros as well; lots of fun things you can do with macros are explained in Chapter 35 of the <b>GNU Emacs Manual.</b></p>
<p>Keyboard macros, like other commands, may be bound to keystrokes for convenient use. See the <a href="#EmacsonAthena-macrosbindings">Customizing Command Key Bindings</a> module for more information.</p>
<h4><a name="EmacsonAthena-CustomizingCommandKeyBindings"></a>Customizing Command Key Bindings <a name="EmacsonAthena-macrosbindings"></a></h4>
<p>There are many Emacs commands that are bound to particular keystrokes for easy access. Many of these are listed in these modules: <b>C-x C-s</b> runs the <b>save-buffer</b> command, <b>C-x i</b> runs the <b>insert-file</b> command, and so forth. Often, however, your favorite commands are not bound to keystrokes, making it necessary to type out the whole command name. For example, to get Emacs to automatically do a carriage return at the end of each line, you can use the <b>auto-fill-mode</b> command, but in each buffer you must type out the whole command: <b>M-x auto-fill-mode.</b></p>
<p>Emacs commands are easy to customize, using the <b>global-set-key</b> command. You can also use this command to assign keystrokes to named keyboard macros (see <a href="#EmacsonAthena-macrosusing">Using Keyboard Macros</a>).</p>
<p>Let's say, for example, that you have defined a macro to delete the last word in the next line wherein the word "happy" is found, and you have named this macro "destroy-happiness" (see <a href="#EmacsonAthena-macrosusing">Using Keyboard Macros</a> for an explanation of how to do this). You can execute this series of commands by typing <b>M-x destroy-happiness</b>.</p>
<p>Since this name is still a little tedious to type, and there is a lot of happiness that must be destroyed, you can assign it to a key. You decide to use the command <b>M-</b>* to run this command. You type:</p>
<p><tt><b>M-x global-set-key</b></tt><br/>
<tt>Set Key globally:</tt><br/>
<tt><b>M-</b> *</tt><br/>
<tt>Set key M-* to command:</tt><br/>
<tt><b>destroy-happiness</b></tt></p>
<p>So, the next time you want to kill the last word on a line that has the word "happy" in it, type <b>M-</b>*.</p>
<p>You can use the <b>global-unsetset-key</b> command to <em>undo</em> a binding that is especially annoying or awkward. For example, let's say you keep accidentally hitting <b>M-z</b> when you mean to hit <b>M-<.</b> ( <b>M-z</b> is bound by default to the <b>zap-to-char</b> command, which deletes all text until the first incidence of a particular character.) Let's say, further, that you can conceive of no possible use for the <b>M-z</b> command. You can solve this problem by unsetting the key. (Notice that now, to use the <b>zap-to-char</b> command, you must type out its full name.)</p>
<p>Be careful not to over-write any useful commands by mistake. Use <b>C-h c</b> to find out what a key sequence is being used for by default. Remember, also, that many keystrokes in Emacs are prefixes, and binding those keystrokes to a command will keep you from being able to use all of the commands that are prefixed with that keystroke.</p>
<p>All bindings you define go away when you exit Emacs. If you want to save these bindings, put them into your ~/.emacs file (see <a href="#EmacsonAthena-customizing">Customizing Your Emacs Environment</a>). The syntax for the command is:</p>
<p><tt>(global-set-key "</tt><br/>
<tt><em>keystrokes</em>" '</tt><br/>
<tt><em>command-name</em>)</tt><br/>
<tt>(global-unset-key "</tt><br/>
<tt><em>keystrokes</em>")</tt></p>
<p>The quotes are all important. <b>Ctrl</b> and <b>Meta</b> keystrokes are written <b>\C-</b> and <b>\M-</b>. For example, to put the <b>M-</b>* command into your ~/.emacs file, you would type:</p>
<p><tt><b>(global-set-key "\M-*" 'destroy-happiness)</b></tt></p>
<h3><a name="EmacsonAthena-EditingandCompilingProgramsinEmacs"></a>Editing and Compiling Programs in Emacs <a name="EmacsonAthena-programs"></a></h3>
<h4><a name="EmacsonAthena-LanguageModes"></a>Language Modes <a name="EmacsonAthena-languagemodes"></a></h4>
<p>There are many programming language modes, covering a wide range of behavior. The command <b>M-x run-lisp</b>, for example, actually runs a Lisp interpreter inside an Emacs window. You can look at Section 22.2 of the <b>GNU Emacs Manual</b> if you are interested in some of the Lisp interpretation abilities.</p>
<p>The language mode chosen depends on the suffix of the filename: .c for C, .f for FORTRAN, and .l for LISP. Each mode has its own peculiarities. LISP balances parentheses. C works with braces. FORTRAN has many abbreviations for common commands. All of them, however, share facilities for doing <em>indentation</em> and <em>commenting</em>.</p>
<p>In language mode, the <b>Tab</b> key, instead of just indenting eight spaces, moves the line to its proper indentation based on the number of open brackets and the indentation of previous lines. <b>Delete</b> is modified so that when it it tries to delete a tab, it treats it like an equivalent number of spaces. The indentation may not be perfect, but it is pretty good.</p>
<p>The key bindings for the commenting commands are based on the semicolon, since that is the comment character used in Lisp. Each language has its own particular comment character (which Emacs' language modes know about), but the commands are the same. The behavior is a little different in FORTRAN, of course. Here are a few of the most useful commenting commands:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>M-;, M-x indent-for-comment</td>
<td class='confluenceTd'>Start a comment on this line.</td>
</tr>
<tr>
<td class='confluenceTd'>M-j, M-x indent-new-comment-line</td>
<td class='confluenceTd'>Continue a comment on the next line if currently on a comment line.</td>
</tr>
<tr>
<td class='confluenceTd'>C-x ;, M-x set-comment-column</td>
<td class='confluenceTd'>Set column where comments should begin. Use either the cursor position or a numeric argument.</td>
</tr>
</tbody></table>
</div>
<p>As with any mode, you can type <b>C-h m</b> to get all the mode-specific commands and variables. For detailed descriptions of these and other language mode commands, read Chapter 26 of the <b>GNU Emacs Manual</b>.</p>
<h4><a name="EmacsonAthena-CompilingWithinEmacs"></a>Compiling Within Emacs <a name="EmacsonAthena-compilecommand"></a></h4>
<p>People often use Emacs inefficiently when writing a program. They edit the file, exit Emacs, compile the file, re-enter Emacs, search for errors, fix them, then start the whole cycle over again – unnecessarily. There are many Emacs facilities to speed up the whole process of compilation and debugging for programming languages.</p>
<p>The Emacs <b>compile</b> command allows you to compile files without ever leaving Emacs. When executed, it asks you what command you want to execute, then it asks you to save each buffer. The default command is <b>make -k</b> for programs. The <b>make</b> command uses a file called a Makefile to help compile multifile programs. (For more information, type <b>man make</b> at your Athena prompt.) If you don't have (or don't wish to use) a makefile, you can backspace over the default command and specify any command you like: <b>cc</b>, <b>f77</b>, <b>lint,</b> etc.</p>
<p>Emacs then splits the screen, creating a second window, and runs the specified command in the buffer <b>compile</b>. You can continue working in the first window, if you like. To abort the compilation, use the command <b>kill-compilation</b>:<br/>
<tt><b>M-x kill-compilation</b></tt><br/>
If you start up a new compilation while an old one is still running, Emacs offers to kill off the old one. Otherwise, be sure to stay in two-window mode so you can use the error-finding routines. (See the section <a href="#EmacsonAthena-compileerrors">Finding Compilation Errors</a>.)</p>
<h4><a name="EmacsonAthena-FindingCompilationErrors"></a>Finding Compilation Errors <a name="EmacsonAthena-compileerrors"></a></h4>
<p>You've just written your program, and have used the <b>compile</b> command to compile it in Emacs. Unless you are lucky, you now have a buffer full of errors. Each error has a file name and line number associated with it. Emacs can use this information to move directly to those errors. The command to do that is <b>C-x `</b>, which is <b>next-error</b>.</p>
<p>To start, type <b>C-x `</b>. Notice that <b>`</b> is the backquote, or grave accent, not the apostrophe. This moves you to the occurrence of the first error listed in <b>compile</b>. Emacs moves backwards and forwards, and even loads in new files if necessary. Once there, you can move around, insert and delete, and do whatever is necessary to fix the error. When you are ready, just type <b>C-x `</b> again to move to the next error. The second window scrolls to follow which error you are on. A numeric argument starts over from the first error.</p>
<p>You can stop at any time. For more information about compiling and debugging files within Emacs, consult Chapter 27 of the <b>GNU Emacs Manual</b>.</p>
<h3><a name="EmacsonAthena-VersionControl"></a>Version Control <a name="EmacsonAthena-vc"></a></h3>
<p>Version control systems are packages that record multiple versions of a source file and record history infomation about the creation of these different versions. The version control features of Emacs works with multipme version control systemsincluding RCS, CVS, SVN, GIT, and the less-recommended SCCS. Type <b>man rcs</b>, <b>man cvs</b>, or <b>man sccs</b> at the athena prompt for more information about each of these control systms. The rest of this document deals with version control features within Emacs, regardless of which version control system you use.</p>
<p>The most useful VC command is an all-purpose command that performs either locking or check-in, depending on the situation. <b>C-x C-q</b>, <b>C-x v v</b>, and <b>M-x vc-next-action</b> all perform the the next logical version control operation on the file, be it checking in a new version or checking out a version to you. However, the precise action of the command depends on the state of the file and the version control system you are using.</p>
<p>When you check in changes with <b>C-x v v</b> a buffer called '<b>VC-Log</b>' pops up for you to enter the log entry. When you are finished, type <b>C-c C-c</b> to actually check in the file.</p>
<p>With version control, you can look at and compare old versions of files. Versions are numbered and the version number appears in the status bar of the Emacs window.</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-x v ~ <em>version</em> RET</td>
<td class='confluenceTd'>Examine version <em>version</em> of the visited file, in a buffer of its own</td>
</tr>
<tr>
<td class='confluenceTd'>C-x v =, M-x vc-diff</td>
<td class='confluenceTd'>Compare the current buffer contents with the latest checked-in version of the file</td>
</tr>
<tr>
<td class='confluenceTd'>C-u C-x v = <em>file</em> RET <em>oldvers</em> RET <em>newvers</em> RET</td>
<td class='confluenceTd'>Compare the specified two versions of <em>file</em></td>
</tr>
</tbody></table>
</div>
<p>Comparing versions of a file causes a window containing the buffer <b>**vc-diff**</b> to pop up. This buffer shows the differences between the two versions by displaying both the old and new text found in changed sections.</p>
<h3><a name="EmacsonAthena-SecondaryCommandsofVC"></a>Secondary Commands of VC </h3>
<p>The above commands are the ones you primarly deal with when using version control. The following commands are used less often but are still useful.</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-x v i, M-x vc-register</td>
<td class='confluenceTd'>Register the visited file for version control</td>
</tr>
<tr>
<td class='confluenceTd'>C-x v l, M-x vc-print-log</td>
<td class='confluenceTd'>Display version control state and change history</td>
</tr>
<tr>
<td class='confluenceTd'>C-x v u, M-x vc-revert-buffer</td>
<td class='confluenceTd'>Revert the buffer and the file to the last checked-in version</td>
</tr>
<tr>
<td class='confluenceTd'>C-x v c, M-x vc-cancel-version</td>
<td class='confluenceTd'>Remove the last-entered change from the master for the visited file. This undoes your last check-in.</td>
</tr>
</tbody></table>
</div>
<p>For a file to be under version control, it must be registered with the system. Typing <b>C-x v i</b> will register the file in the current buffer. Once a file is registered, it cannot be edited except through the version control system.</p>
<p>As mentioned before, version control keeps logs of changes which include version number, date and time changed, who changed it, how much was changed, and a log message entered by the user who checked in that version. <b>C-x v l</b> will open a buffer containing all the logs associated with the current file.</p>
<p><b>M-x vc-revert-buffer</b> and <b>M-x vc-cancel-version</b> are used when you make mistakes. Revert-buffer is used <em>before</em> you check in changes. If you're editing a file and decide you want the original (latest version) back (perhaps because you've badly mangled whatever was in the file), use <b>C-x v u</b>. If, however, you've just checked in a file and found a small mistake you need to correct, use <b>C-x v c</b> to undo the check-in just performed. You can then fix the mistake and check it back in, without having separate revisions for the major changes and the typo correction.</p>
<p>For more information on version control, see Chapter 18 of the <b>GNU Emacs Manual</b>.</p>
<h3><a name="EmacsonAthena-UsingShellCommandsfromWithinEmacs"></a>Using Shell Commands from Within Emacs <a name="EmacsonAthena-shellcommand"></a></h3>
<p>Emacs is an extremely versatile program that lets you run a great many other processes through it. However, there are some things that you can't do directly from an Emacs window.</p>
<p>It is possible to create a buffer within an Emacs window that behaves like a normal X window, giving you an Athena prompt and accepting many commands that you would normally type in another window. The command <b>M-x shell</b> creates this buffer. You can switch between the shell buffer and other buffers like you switch between files, with <b>C-x b</b>.</p>
<p>The shell buffer is slower than the standard command line, and there are still some programs that can't be run through it, but you can try it. This is a way to use Emacs as your "window" manager when you're logged in via dialup on a non-workstation terminal.</p>
<h3><a name="EmacsonAthena-MoreThingstoDoWithinEmacs"></a>More Things to Do Within Emacs <a name="EmacsonAthena-stuff"></a></h3>
<p>Emacs is more than just a word processor. In addition to the features described in this documentation, there are many other useful features. There are also many fun, but less-than-useful features we don't have room to describe here. Here is a brief, arbitrary listing of some of these:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>M-x mh-rmail</td>
<td class='confluenceTd'>Run mh-e, Emacs' interface to MH. For more information, see the section on Using the Emacs-based MH Mail Handler (<a href="http://web.mit.edu/olh/Email/email.html#mh" class="external-link" rel="nofollow">http://web.mit.edu/olh/Email/email.html#mh</a>) in the document Electronic Mail on Athena (<a href="http://web.mit.edu/olh/Email/email.html" class="external-link" rel="nofollow">http://web.mit.edu/olh/Email/email.html</a>).</td>
</tr>
<tr>
<td class='confluenceTd'>M-x discuss</td>
<td class='confluenceTd'>Run the Emacs interface to Discuss, Athena's electronic conferencing system. For more information, see the section Emacs Interface to Discuss (<a href="http://web.mit.edu/olh/Discuss/Discuss.html#emacs_head" class="external-link" rel="nofollow">http://web.mit.edu/olh/Discuss/Discuss.html#emacs_head</a>) in the document Discuss on Athena (<a href="http://web.mit.edu/olh/Discuss/Discuss.html" class="external-link" rel="nofollow">http://web.mit.edu/olh/Discuss/Discuss.html</a>).</td>
</tr>
<tr>
<td class='confluenceTd'>M-x gnus</td>
<td class='confluenceTd'>Run gnus, the Emacs interface to network news, after doing initial setup.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x yow</td>
<td class='confluenceTd'>Generates a randomly-chosen Zippy quote in the minibuffer.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x doctor</td>
<td class='confluenceTd'>Creates a buffer and interactively runs an "Eliza" style computer psychotherapist program.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x psychoanalyze-pinhead</td>
<td class='confluenceTd'>Runs <b>M-x doctor</b> with input from <b>M-x yow</b>.</td>
</tr>
<tr>
<td class='confluenceTd'>M-x dissociated-press</td>
<td class='confluenceTd'>Dissociate the text of the current buffer (changes the appearance on the screen, but doesn't change any text).</td>
</tr>
<tr>
<td class='confluenceTd'>M-x dunnet</td>
<td class='confluenceTd'>Text adventure game</td>
</tr>
<tr>
<td class='confluenceTd'>M-x hanoi</td>
<td class='confluenceTd'>Tower of Hanoi. Argument is number of rings.</td>
</tr>
</tbody></table>
</div>
<h2><a name="EmacsonAthena-ErrorRecovery"></a>Error Recovery <a name="EmacsonAthena-errorrecovery"></a></h2>
<h3><a name="EmacsonAthena-DealingwithSystemFailures"></a>Dealing with System Failures <a name="EmacsonAthena-failuresystem"></a></h3>
<p>When you log in to an Athena public workstation, the workstation gives you access to your files by attaching your file system to the workstation's file system. Your file system is actually on another machine on the network, your file server machine. When you edit one of your files with Emacs, you are editing the file over the network. This system has three failure points:</p>
<ul>
<li>your workstation</li>
<li>the network</li>
<li>your file server machine</li>
</ul>
<p>This section provides information about recovering your work in the event of any of these system failures.</p>
<h3><a name="EmacsonAthena-WorkstationFailure"></a>Workstation Failure <a name="EmacsonAthena-failurews"></a></h3>
<p>The workstation is the most dependable part of the system; workstations do not fail very often. Workstations fail by either "freezing" (typing at the keyboard does nothing on the screen), or by crashing – your windows disappear and the machine begins rebooting itself.</p>
<p>When the workstation fails, you have few options. Your best bet is to log out and recover your work later from an auto-save file (you must be on the same workstation you were working on before for this). See the section on <a href="#EmacsonAthena-restoringfiles">Recovering Lost Files</a> for how to do this.</p>
<h3><a name="EmacsonAthena-NetworkandFileServerFailures"></a>Network and File Server Failures <a name="EmacsonAthena-failurenetwork"></a></h3>
<p>Network and file server failures are almost indistinguishable. What happens is this: you try to write your file and the workstation begins flooding the console window with messages like the following:</p>
<p><tt>afs: Lost contact with file server 18.145.0.6 in cell athena.mit.edu</tt></p>
<p>If you have the time, wait and see if the file server comes back online. Often the failure will correct itself in five or ten minutes. When the server comes back you can write the file to disk.<br/>
If you must leave before the system comes back, you can mail the file to yourself and save it that way. Proceed as follows:</p>
<ol>
<li>Use the <b>C-x C-w</b> command to write the file. When you enter this command, Emacs prompts you to specify the name of the file you want to write:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>Write file: /mit/</tt><br/>
<tt><em>username</em>/</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
<li>Backspace over the pathname, then type <b>/usr/tmp/ <em>filename</em></b> and <b>Return</b> ( <em>filename</em> specifies the name of the file you want to save). You have now written the file to the workstation's hard disk.</li>
<li>Move to an xterm window and send yourself the file in the form of a mail message. Substitute your username for <em>username</em>, and the filename you created above for <em>filename</em>:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>mhmail</tt><br/>
<tt><em>username</em> < /usr/tmp/</tt><br/>
<tt><em>filename</em></tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
The <b>mhmail</b> command sends a copy of the file you created above to your Athena mailbox. Next time you log into Athena, you will receive the file when you incorporate your mail.</li>
</ol>
<h3><a name="EmacsonAthena-RecoveringLostFiles"></a>Recovering Lost Files <a name="EmacsonAthena-restoringfiles"></a></h3>
<p>If the auto-save version of the file you are working on is newer than the current version (this may happen if the workstation crashes and you later log in to the same workstation), Emacs displays the message:<br/>
<tt>Auto-save file is newer - consider M-x recover-file.</tt><br/>
You can recover an auto-save file by doing the following:</p>
<ol>
<li>Login to the workstation you were using at the time of the crash. Use the <b>ls</b> command to see if your file is still in the /usr/tmp directory:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>athena% <b>ls /usr/tmp</b></tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
Auto-save files have funny names. Say your username is jruser and you are editing the file /mit/jruser/paper.tex. Within /usr/tmp, the file's auto-save file has a name something like this:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>#4863.@afs@athena.mit.edu@user@j@r@jruser@paper.tex#</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
<li>Start Emacs and enter this command:
<br class="atl-forced-newline" /> <br class="atl-forced-newline" />
<tt>M-x recover-file</tt>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
<li>Emacs prompts you to enter the name of the file you want to recover. Enter the filename and press <b>Return</b>.</li>
<li>Emacs splits its window in two and asks if you want to recover the auto-save file. (Note that for long filenames, you won't be able to see the entire question.)</li>
<li>If you want to recover the file, type <b>yes</b> followed by <b>Return</b>.</li>
<li>Emacs creates a new buffer for the file, and copies the auto-save file's contents into it. The editor also informs you that it has turned off the buffer's auto-save-mode. This is done so that you won't inadvertently overwrite the auto-save file.</li>
<li>If you want to continue editing the recovered file, turn on auto-save-mode by entering <b>M-x auto-save-mode</b> followed by <b>Return.</b> Otherwise, save it and exit Emacs.</li>
</ol>
<h2><a name="EmacsonAthena-GettingHelpandMoreInformation"></a>Getting Help and More Information <a name="EmacsonAthena-moreinfo"></a></h2>
<p>Emacs is full of hundreds of features, commands, variables, and neat tricks including many not described here. To find out about Emacs features, consult the following information resources:</p>
<ol>
<li>The Emacs help command <b>C-h</b> provides extensive assistance on-line. The general form of the command and useful subcommands are listed here:
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Command</th>
<th class='confluenceTh'>Action</th>
</tr>
<tr>
<td class='confluenceTd'>C-h C-h, M-x help-for-help</td>
<td class='confluenceTd'>List the various characters that give further information about different parts of Emacs.</td>
</tr>
<tr>
<td class='confluenceTd'>C-h a, M-x command-apropos</td>
<td class='confluenceTd'>List all Emacs functions that contain a given word or string. (e.g., <b>C-h a write</b> lists all commands with "write" in their names. Emacs commands have descriptive names, so you're likely to find what you want.)</td>
</tr>
<tr>
<td class='confluenceTd'>C-h c, M-x describe-key-briefly</td>
<td class='confluenceTd'>Give name of any keystroke command.</td>
</tr>
<tr>
<td class='confluenceTd'>C-h f, M-x describe-function</td>
<td class='confluenceTd'>Describe any Emacs function.</td>
</tr>
<tr>
<td class='confluenceTd'>C-h i, M-x info</td>
<td class='confluenceTd'>Read documentation.</td>
</tr>
<tr>
<td class='confluenceTd'>C-h k, M-x describe-key</td>
<td class='confluenceTd'>Describe any keystroke command.</td>
</tr>
<tr>
<td class='confluenceTd'>C-h m, M-x describe-mode</td>
<td class='confluenceTd'>Describe the commands unique to the current mode.</td>
</tr>
<tr>
<td class='confluenceTd'>C-h v, M-x describe-variable</td>
<td class='confluenceTd'>Describe any variable.</td>
</tr>
</tbody></table>
</div>
<p> <b>Warning:</b> Do not try <b>C-h</b> until you have read <a href="#EmacsonAthena-multibuffers">Working with Multiple Buffers</a>, or the extra buffers created by help may confuse you.</p></li>
<li>There is an interactive tutorial that you can use to teach yourself Emacs. To use this tutorial, enter the <b>C-h t</b> command in Emacs, then follow directions. You can get out of the tutorial at any time by entering <b>C-x C-c</b>, just as you would to exit any normal Emacs session. If it asks you to save TUTORIAL, just say no. Decide for yourself if you want to save any other buffers it asks about.</li>
<li>The main <a href="http://www.gnu.org/software/emacs/manual/html_mono/emacs.html" class="external-link" rel="nofollow">GNU Emacs Manual</a> describes all of Emacs in detail.</li>
<li>You can view the Emacs manual page by typing <b>man emacs</b> at the Athena prompt.</li>
<li><a href="/confluence/display/category/Emacs" title="Emacs">Emacs Stock Answers in Hermes</a></li>
<li><a href="http://ist.mit.edu/support/olc/ask" class="external-link" rel="nofollow">Ask Athena Consulting a Question</a></li>
</ol>
<h2><a name="EmacsonAthena-RelatedLinks"></a>Related Links</h2>
<p><a href="http://ist.mit.edu/athena/help" class="external-link" rel="nofollow">Athena Help</a><br/>
<a href="http://ist.mit.edu/athena" class="external-link" rel="nofollow">Athena at MIT</a></p>
</div>
<!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="http://kb.mit.edu/confluence/display/istcontrib/Emacs+on+Athena"
dc:identifier="http://kb.mit.edu/confluence/display/istcontrib/Emacs+on+Athena"
dc:title="Emacs on Athena"
trackback:ping="http://kb.mit.edu/confluence/rpc/trackback/15139848"/>
</rdf:RDF>
-->
</div>
<script type="text/x-template" title="manage-watchers-dialog">
<div class="dialog-content">
<div class="column page-watchers">
<h3>Watching this page</h3>
<p class="description">These people are notified when the page is changed. You can add or remove people from this list.</p>
<form action="/confluence/json/addwatch.action" method="POST">
<input type="hidden" name="atl_token" value="b7367e21a4f2023a69e89a2e1df84bb8cbe0a8c6">
<input type="hidden" name="pageId" value="15139848"/>
<input type="hidden" id="add-watcher-username" name="username" value=""/>
<label for="add-watcher-user">User</label>
<input id="add-watcher-user" name="userFullName" type="search" class="autocomplete-user"
value="" placeholder="Full name or username" autocomplete="off"
data-max="10" data-target="#add-watcher-username" data-dropdown-target="#add-watcher-dropdown"
data-template="{title}" data-none-message="No matching users found.">
<input id="add-watcher-submit" type="submit" name="add" value="Add">
<div id="add-watcher-dropdown" class="aui-dd-parent autocomplete"></div>
<div class="status hidden"></div>
</form>
<ul class="user-list">
<li class="loading">Loading…</li>
<li class="no-users hidden">No page watchers</li>
</ul>
</div>
<div class="column space-watchers">
<h3>Watching this space</h3>
<p class="description">These people are notified when any content in the space is changed. You cannot modify this list.</p>
<ul class="user-list">
<li class="loading">Loading…</li>
<li class="no-users hidden">No space watchers</li>
</ul>
</div>
</div>
</script>
<script type="text/x-template" title="manage-watchers-user">
<li class="watch-user">
<img class="profile-picture confluence-userlink" src="{iconUrl}" data-username="{username}">
<a class="confluence-userlink" href="{url}" data-username="{username}">{fullName} <span class="username">({username})</span></a>
<span class="remove-watch" title="Remove"
data-username="{username}">Remove</span>
</li>
</script>
<script type="text/x-template" title="manage-watchers-help-link">
<div class="dialog-help-link">
<a href="http://docs.atlassian.com/confluence/docs-35/Managing+Watchers" target="_blank">Help</a>
</div>
</script>
<!--END page.vmd-->