Search and Replace in the Nano Text Editor
Look for a specific string or replace it with something else. Here's how to search and replace in Nano.
Every modern text editor comes with a search and replace feature which is also known as text substitution.
There are multiple reasons why you want to search and replace the text in nano such as you entered a wrong value and now it is giving you errors.
In that case, you can search for the incorrect term and replace it with a new one using this feature.
To search and replace in the nano editor, you have to follow 3 simple steps:
- Press
Ctrl + \
orAlt + r
to open search and replace prompt - Enter the term you want to search and press the
Enter
key - It will show you matching terms one by one. If you want to replace all of them press
A
or keep pressingN
until you find the right term and once you do, pressY
.
Ctrl + W
key combination and it will allow you to only find the text, not replace it. Press Alt+W
to go to the next match.Sounds complex? Let's address all the steps in detail.
How to search in the nano editor
Before you search and replace, it is a good idea to know how many terms are there matching your search query.
For that, you need to follow two simple steps:
- Press
Ctrl + w
to get the search prompt and enter the term you want to search - Press
Alt + w
to quickly move between matching terms
For example, here, I searched for the term filename
:
How to search and replace in the nano editor
I assume you already have opened the file in the nano text editor and if you haven't, use the nano command in the following manner:
nano Filename
Now, press Ctrl + \
or Alt + r
to open the search and replace prompt and enter the term you want to replace:
Once done, press the Enter
key and it will ask you to enter the term you want to replace with:
Once done, hit the Enter
key and it will show you terms matching to the search query (the term you enter to be replaced) one by one.
Here you have two options:
- Press
A
and it will replace all the terms with the new one - Keep pressing
N
until you find the term you want to replace and pressY
when you find the one you were looking for that needs to be replaced.
I will show you both.
Replace all with the new term
If you want to replace all the terms that matched your search term, then here's how you do it with an example.
For example, if I wanted to replace every Line
word with the New Line
, then what I would do is:
- Search for
Line
and enterNew Line
as a replacement term. - Once it shows the first result, enter
A
and it will replace all the found terms with a new one at once.
In the end, it shows how many terms were replaced. In my case, it replaced 10!
Replace selective terms only
If you want to be selective while searching and replacing in nano, here's a simple example to do it.
For example, I want to replace the 4th and 7th Line
term with New Line
, then I would follow these 3 steps:
- Search for the term
Line
and enterNew Line
as a replacement term. - Keep pressing
N
until I reach to the 4thLine
term and then pressY
and it will be replaced withNew Line
. Will follow the same for the 7th term. - Once done, press
Ctrl + C
to exit from the search and replace prompt.
Looks complex? Here's how I did it:
Pretty easy. Right?
Do more with the Nano editor
If you are a beginner and want to get started, here's a beginner's guide to the nano editor:
I hope you will find this guide helpful.