Add line of code in entire project
-
Hi,
I am using Notepad++ to edit my website, which is in HTML, and contains 30 pages. If I want to edit my navigation menu, I will have to edit on each page, which is time-consuming and not appropriate.
Example:
<a href=“#”>LINK 1</a>
<a href=“#”>LINK 2</a>
<a href=“#”>LINK 3</a>
<a href=“#”>LINK 4</a>I need to know two things.
1/ How to add a line of code, let’s say <a href=“#”>LINK 5</a> after <a href=“#”>LINK 4</a> in all pages I have in the project?
2/ How to add a line of code, let’s say between LINK 3 and LINK 4 on all web pages at one in the project?
-
You can choose between two ways to do this.
One option is to use Find in files… from the Search menu. Set up Find what and Replace with as needed, put the appropriate file mask (e.g.,
*.htm
) in Filters, and select the appropriate Directory. That works if you want to make the change in all files in a single directory that match the pattern.The other option is to open all the files you want to change in tabs within a single instance of Notepad++, then select Replace… from the Search menu, set up Find what and Replace with as needed, and use Replace All in All Opened Documents.
How you set up Find what and Replace with depends largely on whether the original text and the new text are exactly the same in every file. If they are, you’ll probably want to use Search Mode: Extended so you can easily add line endings where you want to create new lines. If they vary with context, you’ll need to use Search Mode: Regular expression and learn a bit about how regular expressions work if you don’t already know about them.
EDIT to add: If by “project” you mean a Notepad++ project, there’s a feature for searching and replacing within a project. I’ve never used Notepad++ projects, but if you do, it’s probably obvious how to use that instead of Find in Files.
-
@Coises Your answer is on replacing. While me, I want to search and add. Like adding something after <a href=“#”>LINK 3</a> in all pages which have <a href=“#”>LINK 3</a>. It should be before or after it.
-
@Alij2022
You could use the Replace with, search for <a href=“#”>LINK 3</a> and replace with <a href=“#”>LINK 3</a>\n<a href=“#”>LINK 4</a>
This will replace the original line, and add a new one with the link you want. -
@Alij2022 said in Add line of code in entire project:
Your answer is on replacing. While me, I want to search and add
If you really think hard about it, “search and add” is really just “replace”…
-
Hello, @Alij2022, @coises, @Pierre-Åberg, @alan-kilborn and All
So, to answer your two goals, let’s simplify the problem and suppose this INPUT text, placed in a new N++ tab :
<a href=“#”>LINK 1</a> <a href=“#”>LINK 2</a> <a href=“#”>LINK 3</a> <a href=“#”>LINK 4</a> LAST line of code
Then :
-
Open the Replace dialog (
Ctrl + H
) -
Untick all box options
-
For your first goal :
-
SEARCH
<a href=“#”>LINK 4</a>(\R)\K
-
REPLACE
<a href=“#”>LINK 5</a>\1
-
-
For your second goal :
-
SEARCH
<a href=“#”>LINK 3</a>(\R)\K
-
REPLACE
A NEW line of code\1
-
-
Tick the
Match case
andWrap around
options -
Select the
Regular expression
search mode -
Click once ONLY on the
Replace All
button ( do not use theReplace
button )
Here you are !
You should get, successsively, these two OUTPUT texts, below :
<a href=“#”>LINK 1</a> <a href=“#”>LINK 2</a> <a href=“#”>LINK 3</a> <a href=“#”>LINK 4</a> <a href=“#”>LINK 5</a> LAST line of code
Then :
<a href=“#”>LINK 1</a> <a href=“#”>LINK 2</a> <a href=“#”>LINK 3</a> A NEW line of code <a href=“#”>LINK 4</a> <a href=“#”>LINK 5</a> LAST line of code
If these two regex S/R do what you want, expand this method to a bunch of files :
-
Do a backup of the folder ontaining your
HTML
files ( one never knows ! ) -
Open the Find in Files (
Ctrl + Shift + F
) -
Enter, either, the first or the second regex S/R, in the
Find what :
andReplace with :
zones -
Click the
Match case
option -
Enter
.htm*
in theFilters
zone and yourHTML
folder in theDiretory
zone -
Click on the
Replace All
button and confirm the dialog windows
Best Regards,
guy038
-
-
I think that the approach you are using to do that is wrong in principle: I mean having that code repeated 30 times one per page all over your website is not what you really should do.
Wouldn’t be better to have one file that contains the whole menu code and then “embed” that file into all of your web site files where it is needed with a server side include or a simple php code (withc are just a single coder row)?Does your website’s server support server side include or php?
With one of those feature you can do something like
The following is pseudo code not actual code:
menu.html page1.html embeds > menu.html page2.html embeds > menu.html --- page30.html embeds > menu.html
So that way in future when you’ll need to modify the menu, you will need to modify it once and all pages will inherit the new update automatically.
This way you can also strip out any rendudant code from you pages and put it into a single file and include it into the point where you need it with just a line of code:
Server_Side_Include: (need .htmls file extension)
<!–#include virtual=“menu.html”–>While in php you can insert any PHP file into the HTML code by using two keywords that are ‘Include’ and ‘Require’.
Try to get used to these and you will be much happier web dev.
-
When you make such a post, you should also add that “this really isn’t a topic for this site (this site is only for Notepad++ discussion); to pursue these ideas you should find a more appropriate place”.