replace whole line of text in BBEdit

Hi, Please Help.
I’m trying to edit an xml file in BBEdit but unfortunately I’m unable to use a search and replace scanario as I don’t always know what I will be searching for, just that I want to replace the whole line of text. Any ideas on how I can do this?

It is unclear what you need to do. Please provide more details, and perhaps an example or two.

BBEdit supports regexp search&replace functionality (checkbox ‘use grep’)
so you can search for ^.searchstring.$ to match whole lines containing the string ‘searchstring’


You can also get plain ol’ AppleScript to do a few things.

Here are one or two examples that should work in either BBEdit or TextWrangler:

(* this just makes a sample document. *)

set sample_text to "Walking all the day, near tall towers
where falcons build their nests
Siver winged they fly,
they know the call of freedom in their breasts
Saw Black Head against the sky
with twisted rocks that run down to the sea..."

tell application "BBEdit" (* or "TextWrangler" *)
	make document with properties {text:sample_text}
end tell

delay 3 (* just some time to view the original text *)

(* script proper starts here *)

tell application "BBEdit" (* or "TextWrangler" *)
	tell document 1
		(* replace first line that contains a search string *)
		set search_string to "falcons"
		set line_replacement to "So... what happened to that great line about falcons building nests?"
		tell (first line whose contents contains search_string)
			if exists then set contents to line_replacement
		end tell
		(* replace every line that contains a search string *)
		set search_string to "all"
		set line_replacement to "No lines containing \"" & search_string & "\" allowed!"
		repeat with l in (lines whose contents contains search_string)
			set l's contents to line_replacement
		end repeat
		(* replace a numbered line *)
		set line_number to 5
		set line_replacement to "... And where did line " & line_number & " wander off to?"
		tell (first line whose startLine is line_number)
			if exists then set contents to line_replacement
		end tell
	end tell
end tell

Sincere apologies to Phil Colclough, for having messed with his beautifully evocative lyrics.

Hi Kai,
That’s just what I needed,
Thanks for all of your advice, great examples! :smiley:

This is a “blind solution”. We don’t know what problem you’re trying to solve, here. There is a possibility that what you think is going to solve your problem . . . won’t, or won’t under some likely set of circumstances. Maybe you’ve got the right solution, maybe you don’t. Without describing the problem more fully, we can’t know if there’s a more complete solution or not.

In a nutshell, the point is that you noted that you had a problem to solve, and that you wanted to perform a specific task. But what if the specific task isn’t the best solution to the problem?

Glad to have been able to help, blend. :slight_smile: