[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.

Welcome to the Slackware Documentation Project

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
slackbook:emacs [2012/09/12 21:01 (UTC)] – [Sources] sycamorexslackbook:emacs [2012/09/16 16:52 (UTC)] (current) – [Sources] updated authors and tags mfillpot
Line 1: Line 1:
 ====== Emacs ====== ====== Emacs ======
 +===== What is Emacs? =====
  
-While [[slackbook:vi|vi]] (with its clones) is without a doubt the most ubiquitous editor on Unix-like systems, emacs(1) comes in a good secondInstead of using different "modes"like vi does, it uses <key>Ctrl</key> and <key>Alt</key> key combinations to enter commands, in much the same way that you can use <key>Ctrl</key> and <key>Alt</key> key combinations in word processor and indeed in many other applications to execute certain functions. (Though it should be noted that the commands rarely correspond; so while many modern applications use <key>Ctrl+C/X/V</key> for copyingcutting and pastingemacs uses different keys and actually somewhat different mechanism for this.)+**//vi//** and its clones are very functional 
 +and powerful editors Howeverthey are often considered not 
 +particularly extensible.  **//vim//** is a 
 +successful and powerful 
 +**//vi//** variant that shrugs this trend, 
 +being both extremely extensible and lightweight.  Howevermany 
 +users prefer more //"heavy"// and extensible editor This is why 
 +many people (including the author of this chapterprefer 
 +**//Emacs//**.
  
-Also unlike vi, which is an (excellent) editor and nothing more, emacs is a program with near endless capabilities. emacs is (for the most part) written in Lisp, which is a very powerful programming language that has the peculiar property that every program written in it is automatically a Lisp compiler of its own. This means that the user can extend emacs, and in fact write completely new programs "in emacs". 
  
-As resultemacs is not just an editor anymore. There are many add-on packages for emacs available (many come with the program's sourcethat provide all sorts of functionality. Many of these are related to text editingwhich is after all emacs' basic taskbut it doesn't stop thereThere are for example several spreadsheet programs for emacs, there are databases, games, mail and news clients (the top one being Gnus), etc.+Emacs takes extensibility up to eleven.  Outside of core of C, 
 +the rest of **//Emacs//** is written in a 
 +Lisp variant, nearly all of which is exposed to you, so that you 
 +may configure it or even extend it at will (many good Emacs Lisp 
 +tutorials can be found on the Internet).  People have written 
 +all sorts of extensions in Emacs Lispfrom syntax highlighting 
 +for an obscure languageto a built-in terminal In fact, 
 +there's even a **//vi//** emulation mode 
 +within **//Emacs//** (called viper), so you 
 +can still get the modal editing that comes with vi, while having 
 +access to the power of the **//Emacs//** 
 +core.
  
-There are two main versions of emacs: GNU Emacs (which is the version that comes with Slackware) and XEmacs. The latter is not a version for Emacs running under X. In fact, both emacs and XEmacs run on the console as well as under X. XEmacs was once started as a project to tidy up the emacs code. Currently, both versions are being actively developed, and there is in fact much interaction between the two development teams. For the present chapter, it is immaterial whether you use emacs or XEmacs, the differences between them are not relevant to the normal user. 
  
-===== Starting Emacs =====+Like **//vi//**, there are many variants of 
 +**//Emacs//** (termed //"emacsen"//).  However, 
 +the one most commonly used (and the only one in Slackware) is 
 +GNU Emacs.  When people reference //"Emacs"// directly, they almost 
 +always mean GNU Emacs.
  
-Emacs can be started from the shell by simply typing emacs. When you are running X, emacs will (normally) come up with its own X window, usually with a menu bar at the top, where you can find the most important functions. On startup, emacs will first show a welcome message, and then after a few seconds will drop you in the *scratch* buffer. (See Section 17.2.) 
  
-You can also start emacs on an existing file by typing+Unlike **//vi//**, 
 +**//Emacs//** operates more like a 
 +traditional editor by default.  This means that most keyboard 
 +shortcuts can be performed without repeatedly changing modes. 
 +You can open up a file and start typing away without having to 
 +learn what the modes do, or forgetting which one you are 
 +currently using. 
 +===== Starting Out =====
  
-  %  emacs +**//Emacs//** can be started simply by 
-  /etc/resolv.conf +running the **//emacs//** command in your 
-  +terminal.  When 
 +you first start it in a console without arguments, you will see 
 +something that resembles this:
  
-This will cause emacs to load the specified file when it starts up, skipping the welcome message.+     
 +[[emacs-startup.png|Starting Out]]
  
-**Command Keys**+If you are in X windows, **//Emacs//** may 
 +start a GUI instead of running in your console.  If this is the 
 +case and you don't want a GUI, you can invoke it with the flag 
 +//'-nw'//.
  
-As mentioned above, emacs uses <key>Ctrl</key> and <key>Alt</key> combinations for commands. The usual convention is to write these with <key>C</key>-letter and <key>M</key>-letter, respectively. So <key>C-x</key> means <key>Ctrl+x</key>, and <key>M-x</key> means <key>Alt+x</key>. 
  
-<note>The letter M is used instead of A because originally the key was not the <key>Alt</key> key but the Meta key. The Meta key has all but disappeared from computer keyboards, and in emacs the <key>Alt</key> key has taken over its function+While here, you can browse around using the 
-</note>+keyboard arrow keys. 
 +Underlined elements are links, and you can activate them by 
 +moving over them and pressing <key>'Enter'</key>.  The documentation 
 +mentioned is very good, and can help you get your bearings 
 +should you have any problems.  Also note how they describe key 
 +sequences such as <key>C-h</key>, meaning press the 
 +<key>'h'</key> 
 +key while holding down the <key>'CTRL'</key> key.  Same deal 
 +with **M-`**, meaning to hold the the 
 +**Meta** key (usually <key>'Alt'</key>and 
 +press the backtick <key>'`'</key> key.  When they say (e.g.)  <key>C-x</key> <key>C-c</key>, this means to press the <key>'x'</key> key while holding down 
 +the <key>'CTRL'</keykey, then press the <key>'x'</key> key while also 
 +holding down the <key>'CTRL'</key> key.  Conveniently, this 
 +is also one of the more important commands in 
 +**//Emacs//**: to close it.
  
-Many emacs commands consist of sequences of keys and key combinations. For example, <key>C-x</key> <key>C-c</key> (that is <key>Ctrl+x</key> followed by <key>Ctrl+c</key>) quits Emacs, <key>C-x</key> <key>C-s</key> saves the current file. Keep in mind that <key>C-x</key> <key>C-b</key> is not the same as <key>C-x b</key>. The former means <key>Ctrl+x</key> followed by <key>Ctrl+b</key>, while the latter means <key>Ctrl+x</key> followed by just <key>b</key> 
  
-===== Buffers =====+Alternatively, if you call **//emacs//** with a 
 +file name as an argument, it will open that file, just like 
 +**//vi//**.  Upon doing this, you will be 
 +presented with the contents of the file in question.  Here, you can 
 +navigate the document using traditional arrow keys and type in 
 +information at will without any issues.
  
-In emacs, the concept of 'buffers' is essential. Every file that you open is loaded into its own buffer. Furthermore, emacs has several special buffers, which do not contain a file but are used for other things. Such special buffers usually have a name that starts and ends with an asterisk. For example, the buffer that emacs shows when it is first started, is the so-called *scratch* buffer. In the *scratch* buffer, you can type text in the normal way, but text that is typed there is not saved when emacs is closed. 
  
-There is one other special buffer you need to know about, and that is the minibuffer. This buffer consists of only one lineand is always on the screen: it is the very last line of the emacs window, below the status bar for the current bufferThe minibuffer is where emacs shows messages for the userand it is also the place where commands that require some user input are executedFor example, when you open a file, emacs will ask for its name in the minibuffer.+Say you make some edits, and you now want to save your file. 
 +The following key sequence will do that: <key>C-x</key><key>C-s</key>
 +If you made a new file, you will be prompted for this in what is 
 +called the //"minibuffer"//, the blank line below the gray line at 
 +the bottom of the screen Type in the file name of your choice, 
 +then hit <key>'Enter'</key>If you don't want to 
 +save the file, you can press <key>C-g</key>, which aborts 
 +operations that ask for input.  Do note that tab-completion is 
 +usually available for operations that use the minibuffer.
  
-Switching from one buffer to another can be done with the command <key>C-x b</key>. This will prompt you for the name of a buffer (a buffer's name is usually the name of the file you are editing in it), and it gives a default choice, which is normally the buffer that you were in before you switched to or created the current buffer. Just hitting Enter will switch to that default buffer. 
  
-If you want to switch to another buffer than the default offered by Emacs, just type its name. Note that you can use so-called Tab-completion here: type the first few letters of the buffer's name and hit Tab; emacs will then complete the name of the buffer. Tab completion works everywhere in emacs where it makes sense.+Should you want to open a new file within your same 
 +**//Emacs//** session, type in <key>C-x</key><key>C-f</key> You will be prompted for a file name in the 
 +minibuffer **//Emacs//** doesn't care 
 +whether it exists or not.  If it doesn't exist, a new buffer 
 +will be created for it (the file will be created upon saving 
 +with <key>C-x</key><key>C-s</key>), or it will be opened as 
 +expected.  However, the old file will still be open!  You can 
 +switch back to it using <key>C-x</key><key>C-b</key>, entering in 
 +the file's name (or more technically, the buffer's name), then 
 +hitting <key>'Enter'</key>. 
 +===== How to Move Around =====
  
-You can get a list of open buffers by hitting <key>C-x</key> <key>C-b</key>. This command will usually split the screen in twodisplaying the buffer you were working in in the top half, and a new buffer called *Buffer Listin the bottom halfThis buffer contains a list of all the bufferstheir sizes and modesand the files, if any, that those buffers are visiting (as it is called in emacs). You can get rid of this split screen by typing <key>C-x 1</key>+Like **//vi//**, 
 +**//Emacs//** is also older than the arrow 
 +keys on your keyboard Alsolike in 
 +**//vi//**using the arrow keys to navigate 
 +files is also supported.  While the 
 +**//vi//** movement keys are more ergonomic, 
 +**//emacs//**'are more //"mnemonic"//
 +However, it is still very possible to operate using the main 
 +**//Emacs//** keybindings quickly and 
 +efficiently Here is a table of the basic movement keybindings:
  
-===== Modes =====+     
 +**Emacs Cursor Movement** 
 +^Command^Result| 
 +|<key>C-f</key>|Move the cursor one character to the right (forward)| 
 +|<key>C-b</key>|Move the cursor one character to the left (backward)| 
 +|<key>C-n</key>|Move the cursor one line down (next)| 
 +|<key>C-p</key>|Move the cursor one line up (previous)|
  
-Every buffer in emacs has an associated mode. This mode is very different from the idea of modes in vi: mode tells you what kind of buffer you are in. For example, there is text-mode for normal text files, but there are also modes such as c-mode for editing programs, sh-mode for editing shell scriptslatex-mode for editing LaTeX files, mail-mode for editing email and news messagesetcA mode provides special customizations and functionality that is useful for the kind of file you are editing. It is even possible for a mode to redefine keys and key commands. For example, in Text mode, the Tab key simply jumps to the next tab stop, but in many programming language modes, the Tab key indents the current line according to the depth of the block that line is in.+Of course, like with **//vi//** it is also 
 +possible to repeat these commands with numeric argument.  If you 
 +type in **M-1** **M-0** <key>C-p</key>or <key>C-u</key> 10 
 +<key>C-p</key>the cursor will move ten lines up If you type in 
 +**M-5** <key>C-f</key> or <key>C-u</key> 5 <key>C-f</key>, the cursor 
 +will move five characters to the right. 
 +===== Getting Help =====
  
-The modes mentioned above are called //major// modes. Each buffer has exactly one major mode. Additionally, a buffer can have one or more //minor// modesA minor mode provides additional features that may be useful for certain editing tasks. For example, if you hit the <key>Insert</key> key, you invoke overwrite-mode, which does what you'd expect. There is also an auto-fill-mode, which is handy in combination with text-mode or latex-mode: it causes each line that you type to be automatically wrapped once the line reaches a certain number of characters. Without auto-fill-mode, you have to type <key>M-q</key> to fill out a paragraph. (Which you can also use to reformat a paragraph after you've edited some text in it and it is no longer nicely filled out.)+**//Emacs//** contains a great deal of 
 +documentationto the point that it is often called a 
 +//"self-documenting"// editorThis is because it provides 
 +mechanisms for providing users with documentation while you are 
 +using it.
  
-==== Opening Files ====+Here are some useful functions that display documentation (they 
 +all start with <key>C-h</key>):
  
-To open a file in emacs, type <key>C-x</key> <key>C-f</key> 
  
-Emacs will ask you for the name of the file, filling in some default path for you (which is usually ~). After you type the filename (you can use Tab completion) and hit ENTER, emacs will open the file in a new buffer and display that buffer on the screen.+**Accessing Emacs Documentation** 
 +^Command^Result| 
 +|<key>C-h</key> f FUNCTION-NAME <key>Enter</key>|Show documentation for function FUNCTION-NAME| 
 +|<key>C-h</key> k <key>C-x</key> <key>C-c</key>|Show documentation for the function bound to the keys<key>C-x</key> <key>C-c</key>
 +|<key>C-h</key> t|Show the Emacs tutorial| 
 +|<key>C-h</key> ?|Show all help-related functions|
  
-<note>Emacs will automatically create a new buffer, it will not load the file into the current buffer.</note>+     
 +<key>C-h</key**t** is especially useful if you want or need practice using **//Emacs//**. 
 +===== Calling Functions =====
  
-In order to create a new file in emacsyou cannot just go typing right away. You first have to create buffer for it, and come up with a filenameYou do this by typing <key>C-x</key> <key>C-f</key> and typing a filenamejust as if you were opening an existing fileEmacs will notice that the file you typed doesn't exist, and will create a new buffer and report "(New file)in the minibuffer.+As noted earlier**//Emacs//** exports a 
 +large number of functions to for interactive use.  Some of 
 +theselike those opening and saving files, are mapped to keys. 
 +Others (like the ones for moving to the beginning and end of 
 +lines) are not.  To call them, you have to invoke them Say we 
 +want to call the function //"end-of-line"// We would do 
 +this:
  
-When you type <key>C-x</key> <key>C-f</key> and then enter a directory name instead of a filename, emacs will create a new buffer in which you will find a list of all the files in that directory. You can move the cursor to the file that you are looking for and type , and emacs will open it. 
  
-<note>There are in fact a lot more actions you can perform here, such as deleting, renaming and moving files, and so on. This is the dired-mode of emacs, which is basically a simple file manager.</note>+**M-x** end-of-line <key>Enter</key>
  
-When you have typed <key>C-x</key> <key>C-f</key> and suddenly change your mind, you can type <key>C-g</key> to cancel the action. <key>C-g</key> works almost everywhere where you want to cancel an action or command that you've started but don't want to finish.  
  
-===== Basic Editing =====+And the cursor would move to the end of the line, as the function name 
 +suggests. 
 +===== Emacs Cheat Sheet =====
  
-When you have opened a fileyou can of course move around in it with the cursorThe cursor keys and <key>PgUp</key><key>PgDn</key> do what you'd expect. <key>Home</key> and <key>End</key> jump to the beginning and end of the line. (In older versions, they would actually jump to the beginning and end of the buffer.) However, there are also <key>Ctrl</key> and Meta (<key>Alt</key>) key combos that move the cursor around. Because you do not need to move your hands to another part of the keyboard for these, they are much quicker once you get used to them.+While Emacs can be simple to useits scope can easily be 
 +overwhelming.  Below are some useful Emacs commands Some 
 +aspects have been simplified, most notably regarding text 
 +selection.  These concepts, and moreare described the 
 +**//Emacs//** manual, and various on-line 
 +tutorials Decent summaries can also be gleaned from web 
 +searches.
  
-Basic emacs Editing Commands 
-^ Command ^ Result ^ 
-|<key>C-b</key>|go one character back| 
-|<key>C-f</key>|go one character forward| 
-|<key>C-n</key>|go one line down| 
-|<key>C-p</key>|go one line up| 
-|<key>C-a</key>|go to the beginning of the line| 
-|<key>C-e</key>|go to the end of the line| 
-|<key>M-b</key>|go one word back| 
-|<key>M-f</key>|go one word forward| 
-|<key>M-}</key>|go one paragraph forward| 
-|<key>M-{</key>|go one paragraph backward| 
-|<key>M-a</key>|go one sentence backward| 
-|<key>M-e</key>|go one sentence forward| 
-|<key>C-d</key>|delete the character under the cursor| 
-|<key>M-d</key>|delete until the end of the current word| 
-|<key>C-v</key>|go down one screen (i.e., **PgDn**)| 
-|<key>M-v</key>|go up one screen (i.e., **PgUp**)| 
-|<key>M-< </key>|go to the beginning of the buffer| 
-|<key>M- ></key>|go to the end of the buffer| 
-|<key>C-_</key>|undo the last change (can be repeated); note that you actually have to type SHIFT+CTRL+hyphen for this.| 
-|<key>C-k</key>|delete to end of line| 
-|<key>C-s</key>|forward search| 
-|<key>C-r</key>|reverse search| 
  
-Note that many Meta commands are parallel to the <key>Ctrl</key> commands except that they operate on larger units: while <key>C-f</key> goes forward one character, <key>M-f</key> goes forward an entire word, etc. +**Emacs Cheat Sheet** 
- +^Command^Result| 
-Also note that <key>M-< </key> and <key>M- ></key> require you to type <key>Shift+ALT+,</key> and <key>Shift+Alt+.</key> respectively, since <key>< </key> and <key> ></key> are on <key>Shift+,</key> and <key>Shift+.</key>(Unless of course you have a different keyboard layout from the standard US layout.) +|<key>C-f</key> |Move the cursor one character to the right (forward)| 
- +|<key>C-b</key> |Move the cursor one character to the left (backward)| 
-Note that <key>C-k</key> deletes (kills, as it is commonly calledall the text after the cursor to the end of the line, but doesn't delete the line itself (i.e., it doesn't delete the final newline). It only deletes the line if there was no text after the cursor. In other words, in order to delete a complete line, you have to put the cursor at the beginning of the line, and then hit <key>C-k</key> twice: once to delete the text on the lineand once to delete the line itself.  +|<key>C-n</key> |Move the cursor one line down (next)| 
- +|<key>C-p</key> |Move the cursor one line up (previous)| 
-===== Saving Files ===== +|<key>C-h</key> <key>'f'</key> FUNCTION-NAME <key>Enter</key> |Show documentation for function FUNCTION-NAME| 
- +|<key>C-h</key> <key>'k'</key> <key>C-x</key<key>C-c</key> |Show documentation for the function bound to the keys <key>C-x</key> <key>C-c</key>
-In order to save a file, you type <key>C-x</key> <key>C-s</key> +|<key>C-h</key> <key>t</key> |Show the Emacs tutorial| 
- +|<key>C-h</key<key>?</key> |Show all help-related functions| 
-Emacs will not ask you for a filename, the buffer will just be saved to the file it was loaded from. If you want to save your text to another filetype <key>C-x</key> <key>C-w</key> +|M-<key>'`'</key> |Access the Menu Bar| 
- +|<key>C-g</key> |Cancel the current operationThis is most useful when in the minibuffer.| 
-When you save the file for the first time in this session, emacs will normally save the old version of your file to a backup file, which has the same name appended with a tilde: so if you're editing a file cars.txtemacs will create a backup cars.txt~+|M-<key>x</key> FUNCTION-NAME <key>Enter</key> |Call the interactive function FUNCTION-NAME| 
- +|M-<key>1</key> M-<key>0</key> <key>C-n</key> |Move the cursor ten lines down| 
-This backup file is a copy of the file that you openedWhile you are working, emacs will also regularly create an auto-save copy of the work you are doing, to a file named with hash signs: #cars.txt#. This backup is deleted when you save the file with <key>C-x</key> <key>C-s</key>+|<key>C-u</key> <key>1</key><key>0</key> <key>C-n</key> |Move the cursor ten lines down (same as above)
- +|M-<key>'x'</key> beginning-of-line |Move the cursor to the beginning of the current line
-When you are done editing a file, you can kill the buffer that holds it by typing <key>C-x k</key> +|M-<key>'x'</key> end-of-line |Move the cursor to the end of the current line
- +|M-<key>'>'</key> |Move the cursor to the end of the buffer| 
-Emacs will then ask you which buffer you want to kill, with the current buffer as default, which you can select by hitting <key>Enter</key>. If you haven't saved your file yetemacs will ask you if you really want to kill the buffer. +|M-<key>'<'</key> |Move the cursor to the beginning of the buffer| 
- +|<key>C-k</key> |Remove text from the cursor to the end of the line and place it into the kill ring| 
-If you'd prefer to save the file with a new name so that you don't overwrite the original version, use <key>C-x</key> <key>C-w</key> instead. You may then give a new name for the file, and the buffer will be updated to reflect the contents of this new file, leaving the old file unchanged. +|<key>C-space</key> |Enter selection mode (use normal motion keys to move around). Press C-space again to leave it.| 
- +|<key>C-w</key> |While in selection modedelete the selected regionand store the result into the kill ring| 
-===== Quitting Emacs ===== +|M-<key>w</key> |While in selection mode, store the selected region into the kill ring.| 
- +|C-<key>y</key> |"Yanks" the contents of the kill ring and places them at the cursor's location| 
-When you are done with emacs altogether, you can type <key>C-x</key> <key>C-c</key> +|<key>C-/</key> |Undo the previous actionUnlike most other editorsthis includes previous undo actions.| 
- +|<key>insert</key> |Enable or disable overwriting characters| 
-This quits emacsIf you have any unsaved files, emacs will tell you so, and ask if you want to save them each in turn. If you answer no to any of these, emacs will ask for one final confirmation and then quit+|<key>C-s</key> asdf <key>Enter</key> |Forward incremental search for the string "asdf"Repeat <key>C-s</key> as needed to search for future itemsor <key>C-r</key> (below) to search backwards.
 +|<key>C-r</key> asdf <key>Enter</key> |Backward incremental search for the string "asdf". Repeat <key>C-r</key> as needed to search for future items, or <key>C-s</key> (above) to search forwards.| 
 +|M-<key>'%'</key> old <key>Enter</key> new <key>Enter</key> |Search for each instance of "old" and prompt you to replace it with "new". You can force replacement of all items by typing <key>'!'</key> at the replacement prompt.| 
 +|<key>C-x</key> <key>C-c</key> |Exit Emacsprompting you to save each unsaved buffer before doing so| 
 +|<key>C-x</key> <key>C-s</key> |Save the currrent buffer to its file| 
 +|<key>C-x</key> <key>C-w</key> new-file.txt <key>Enter</key> |Save the current buffer to a file //"new-file.txt"//|
  
 ====== Chapter Navigation ====== ====== Chapter Navigation ======
Line 126: Line 218:
  
 **Next Chapter: [[slackbook:network|Networking]]** **Next Chapter: [[slackbook:network|Networking]]**
-=====Sources===== +====== Sources =====
-  * Original source: http://slackbook.org/beta/#ch_emacs (authors: Alan Hicks, Chris Lumens, David Cantrell, Logan Johnson)+<!-- If you copy information from another source, then specify that source --> 
 +  * Original source: [[http://www.slackbook.org/beta]] \\ 
 +<!-- Authors are allowed to give credit to themselves! --> 
 +  * Originally written by Alan Hicks, Chris Lumens, David Cantrell, Logan Johnson 
 +<!-- * Contrbutions by [[wiki:user:yyy | User Y]] --> 
 + 
 +<!-- Please do not modify anything below, except adding new tags.--> 
 +<!-- You must also remove the tag-word "template" below. Otherwise your page will not show up in the Table of Contents --> 
 +{{tag>slackbook emacs text_editor}}
 slackbook:emacs ()