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
Next revision
Previous revision
Next revisionBoth sides next revision
emacs_tutorial [2021/03/21 05:58] – [Extended Commands and Command Completion] hc9emacs_tutorial [2021/03/21 06:11] – [Search & Replace] hc9
Line 51: Line 51:
 ===== 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-wfor copy or "C-wfor 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-yone or more times. Each time you press "M-ythe 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 65: Line 65:
 ===== Search & Replace ===== ===== Search & Replace =====
  
-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-sto search again and jump to the next match, or you can just hit "Enterto exit the search mode and leave the cursor at the last match. "C-gwill 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-%". 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 "Enteragain. 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-gto 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 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 "Enterkey. Prior search and replacement strings can be accessed with up- or down-arrow keys or "M-pand "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-wwill 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.
  
 Here are the search and replace commands we discussed: Here are the search and replace commands we discussed:
emacs_tutorial.txt · Last modified: 2021/03/21 06:38 by hc9