[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
Next revisionBoth sides next revision
slackbook:emacs [2012/08/22 02:33 (UTC)] vharishankarslackbook:emacs [2012/09/16 16:50 (UTC)] – [Emacs Cheat Sheet] **Emacs Cheat Sheet** ^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)| |< 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. 
  
 +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.
 +
 +
 +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//** can be started simply by
 +running the **//emacs//** command in your
 +terminal.  When
 +you first start it in a console without arguments, you will see
 +something that resembles this:
 +
 +    
 +[[emacs-startup.png|Starting Out]]
 +
 +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'//.
 +
 +
 +While here, you can browse around using the
 +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'</key> key, 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.
 +
 +
 +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.
 +
 +
 +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.
 +
 +
 +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 =====
 +
 +Like **//vi//**,
 +**//Emacs//** is also older than the arrow
 +keys on your keyboard.  Also, like in
 +**//vi//**, using the arrow keys to navigate
 +files is also supported.  While the
 +**//vi//** movement keys are more ergonomic,
 +**//emacs//**'s 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:
 +
 +    
 +**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)|
 +
 +Of course, like with **//vi//** it is also
 +possible to repeat these commands with a 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 =====
 +
 +**//Emacs//** contains a great deal of
 +documentation, to the point that it is often called a
 +//"self-documenting"// editor. This is because it provides
 +mechanisms for providing users with documentation while you are
 +using it.
 +
 +Here are some useful functions that display documentation (they
 +all start with <key>C-h</key>):
 +
 +
 +**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|
 +
 +    
 +<key>C-h</key> **t** is especially useful if you want or need practice using **//Emacs//**.
 +===== Calling Functions =====
 +
 +As noted earlier, **//Emacs//** exports a
 +large number of functions to for interactive use.  Some of
 +these, like 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:
 +
 +
 +**M-x** end-of-line <key>Enter</key>
 +
 +
 +And the cursor would move to the end of the line, as the function name
 +suggests.
 +===== Emacs Cheat Sheet =====
 +
 +While Emacs can be simple to use, its scope can easily be
 +overwhelming.  Below are some useful Emacs commands.  Some
 +aspects have been simplified, most notably regarding text
 +selection.  These concepts, and more, are described the
 +**//Emacs//** manual, and various on-line
 +tutorials.  Decent summaries can also be gleaned from web
 +searches.
 +
 +
 +**Emacs Cheat Sheet**
 +^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)|
 +|<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>|
 +|<key>C-h</key> <key>t</key> |Show the Emacs tutorial|
 +|<key>C-h</key> <key>?</key> |Show all help-related functions|
 +|M-<key>'`'</key> |Access the Menu Bar|
 +|<key>C-g</key> |Cancel the current operation. This is most useful when in the minibuffer.|
 +|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|
 +|<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|
 +|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|
 +|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|
 +|<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 mode, delete the selected region, and store the result into the kill ring|
 +|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|
 +|<key>C-/</key> |Undo the previous action. Unlike most other editors, this includes previous undo actions.|
 +|<key>insert</key> |Enable or disable overwriting characters|
 +|<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 items, or <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 Emacs, prompting 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"//|
 ===== Starting Emacs ===== ===== Starting Emacs =====
  
Line 121: Line 324:
 This quits emacs. If 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.  This quits emacs. If 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. 
  
-=====Sources===== +====== Chapter Navigation ====== 
-  + 
 +**Previous Chapter: [[slackbook:vi|vi]]** 
 + 
 +**Next Chapter: [[slackbook:network|Networking]]** 
 +======Sources======
   * Original source: http://slackbook.org/beta/#ch_emacs (authors: Alan Hicks, Chris Lumens, David Cantrell, Logan Johnson)   * Original source: http://slackbook.org/beta/#ch_emacs (authors: Alan Hicks, Chris Lumens, David Cantrell, Logan Johnson)
 slackbook:emacs ()