Deleting multiple lines

1,487 views
Skip to first unread message

gregjsmith

unread,
Mar 13, 2009, 4:44:18 PM3/13/09
to TextWrangler Talk
How do I go about deleting lines X through Y in TextWrangler? I don't
want to have to manually select them because there are like 10,000
lines.

Patrick Woolsey

unread,
Mar 13, 2009, 5:39:14 PM3/13/09
to textwr...@googlegroups.com
gregjsmith <gregory...@gmail.com> sez:

Depending on how those lines are delimited, there may be other ways to do
this (e.g. a search with "Extend Selection"), but the basic procedure is:

* turn on "Show Line Numbers" via the Text Options popup, or Edit -> Text
Options

* scroll to line X

* set the insertion point at the start of line X

* scroll until line Y is in view

* Shift-click at the beginning of line Y+1 (which will select everything
from line X to that point)

and press Delete. :-)


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <http://www.barebones.com>
P.O. Box 1048, Bedford, MA 01730-1048


Steve

unread,
Nov 4, 2012, 1:04:48 PM11/4/12
to textwr...@googlegroups.com
An alternate "cheating" method could also be this (turn 'grep' on):

Search:  \A((?:.*\r){50})(?:.*\r){20}
Replace: \1

\A matches the very top of the document.

(  (?:.*\r){50}   )    has a matching group and a non-matching group. The non-matching group contains "everything up to a newline character" which is then repeated 50 times.  The outer "matching" group will store all 50 lines that were just captured so we can stick it back into the "\1" replacement.

(?:.*\r){20}    is another non-matching group of 20 lines. You plan to delete these lines, so there is no reason to "capture" them (the construct of (?: ... ) means not to "collect" the text in it).

So if you have exactly 200 lines that you want to save at the top of the document, then 10,000 lines after that which you want to remove, simply adjust your {50} and {20} respectively, and you're good to go.  :-)

-Steve

Christopher Stone

unread,
Nov 4, 2012, 5:14:38 PM11/4/12
to textwr...@googlegroups.com
On Nov 04, 2012, at 07:56, Derek Neil <krazy...@gmail.com> wrote:
THANK YOU!


On Friday, 13 March 2009 18:39:14 UTC-3, Patrick Woolsey wrote:
Depending on how those lines are delimited, there may be other ways to do this (e.g. a search with "Extend Selection"), but the basic procedure is:
______________________________________________________________________

Hey Derek,

It's easy enough to delete a contiguous range of lines.

This script expects input in the form of '10:20' (no quotes) — where the first number is the start-line and the second number is the end-line of the range.

You can change the default answer to whatever you'd like.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------

try

  

  set delRng to text returned of ¬
    (display dialog "Enter Range of Lines to Delete:" default answer "1:100" giving up after 60)
  set AppleScript's text item delimiters to ":"
  set delRng to text items of delRng
  if length of delRng = 2 then
    set {startRng, endRng} to text items of delRng
    tell application "TextWrangler"
      tell front text window
        delete (lines startRng thru endRng)
      end tell
    end tell
  end if

  

on error e number n
  set e to e & return & return & "Num: " & n
  tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
  if button returned of dDlg = "Copy" then set the clipboard to e
end try

-------------------------------------------------------------------------------------------

Reply all
Reply to author
Forward
0 new messages