How do I search the contents of all my Terminal windows
Terminal tabs have two useful AppleScript properties called history and contents.

contents contains the text content of the tab currently visible in its pane (that is, any lines lying within the boundaries of the visible portion the window).

history contains the entire history of the tab's contents, from the moment it came into existence to the moment the property's value is requested.

The AppleScript command to retrieve the entire contents (history) of all tabs across all windows currently open in Terminal is:

    tell application "Terminal" to get the history of every tab of every window


But, as you discovered, history can contain an awful lot of text data, depending on how many tabs you have open and how actively they've been used.

Your approach to dealing with this issue is a good one: looping through each tab/window individually and handling its contents one-by-one.

Your current AppleScript only retrieves the contents of each tab, which is going to be a very small subset of the data contained by each tab (which may be sufficient for your needs).  If you need to deal with the larger quantities of data contained in each tab's history property, this might be a means of doing so:

    set F to POSIX file "/Users/CK/Desktop/Terminal.txt"

    set _f to open for access F with write permission
    set eof _f to 0 -- Erase old contents

    tell application "Terminal"
        -- Loop through every window
        repeat with W in (a reference to windows)
            set [W] to W

            -- Loop through every tab of each window
            repeat with T in (tabs of W)
                set [T] to T

                set R to (a reference to history of T)
                write R &amp; linefeed to _f
            end repeat
        end repeat

    end tell

    close access _f


This should sequentially append the text history of each tab from each window to the text file specified at the top of the script.