User Tools

Site Tools


emacs_tutorial

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
emacs_tutorial [2024/09/02 09:30] – w”) hc9emacs_tutorial [2024/09/02 22:15] (current) – ' hc9
Line 5: Line 5:
 Emacs is an extensible, customizable, full-screen text editor. You can learn enough to be productive in Emacs in 30 minutes, but there is enough to learn about Emacs to keep you busy for years. The goal of this tutorial is to show you enough so that you are comfortable using Emacs as a work environment, not just as a text editor. This document refers to GNU Emacs version 24 (the version installed on SDF). Emacs is an extensible, customizable, full-screen text editor. You can learn enough to be productive in Emacs in 30 minutes, but there is enough to learn about Emacs to keep you busy for years. The goal of this tutorial is to show you enough so that you are comfortable using Emacs as a work environment, not just as a text editor. This document refers to GNU Emacs version 24 (the version installed on SDF).
  
-Start by typing ''emacs'' at the shell prompt, read the splash screen and hit any key to clear it. Youll be in a so-called //scratch buffer//. A //buffer// is a place in Emacs that usually contains the text of a file for editing (but can contain other text, like error messages, command output or directory contents). Anyway, the scratch buffer is just that – a place to scratch notes or otherwise put temporary snippets of text you dont care to save. When you exit Emacs, the contents of the scratch buffer are discarded.+Start by typing ''emacs'' at the shell prompt, read the splash screen and hit any key to clear it. You'll be in a so-called //scratch buffer//. A //buffer// is a place in Emacs that usually contains the text of a file for editing (but can contain other text, like error messages, command output or directory contents). Anyway, the scratch buffer is just that – a place to scratch notes or otherwise put temporary snippets of text you don't care to save. When you exit Emacs, the contents of the scratch buffer are discarded.
  
 You will see a white or grey line at the bottom of the Emacs window, with some information displayed like the name of the buffer or file you are viewing, the line number your cursor is on, and whether or not the current buffer has been modified (indicated by two asterisks to the left of the buffer name). This is called the //modeline//. Like everything else in Emacs, what is displayed in the modeline is configurable, but the defaults should work fine for most people. You will see a white or grey line at the bottom of the Emacs window, with some information displayed like the name of the buffer or file you are viewing, the line number your cursor is on, and whether or not the current buffer has been modified (indicated by two asterisks to the left of the buffer name). This is called the //modeline//. Like everything else in Emacs, what is displayed in the modeline is configurable, but the defaults should work fine for most people.
Line 13: Line 13:
 ===== Basic Commands ===== ===== Basic Commands =====
  
-Emacs is a non-modal editor, meaning when you have a file loaded into a buffer, you can type text and it will appear directly in the buffer. You dont need to be in a special insert mode as in vi. That also means cursor movement and other text manipulation commands are not mapped to single alphabetic characters as in vi. Commands in Emacs are typically entered with a two- or three-key sequence, either Control- or Alt- followed by a one- or two-key sequence. The Control key in the Emacs documentation is denoted by “C” and Alt by “M” (Emacs documentation refers to the Alt key as Meta, hence the “M". The Esc key on most keyboards acts as an Alt or Meta key as well). For example, to quit Emacs, you type ''C-x C-c'', meaning you hold down the Control key, hit the “x” key and release it, then hit the “c” key and release it. To move up a screenfull of text, you type ''M-v'', meaning hold down the Alt key and type “v". With that in mind, here are a few useful key sequences:+Emacs is a non-modal editor, meaning when you have a file loaded into a buffer, you can type text and it will appear directly in the buffer. You don't need to be in a special insert mode as in vi. That also means cursor movement and other text manipulation commands are not mapped to single alphabetic characters as in vi. Commands in Emacs are typically entered with a two- or three-key sequence, either Control- or Alt- followed by a one- or two-key sequence. The Control key in the Emacs documentation is denoted by “C” and Alt by “M” (Emacs documentation refers to the Alt key as Meta, hence the “M". The Esc key on most keyboards acts as an Alt or Meta key as well). For example, to quit Emacs, you type ''C-x C-c'', meaning you hold down the Control key, hit the “x” key and release it, then hit the “c” key and release it. To move up a screenfull of text, you type ''M-v'', meaning hold down the Alt key and type “v". With that in mind, here are a few useful key sequences:
  
 | C-x C-f | Find file and load into buffer (Emacs prompts for filename) | | C-x C-f | Find file and load into buffer (Emacs prompts for filename) |
Line 47: Line 47:
 ===== Extended Commands and Command Completion ===== ===== Extended Commands and Command Completion =====
  
-Emacs has the concept of //extended commands//, which consist of ''M-x'' followed by the name of the command. As an example of a useful extended command, sometimes the backspace key gets mapped to C-h (I find this happens sometimes under remote screen sessions, youll know this is the case if you hit the backspace key twice and the emacs help buffer pops up, just hit ''C-g'' to clear the help window). To get the backspace key to act as it should (i.e., it deletes the previous character), you enter ''M-x normal-erase-is-backspace-mode''. That's a lot to type, so you can take advantage of Emacs' //command-completion// by typing a partial command and hitting the “Tab” key one or more times. In this case, you type ''M-x normal-e'' and hit ''Tab'', and Emacs will complete the command for you. You can then just hit ''Enter'' to execute the command. If you dont type enough of the command to make it unique, Emacs will display a list of alternatives for you to choose from. For example, if you type ''M-x normal-'' and hit the ''Tab'' key, Emacs will pop up a buffer named *Completion* with with two alternatives for you to choose from: normal-mode and mormal-erase-is-backspace-mode. Just type a bit more of the command you want and hit tab again to narrow the completion list or to complete the command if it is the only one left.+Emacs has the concept of //extended commands//, which consist of ''M-x'' followed by the name of the command. As an example of a useful extended command, sometimes the backspace key gets mapped to C-h (I find this happens sometimes under remote screen sessions, you'll know this is the case if you hit the backspace key twice and the emacs help buffer pops up, just hit ''C-g'' to clear the help window). To get the backspace key to act as it should (i.e., it deletes the previous character), you enter ''M-x normal-erase-is-backspace-mode''. That's a lot to type, so you can take advantage of Emacs' //command-completion// by typing a partial command and hitting the “Tab” key one or more times. In this case, you type ''M-x normal-e'' and hit ''Tab'', and Emacs will complete the command for you. You can then just hit ''Enter'' to execute the command. If you don't type enough of the command to make it unique, Emacs will display a list of alternatives for you to choose from. For example, if you type ''M-x normal-'' and hit the ''Tab'' key, Emacs will pop up a buffer named *Completion* with with two alternatives for you to choose from: normal-mode and mormal-erase-is-backspace-mode. Just type a bit more of the command you want and hit tab again to narrow the completion list or to complete the command if it is the only one left.
  
 ===== Kill & Yank (Cut/Copy and Paste) ===== ===== Kill & Yank (Cut/Copy and Paste) =====
  
-Most Emacs commands operate on the region defined by the //point//, which is the location of the cursor at any given time, and the //mark//, which is set with the command ''C-space''. To copy or cut a region of text, move the cursor to the start of the text area you are interested in and type ''C-space''. You will see “Mark set” in the echo area. Now move the cursor to the end of the text region (which moves the point) and type ''M-w'' for copy or ''C-w'' for cut (both called killing text). The text you copy or cut is stored by Emacs in the //kill-ring//, which is a circular buffer that stores the text snippets you kill in a last-in-first-out order. To paste the most recently stored text, move the cursor to where the text should be inserted, and type ''C-y''. The “y” stands for yank, what youll see paste referred to in the Emacs help documentation (remember Emacs pre-dated modern windowing systems and other full-screen editors, so the terms cut and paste were not in use yet). After a yank (''C-y'') command, you can replace the inserted text with earlier kills in turn by typing ''M-y'' one or more times. Each time you press ''M-y'' the next block of killed text is popped off of the kill ring and inserted into your buffer, replacing the last insert at the same time.+Most Emacs commands operate on the region defined by the //point//, which is the location of the cursor at any given time, and the //mark//, which is set with the command ''C-space''. To copy or cut a region of text, move the cursor to the start of the text area you are interested in and type ''C-space''. You will see “Mark set” in the echo area. Now move the cursor to the end of the text region (which moves the point) and type ''M-w'' for copy or ''C-w'' for cut (both called killing text). The text you copy or cut is stored by Emacs in the //kill-ring//, which is a circular buffer that stores the text snippets you kill in a last-in-first-out order. To paste the most recently stored text, move the cursor to where the text should be inserted, and type ''C-y''. The “y” stands for yank, what you'll see paste referred to in the Emacs help documentation (remember Emacs pre-dated modern windowing systems and other full-screen editors, so the terms cut and paste were not in use yet). After a yank (''C-y'') command, you can replace the inserted text with earlier kills in turn by typing ''M-y'' one or more times. Each time you press ''M-y'' the next block of killed text is popped off of the kill ring and inserted into your buffer, replacing the last insert at the same time.
  
 Here are the commands we discussed above, and a few other useful ones: Here are the commands we discussed above, and a few other useful ones:
Line 67: Line 67:
 Emacs has a nice search mode called //incremental search//. To use it, type ''C-s''. The text “I-search:” will appear in the echo area. Now start typing a search string. As you type, Emacs will search for your string in real-time (starting at point), highlighting any matches it finds. You can backspace and re-type text, and the search will continue to change with the text you type. When you find a match, you can hit ''C-s'' to search again and jump to the next match, or you can just hit ''Enter'' to exit the search mode and leave the cursor at the last match. ''C-g'' will abort the search and put your cursor back where you started. Searches will re-start at the top of a buffer if they hit the bottom. You can search backwards in a similar fashion with ''C-r''. Emacs has a nice search mode called //incremental search//. To use it, type ''C-s''. The text “I-search:” will appear in the echo area. Now start typing a search string. As you type, Emacs will search for your string in real-time (starting at point), highlighting any matches it finds. You can backspace and re-type text, and the search will continue to change with the text you type. When you find a match, you can hit ''C-s'' to search again and jump to the next match, or you can just hit ''Enter'' to exit the search mode and leave the cursor at the last match. ''C-g'' will abort the search and put your cursor back where you started. Searches will re-start at the top of a buffer if they hit the bottom. You can search backwards in a similar fashion with ''C-r''.
  
-To replace text that matches a search pattern, type ''M-%''. Youll see “Query replace:” in the echo area. Type a search string, and hit ''Enter''. The echo area will now display “Query replace <search string> with:". Type the replacement string, and hit ''Enter'' again. Emacs will search through your buffer, looking for the search string. When it finds it, it will display “Query replacing <search string> with <replacement string>: (? for help)". Type “y” to replace this match and move onto the next, or “n” to skip this match. Type “!” to replace this occurrence of the search string and all other occurrences in your buffer without prompting. As usual, you can type ''C-g'' to abort a search/replace operation.+To replace text that matches a search pattern, type ''M-%''. You'll see “Query replace:” in the echo area. Type a search string, and hit ''Enter''. The echo area will now display “Query replace <search string> with:". Type the replacement string, and hit ''Enter'' again. Emacs will search through your buffer, looking for the search string. When it finds it, it will display “Query replacing <search string> with <replacement string>: (? for help)". Type “y” to replace this match and move onto the next, or “n” to skip this match. Type “!” to replace this occurrence of the search string and all other occurrences in your buffer without prompting. As usual, you can type ''C-g'' to abort a search/replace operation.
  
 All searches in Emacs are case-insensitive by default, unless you type at least one capital letter in your search string – in that case, the search becomes case-sensitive. All searches in Emacs are case-insensitive by default, unless you type at least one capital letter in your search string – in that case, the search becomes case-sensitive.
  
-One nice feature youll notice is that Emacs remembers the search and replacement strings youve used, so if you type ''M-%'' again, the last search/replace operation can be repeated by just hitting the ''Enter'' key. Prior search and replacement strings can be accessed with up- or down-arrow keys or ''M-p'' and ''M-n'' (for previous- and next-, respectively) – this is like the history mechanism in the Bash shell.+One nice feature you'll notice is that Emacs remembers the search and replacement strings you've used, so if you type ''M-%'' again, the last search/replace operation can be repeated by just hitting the ''Enter'' key. Prior search and replacement strings can be accessed with up- or down-arrow keys or ''M-p'' and ''M-n'' (for previous- and next-, respectively) – this is like the history mechanism in the Bash shell.
  
 Another nice tip during searches is that ''C-w'' will highlight the word around the cursor, then successive words each time it's pressed during a search (so the highlighted area will grow with each press of ''C-w''). You can search again by typing ''C-s'', this time the search string is whatever was highlighted. Another nice tip during searches is that ''C-w'' will highlight the word around the cursor, then successive words each time it's pressed during a search (so the highlighted area will grow with each press of ''C-w''). You can search again by typing ''C-s'', this time the search string is whatever was highlighted.
Line 103: Line 103:
 ===== Buffers ===== ===== Buffers =====
  
-When you visit a file with ''C-x C-f'', Emacs loads the file into a new buffer created just for that file. Existing buffers are not destroyed, but persist and can be switched to with ''C-x b''. Emacs will prompt you for a buffer name to switch to; tab-completion works here as it does in other places. One tip that I wish I had known when I first started using Emacs is to use a special mode called //iswitch mode// to help manage buffers. Since it's not uncommon to have dozens of buffers in an Emacs editing session, you can easily forget buffer names. With iswitch mode enabled, a ''C-x b'' displays a list of buffers in the echo area that changes in real-time as you type characters. You dont even have to type the first few letters of a buffer name, any substring of a buffer name will do. To enable iswitch mode, type the extended command ''M-x iswitchb-mode'' (we'll see later how to permanently enable modes like this in a startup file). To see a list of all buffers, type ''C-x C-b''. The buffer list will appear in a new window. Here is a list of some useful buffer commands:+When you visit a file with ''C-x C-f'', Emacs loads the file into a new buffer created just for that file. Existing buffers are not destroyed, but persist and can be switched to with ''C-x b''. Emacs will prompt you for a buffer name to switch to; tab-completion works here as it does in other places. One tip that I wish I had known when I first started using Emacs is to use a special mode called //iswitch mode// to help manage buffers. Since it's not uncommon to have dozens of buffers in an Emacs editing session, you can easily forget buffer names. With iswitch mode enabled, a ''C-x b'' displays a list of buffers in the echo area that changes in real-time as you type characters. You don't even have to type the first few letters of a buffer name, any substring of a buffer name will do. To enable iswitch mode, type the extended command ''M-x iswitchb-mode'' (we'll see later how to permanently enable modes like this in a startup file). To see a list of all buffers, type ''C-x C-b''. The buffer list will appear in a new window. Here is a list of some useful buffer commands:
  
 | C-x b | Switch to another buffer | | C-x b | Switch to another buffer |
Line 146: Line 146:
 ===== Shell Modes ===== ===== Shell Modes =====
  
-It is possible to run a shell from within Emacs; there are a few different ways to do so. The easiest way is to type ''M-x shell''. This creates a a buffer called *shell* with a shell prompt. Shell commands you type are sent to a system shell and the output displayed in the shell buffer. One thing youll notice in shell mode is that the arrow keys move you around the buffer – they dont access command history as they do in most shells. To access command history, use ''M-p'' and ''M-n'' for previous and next, respectively.+It is possible to run a shell from within Emacs; there are a few different ways to do so. The easiest way is to type ''M-x shell''. This creates a a buffer called *shell* with a shell prompt. Shell commands you type are sent to a system shell and the output displayed in the shell buffer. One thing you'll notice in shell mode is that the arrow keys move you around the buffer – they don't access command history as they do in most shells. To access command history, use ''M-p'' and ''M-n'' for previous and next, respectively.
  
 One of the drawbacks of shell-mode is that it is not suitable for full-screen applications (like less, lynx, mutt or pine), to run these inside of Emacs, you need to use a terminal mode. Type ''M-x ansi-term'', and specify which shell you would like to run (your default will be the shell you are using, e.g. ''/usr/pkg/bin/bash''). One of the drawbacks of shell-mode is that it is not suitable for full-screen applications (like less, lynx, mutt or pine), to run these inside of Emacs, you need to use a terminal mode. Type ''M-x ansi-term'', and specify which shell you would like to run (your default will be the shell you are using, e.g. ''/usr/pkg/bin/bash'').
Line 160: Line 160:
 ===== Emacs Startup and Configuration ===== ===== Emacs Startup and Configuration =====
  
-When Emacs starts it will load a file named ''.emacs'' in your home directory and execute the commands found in it. This file is written in Emacs Lisp, but you can use it without knowing any Lisp, by copying other people's sample files. It is mainly useful for making certain extended commands a permanent part of your Emacs experience, so you dont have to type them every time you start Emacs. In the example below, comment lines start with one or more semi-colons, so you would delete the semi-colons to enable the given feature.+When Emacs starts it will load a file named ''.emacs'' in your home directory and execute the commands found in it. This file is written in Emacs Lisp, but you can use it without knowing any Lisp, by copying other people's sample files. It is mainly useful for making certain extended commands a permanent part of your Emacs experience, so you don't have to type them every time you start Emacs. In the example below, comment lines start with one or more semi-colons, so you would delete the semi-colons to enable the given feature.
  
 <file config .emacs> <file config .emacs>
emacs_tutorial.1725269429.txt.gz · Last modified: 2024/09/02 09:30 by hc9