Subscribe to andrewsw's Blog        RSS Feed

Multi-line replace in Sublime Text

Icon Leave Comment
I recently needed to find and replace blocks of code across multiple files. For example, to replace the footer of every HTML page. (Normally I would use PHP includes so that there is only a single footer, in a single file, but I wasn't allowed to do this for this site.)

It took a bit of digging to achieve this in Sublime Text. (Maybe it is easier in some other editor?) From the Find menu, Find in Files..., we can find and replace across multiple files. There is a small button to the left to allow Regex to be used, but attempting to use \n, \r\n, etc., won't work, nor will \s include newlines. The required regular expression is this:


The ?s modifier enters single-line mode, meaning that .* will include the newline character(s).

.* means any sequence of characters and the question mark following this makes the expression greedy. That is, it will stop at the nearest occurrence of </footer>. [Generally, there will only be one footer anyway, but W3 allows more than one footer (or header) as footer is the footer of its nearest ancestor sectioning content or sectioning root element.]

In the Replace box I typed <footer>, pressed Ctrl-Enter for a newline, then pasted the previously copied new footer-content, Ctrl-Enter and </footer>. Note that you can increase the height of this Find panel by dragging; double-clicking the top of this panel will autofit it afterwards to its original height.

A useful shortcut to know is, after clicking into an opening tag, press Ctrl-Shift-A to select everything within the tag.

Make use of the Where box to restrict the types of files to modify, and the folders to look in. For example,


The build, the three dots on the right, help you obtain the correct syntax for these restrictions.

Press Find first to check what will be replaced!

On replacing all files will be opened. Maybe there is a setting to disable this behaviour, maybe not. It doesn't matter because (after some checking!) you can use the File menu, Save All, and then, after a delay, File and Close All Files.

Apart from always checking Find first, another sensible hint is to use, and check, as much text in the Find area as necessary to be specific about what you will be replacing. For example, I at one point needed to find http:// and switch to //. If I had to change this back later would a search for // be accurate? We can also save the Find Results as a separate file, which could prove a life safer later.

Being able to replace across multiple files is a great feature in Sublime Text, and possibly other editors, but we need to take some precautions. Always ask yourself "Will I be able to reverse this global replace in the future if necessary?".

0 Comments On This Entry


Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

January 2021

17 18 1920212223


    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)