Learning Emacs series tutorials (8): Find and replace

Source: Internet
Author: User
Tags isearch

Search replacement is a feature that is not available in any editor. Even a small notepad provides quick search, not to mention a giant like Emacs. The importance of Emacs is not mentioned here. Next we will introduce you step by step how to search and replace Emacs.

Emacs divides search into four categories: simple search, incremental search, phrase search, and regular search. The four methods have their own characteristics and their applicability are also different. You can familiarize yourself with one or two methods as needed.

I. incremental search

The reason for introducing the incremental search is that this is the most commonly used search method, which is convenient and directC-S(Isearch-forward) Then, the incremental search status is displayed. The following prompt is displayed for minibuffer:

 

In fact, incremental search is very easy to understand, that is, it is to search at the input side. During the input process, the matching closest to the cursor will be highlighted in a dark color, and other matching strings on the screen will be highlighted in a light color, see the figure below:

 

All words that contain "to" are marked, such as tutorial. You can use backspace to delete input characters at any time when you enter the search string. After the input is complete, press enter to locate the matched string closest to the forward, and the prompt "Mark saved where search started" appears ", that is to say, we add the position where you start to search for the time mark to the mark ring. We can return to that point through c-u C. If the string cannot be found, the prompt is "failing I-search", followed by the search content. C-s searches are performed forward. We can also useC-r(Isearch-backward). Note that the cursor will be located at the front of the matching string. Although the search process only goes in one direction, at the bottom of the document or in the header, it will still return to the header or bottom of the document to continue searching.

In the search process, the first matching item is not necessarily the content we need, or we need to find more than one place. In this way, we want to quickly locate the next matching item, the solution is also very simple. In the following example, we enter "to" and do not press Enter. Instead, we can use C-s to locate the problem backward or use c-r to locate the problem, stop where you want. In addition, if you quit this search and want to find the same item later, useC-SThat's all.C-R c-r.

Emacs maintains a search ring during multiple searches. This is the third ring we have seen. The first two are Mark ring and kill ring. After inputting C-S, you can use M-N and M-P to call up the content of the previous and next searches in the search ring. Although this is also called a ring, it is a standard queue and will not be cyclically formed.

There are also some special features in incremental search: Usually case-insensitive searches are used, but if the search content we enter contains lowercase letters and lowercase letters, emacs automatically enters the case-sensitive matching search status. You can also useM-CThe quick switch is case sensitive, but is only enabled for this search. If you want to find a carriage returnC-JCan also be usedM-Tab(Isearch-complete) The search is automatically completed. The candidate items are the items in the search loop. Of course, using M-tab is ineffective, and even Emacs is absent after use, we need to rebind Isearch-complete; PressC-HThe help of incremental search will be displayed. You can study the content on your own. UseC-WYou can copy words at the cursor to the search area for quick input. This function is commonly used.C-yIt can copy the content at the cursor's position until the end of the line to the search area, reminding that the two Commands will replace all the letters into lower case during the copy process;M-yYou can also copy the content in the kill ring to the search area;C-m-WUsed to delete the last character in the search area.C-m-yCopy the character at the cursor to the end of the search area. There is also a way to copy the character at the cursor. after entering the incremental search status, locate the character at the end of the search area by M-E, and then useC-FYou can start copying.

 

2. Simple search

Simple search, as its name implies, is a simple search method (in fact, it is not necessarily faster than incremental search), and sometimes it is called non-incremental search. The command isC-s RET string RETFirst, call up the incremental search with C-S, press enter to cancel the simple search, enter the string to be searched, and then press ENTER Emacs to start searching backwards, until the first satisfied string is followed. When you press the command, you can observe the prompt in minibuffer. After you press C-S, a blue "I-Search:" will appear, and press enter to change it to "Search :", when the entered content is found, the prompt "Mark saved where search started" appears. In fact, the simple search and incremental search are similar in most cases. commands in incremental search can basically be used here, the biggest difference is that it does not start to search until the input is complete, and there is no highlight. If you look forward,C-r RET string RET. Forward and backward commandsSearch-forward,Search-backward.

 

3. phrase search

Phrase search is generally used in editing large texts, such as writing news or writing letters. During the search process, it ignores some punctuation marks, such as line breaks, commas, periods, and so on. It also performs incremental search while entering and searching, however, for a word, it is a full-word match. The command isM-S W(Isearch-forward-word). Search example:

 

 

When I was looking for "it", the permitted circled in the red box was not highlighted. When I input "it under", the comma and carriage return characters were ignored, these features are useful when searching for large sentences. You can also use the non-incremental search method for phrase search. The corresponding commandM-s w ret words RET(Word-search-forward).M-s w c-r RET words RET(Word-search-backward), The words here are the phrases you need to find. Most of the remaining phrase search methods are similar to those described above.

 

Iv. Regular Expression search

The regular expression is used for this search. The function is quite powerful. It mainly depends on the level of the regular expression of each user. The regular expression syntax in Emacs will be discussed later. Here we will only explain how to call up the regular expression search. CommandC-m-S(Isearch-forward-Regexp), Cough, use the shortcut key to turn off QQ. This is to look forward, the command can be seen to use the incremental search method, backward isC-m-R(Isearch-backward-Regexp).

 

V. Replacement

The other half of this article introduces the replacement function in Emacs, which is to convert a string into another string.

The simplest and most crude method is to useReplace-stringThe usage of this command is as follows: M-x replace-string RET oldstring RET newstring ret. Enter the string to be replaced by the first carriage return, enter the string to be replaced by the second carriage return, and finally confirm. It will replace all the strings matching the oldstring. We recommend that you do not use this before you are absolutely sure. During the replacement process, emacs can intelligently process case sensitivity. For example, if you input m-x replace-string ret abc ret efg ret, all "ABC" changes to "EFG ", however, "ABC" is replaced with "EFG". In this case, "ABC" is changed to "EFG", uppercase and lowercase, and lowercase. Of course we may not want to make such a changeCase-replaceSet it to nil. There is also a related variable.Case-fold-SearchIf it is set to nil, it will forcibly match the case when searching.

Regular version commands for brute force replacementReplace-Regexp, The usage is the same as above, but the oldstring is changed to a regular expression.

In fact, we do not want to replace everything during the replacement process. We still want to have a process to judge and choose for ourselves. In Emacs, we call it "Search replacement,M-%(Query-replace). The oldstring type is the same as the previous one. Then, newstring is input, but it is found one by one. Then, each matching string is used to ask the user how to deal with it, make the next action based on your answer. The following table lists all possible answers and corresponding answers.

Input Response
SPC or Y Replace the current match and move it to the next match
Del or N Ignore this match and proceed to the next match
. Replace the current match and exit
, Replace the current match and stop it here, and then press y to move forward.
! Replace all remaining matches
^ Return to the previous match
RET or Q Exit directly
E Modify new string
C-r Enter the recursive editing status
C-W Deletes the current match and enters the recursive editing status.
C-m-C Exit the recursive editing status and return to search and replace
C-] Exit the recursive editing status and exit searching and replacement.
C-H Show Help

In these commands, there will be more than Y, N, and Q. However, if you press any other key, the system will exit the search and replacement status. If you exit accidentally, you can use C-x ESC to return the result.

Finally, let's talk about the recursive editing status. Simply put, you suddenly find that you need to modify something during the search and replacement process, but you don't want to exit the search and replacement process, at this time, emacs provides a temporary editing status that allows you to do what you just remembered. After you have done it, you can return to the searching and replacing status. This temporary status is called the recursive editing status.

 

Note: A square brackets "[]" is added to the mode name in mode line to indicate that the mode is in the recursive editing state. After the execution, you can use C-m-C to return to the previous state. In fact, you can use commands at any time.Recursive-EditGo to recursive editing and useAbort-recursive-EditOrTop-level Exit or use C-m-C to return the previous status.

 

Summary:

Buttons

Command

Function

C-S

Isearch-forward

Forward incremental search

C-r

Isearch-backward

Perform incremental search backward

M-C

(Search status) switch to uppercase sensitive

C-J

Newline-and-indent

(Search status) enter a line break

M-Tab

Isearch-complete (Search status) Automatic match

C-H

(Search status) Go to search for help

C-W (Search status) Copy words at the cursor to the search area
C-y (Search status) copy the cursor to the search area until the end of the row
M-y (Search status) copy the last item in the kill ring to the search area
C-m-W (Search status) Delete the last character in the search area
C-m-y (Search status) copy the characters at the cursor to the end of the search area
C-F (Search status) copy the characters at the cursor to the end of the search area
C-s RET Search-forward Forward simple search
C-r RET Search-backward Perform simple search backward
M-S W Isearch-forward-word Search for phrases forward
M-S W RET Word-search-forward Forward phrase search (non-increment)
M-s w c-r RET Word-search-backward Search for phrases (non-increment)
C-m-S Isearch-forward-Regexp Forward regular Lookup
C-m-R Isearch-backward-Regexp Perform backward regular search
Replace-string Full text replacement
Replace-Regexp Full-text Regular Expression replacement
M-% Query-replace Search and replace
Recursive-Edit Go to recursive editing
Abort-recursive-Edit Exit recursive editing
Top-level Exit recursive editing

 

 

Variable

Function

Case-replace Set automatic case replacement
Case-fold-Search Set case-insensitive search

 

 

Not complete...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.