How to Find and Replace Text in Emacs [Extensive Guide]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

With versatility and flexibility, Emacs has become a popular text editing and manipulation tool among general users and professional developers. A customizable and easy-to-access text editor, Emacs is equipped with tools to find and replace data of string type. Thus, this article will provide an in-depth analysis of methods on how to find and replace text data (both words and patterns using regex) in Emacs. It will mention the useful keyboard shortcuts to accomplish such tasks. Moreover, it will mention GUI-based techniques to make your text-based search and replace operations smoother in Emacs.

1. Find String Using CTRL+S

The search feature is one of the powerful features in Emacs text editor that lets users search and find specific strings (keywords). To find a specific string in Emacs, press CTRL+S first. Then enter the string to search when prompted. If there’s a match or any matches, they will be highlighted, and the match with the current cursor position will be highlighted in purple like below:find string with CTRL+SIn addition, you can perform an incremental search with CTRL+S. To do so, press CTRL+S continuously to find and search through every occurrence one by one:incremental find of stringSo, hitting “CTRL+S” continuously allows for jumping from one occurrence to another if there’s a match.

2. Find String with Backward Search Using CTRL+R

It is also possible to perform a backward search and thus find the intended string. To find a specific string via backward search press CTRL+R and enter the query string once prompted:CTRL+R deos backward searchThe above image shows an instance of the finding of the query string “Bruce” backward. The purple highlighted “Bruce” denotes the starting occurrence according to the cursor position.

Moreover, Continuous pressing of CTRL+R performs incremental finding i.e. jumping through every match of the query string (Bruce, here, for example):incremental string search backward

3. Find String with Regular Expressions Using CTRL+ALT+S

Emacs allows users to search and find queries using regular expressions (Regex). To find a pattern using a specific regular expression, press the keys CTRL+ALT+S and type the pattern when prompted:string find with regexIn the above, the expression \w*tt\w* finds every occurrence of the words having the pattern “tt” which is the word “Potter” in this case.

How to Find and Replace Text in Emacs

Apart from the finding feature, Emacs allows for quick replacement of the found query. There are several keyboard shortcuts for replacing the word in a text file. Emacs lets the users perform the operation query replace via find and replace which allows for finding a string and then replacing it.

To, replace text using Emacs in Linux, use the keyboard shortcut ALT+SHIFT+5 (also called ALT+% or M-%). After pressing ALT+SHIFT+5, Emacs will prompt you to enter the text to be replaced:entering query to replaceHere, I have entered the word “Bruce” to replace it. As a result, Emacs has marked the occurrences first.

Now, Press ENTER and then type the new string that will replace the old queried string:new string to replace withFinally, press ENTER and subsequently press Y to replace each occurrence one after another:

query replace Here, you see that the word “Bruce” is replaced by “harry” in an incremental manner with the continuous pressing of Y (denotes Yes).

However, to replace all the occurrences of the query string (without asking for permission) press “!” (exclamation mark) once there’s a match:all replace without askingEmacs has replaced all 4 matched occurrences of “Bruce” with the new word “harry” at once (without permission)

In addition, press N when prompted to replace if you wish not to replace any occurrence. Emacs will skip it and move to the next occurrence for permission (if it’s an incremental replacement):skipping replace pressing N

Note: Press CTRL+G to cancel the replacing operation if you wish. This will skip the entire query replacement keeping the file as it is.

Replace Text in Emacs with Regular Expressions

Similarly, Emacs can replace the matched pattern in a text by employing the regular expression technique. For example, consider the file gotham.txt:contents of gotham.txt file Now, if you want to replace the word Potter with the word Dickinson (for example), press CTRL+ALT+SHIFT+5 (CTRL+ALT+%) to enter the pattern using regex to replace:query to replace using regexFor example, I have entered the expression \w*tt\w* which will locate the words in gotham.txt having the pattern tt, and found 8 occurrences of the word “Potter”.

Then, after pressing ENTER, type the new pattern to replace with and press ENTER:entering new word to replace with Finally, after pressing ENTER, type Y to replace “Potter” with “Dickinson” in a recursive manner:query string is replacedThe image states that Emacs replaces “Potter” with “Dickinson”.

Additionally, to skip, quit, or replace all occurrences without asking, you know the gist.

Find and Replace in Emacs with GUI and Keyboard Shortcuts

The article, so far, has shown how to find and replace text in Emacs using keyboard shortcuts (also called keyboard bindings). Now, This section will introduce the GUI (Graphical User Interface)–based approaches to find and replace text using Emacs. Though this approach will also integrate keyboard shortcuts along with GUI, this combined approach will make text finding and replacing smoother and more user-friendly.

Now, in the case of GUI, there are some handy toolbar icons to find string and thus replace it:the find toolbar icon

The above image shows the toolbar icon Find for finding operations in Emacs. Simply click on the icon (instead of using CTRL+S) and type the string to find as before when prompted.

Now, once you type and search the query string, you can get the prompt to enter a new string by clicking on the “Replace” toolbar icon:replace toolbar icon and other useful toolYou can also see the toolbar icon for forward and backward incremental search and the tool to cancel the operations (The Abort icon).

So, you can adopt this GUI and Keyboard shortcut combined method to find and replace words, and string patterns (you already know the gist) in Emacs.

Case-Sensitivity in Emacs Find and Replace

By default, Emacs performs case-sensitive searches but with some interesting behavior. Let’s explore them one by one and start by looking at the following image:case sensitive find of query string The image states that Emacs matches only those instances having an exact match with “Bruce” (B in capital and the rest are in lowercase). And the lowercase bruce is not selected.

However, if you search using the lowercase “bruce”, then all the instances of the query string bruce will be selected (regardless of the case) since this time Emacs matches the query likely with the case-insensitive search:case insensitive find selects all the occurencesTherefore, practice an extra amount of care keeping the above behavior of Emacs in mind when you perform the find and replace operation using the text editor.

Conclusion

This article discusses How to find and replace strings from a text in Emacs in Linux. It mentioned different keyboard shortcuts to find words or strings and subsequently replace them with new ones. Moreover, it has shown finding and replacing of patterns using regular expressions. In addition, it has discussed the finding and replacing of strings using a combination of GUI and Key Bindings.

People Also Ask

How to find a specific string in Emacs?

To find a specific string in Emacs, Press CTRL+S and enter the query string when prompted. As a result, the words with a match will be highlighted so that you can easily locate the query string. In addition, to find a string using a regular expression, use the key binding CTRL+ALT+S.

Can I use GUI to find a specific text in the Emacs text editor?

Yes, you can use GUI to find a specific text in the Emacs text editor. Go to the toolbar menu of Emacs and click the icon that says Find (the icon with magnifying glass). Then enter the query string once prompted. As a result, the matched query will be highlighted. This is how Emacs lets you find a string using GUI.

How to find a specific file in the Emacs text editor in Linux?

To find a specific file in the Emacs text editor, press CTRL+X and then press CTRL+F.  Then type the file name that you want to find once prompted. If the query file already exists, Emacs will open the file with a text buffer. However, if the file does not exist, Emacs will first create the file and then open it with a blank text buffer.

How to replace a string in the Emacs text editor?

To replace a string in the Emacs text editor, press ALT+SHIFT+5 (ALT+%) to first find the occurrence of the string. Emacs will prompt you to enter the string to find and replace. Once there’s a match, it will be highlighted. Now, press ENTER and type the word that will replace the found query. Again, press ENTER and press Y to replace the string in Emacs or press “!” (exclamation mark) to replace all the available matches without asking. Additionally, press N to skip replacing the current occurrence and CTRL+G to cancel the entire replacement process in Emacs. Moreover, you can replace using regular expressions by pressing CTRL+ALT+SHIFT+5.

Can I find a string backward search in Emacs?

Yes, you can find a string in Emacs using backward search. To find a string with a backward search in Emacs, press CTRL+R and enter the query string when prompted. Emacs will highlight the matched occurrences employing a backward search. To jump through all the matched occurrences, recursively press CTRL+R.

2.7/5 - (4 votes)
Md Masrur Ul Alam

Assalamu Alaikum, I’m Md Masrur Ul Alam, currently working as a Linux OS Content Developer Executive at SOFTEKO. I completed my Bachelor's degree in Electronics and Communication Engineering (ECE)from Khulna University of Engineering & Technology (KUET). With an inquisitive mind, I have always had a keen eye for Science and Technolgy based research and education. I strongly hope this entity leverages the success of my effort in developing engaging yet seasoned content on Linux and contributing to the wealth of technical knowledge. Read Full Bio

Leave a Comment