Copy a whole line at once?

2,075 views
Skip to first unread message

cameron.g.brown

unread,
Nov 24, 2010, 3:12:15 AM11/24/10
to TextWrangler Talk
Hi there,

First post to the group. I'm pretty new to TextWrangler too, so please
excuse newbie mistakes =]

My question is: Is there a way to quickly duplicate/copy an entire
line of text? I.e., instead of "select entire line, press Cmd+C,
position cursor on next line, press Cmd+V" I want to to hit Cmd+
(something) and the current line is automagically duplicated on the
line below.

I've used ScITE on Windows for years, and it lets you do this with Ctrl
+D. If I have:

LINE OF TEXT

...if my cursor is anywhere on the line I can hit Ctrl+D and I'll end
up with:

LINE OF TEXT
LINE OF TEXT

Without needing to mark a selection or manually reposition the cursor.
Which is awesome, and I miss it! This is practically the only thing
bugging me about TW, which is generally pretty awesome.

Can I do this in TW?

Thx in advance for any help...

Cam.

Tom Robinson

unread,
Nov 24, 2010, 10:26:25 PM11/24/10
to textwr...@googlegroups.com
Welcome to TextWrangler Cam.

This is how I do it:

1. select the entire line either with a triple click in it, or click in the left margin*

*Requires turning on prefs 'Editing: General -- Allow single-click line selection'

2. hit command-c, command-v, command-v

Cheers

Jean-Christophe Helary

unread,
Nov 24, 2010, 10:39:10 PM11/24/10
to textwr...@googlegroups.com

On 25 nov. 10, at 12:26, Tom Robinson wrote:

> Welcome to TextWrangler Cam.
>
> This is how I do it:
>
> 1. select the entire line either with a triple click in it, or click in the left margin*

Cmd+L

> *Requires turning on prefs 'Editing: General -- Allow single-click line selection'
>
> 2. hit command-c, command-v, command-v


Jean-Christophe Helary
----------------------------------------
fun: http://mac4translators.blogspot.com
work: http://www.doublet.jp (ja/en > fr)
tweets: http://twitter.com/brandelune

Thomas Fischer

unread,
Nov 25, 2010, 3:20:26 AM11/25/10
to textwr...@googlegroups.com

Well, the answer is no and yes.
No, because there is no standard feature in TextWrangler to do this (AFAIK).
Yes, because TextWrangler is very scriptable: Look at the AppleScript Menu (the one with the scroll) in TextWrangler:
Open Script Editor
Create an AppleScript:

tell application "TextWrangler"
tell window 1
select line the (startLine of the selection)
copy (contents of the selection) as text to myText
set the contents of the selection to myText & myText
end tell
end tell

and save it as script "Duplicate Line" to TextWrangler's Scripts Folder.
Then choose Windows -> Palettes -> Scripts, select "Duplicate Line" and assign it Ctrl-D or whatever you like.

This flexibility is what makes TextWrangler so useful for me.

All the best
Thomas


cameron.g.brown

unread,
Nov 29, 2010, 1:01:44 AM11/29/10
to TextWrangler Talk
> Create an AppleScript:

Thanks for the detailed help, that worked perfectly!

Kimo

unread,
Jan 30, 2017, 10:51:22 PM1/30/17
to TextWrangler Talk
Hi Thomas,

I know this is an old post, but I was wondering how I might be able to adapt your script to repeat the copy of the first line below it for a large number of times (1500+)? I need to parse a database file, which oddly enough, has the same values for 1566 rows.

Example:

First Line
First Line
First Line
First Line
First Line
and so on...

The next question, if you like a challenge, would be "how do I make a specific value increment. (No worries if you don't have a tip for that.)

Thanks,
Kimo

Thomas Fischer

unread,
Jan 31, 2017, 7:46:38 AM1/31/17
to textwr...@googlegroups.com
Hi Kimo,

I don’t quite understand your setting.
But in general repeating is not a problem.
You might try

tell application "TextWrangler"
tell window 1
set myStart to the (startLine of the selection)
set myEnd to the (endLine of the selection)
repeat with myIndex from myStart to (myStart + 2 * (myEnd - myStart)) by 2
copy contents of line myIndex as text to myText
set the contents of line myIndex to myText & return & myText
end repeat
end tell
end tell

This would duplicate every single line in you selection, is that what you want? But I don’t see in which sense this would be „First lines“… Your example is not very specific.
I you want the first line to be repeated 1566 times, you’d need something like

tell application "TextWrangler"
tell window 1
set myLineNumber to the (startLine of the selection)
copy (contents of line myLineNumber) as text to myText
set myLines to ""
repeat 1566 times
set myLines to myLines & myText & "\n"
end repeat
set the contents of the selection to myLines
end tell
end tell

To increment a particular value I would need to know how to identify it first and then in which sense you’d want to increment it: numerically, alphabetically, with a particular syntax or length?
But again: AppleScript should be able to do it.

Best regards
Thomas Fischer



--
This is the TextWrangler Talk public discussion group.
If you have a feature request or would like to report a problem,
please email "sup...@barebones.com" instead of posting here.
---
You received this message because you are subscribed to the Google Groups "TextWrangler Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to textwrangler...@googlegroups.com.

Christopher Stone

unread,
Feb 2, 2017, 1:20:18 PM2/2/17
to TextWrangler-Talk
On Jan 30, 2017, at 20:17, Kimo <bk...@azurelink.com> wrote:
I know this is an old post, but I was wondering how I might be able to adapt your script to repeat the copy of the first line below it for a large number of times (1500+)? I need to parse a database file, which oddly enough, has the same values for 1566 rows.


Hey Kimo,

Please be a little more specific about your start and end conditions.

Show samples of at least one line of input.

Show the desired output.

Show what value needs to be incremented and how.

Personally I would not iterate through lines in the document.  I would work in either the selection or the entire document as a whole (whichever is more appropriate to the task).

I'd use the Satimage.osax AppleScript Extension to do the parsing, because it will be lightning fast.

-------------------------------------------------------------------------------------------
# REQUIRES that the Satimage.osax be installed.
-------------------------------------------------------------------------------------------
tell application "TextWrangler"
   tell text of front text document to set selectedLines to contents of the selection
end tell

set selectedLines to splittext selectedLines using linefeed

set incrementIt to 0
repeat with theLine in selectedLines
   set incrementIt to incrementIt + 1
   set tempLine to contents of theLine
   set tempLine to change "(one )(1)" into ("\\1" & incrementIt) in tempLine with regexp without case sensitive
   set contents of theLine to tempLine
end repeat

set selectedLines to join selectedLines using linefeed

tell application "TextWrangler"
   tell text of front text document to set the text of the selection to selectedLines
end tell
-------------------------------------------------------------------------------------------

This can done in BBEdit without the Satimage.osax, because BBEdit has the ability to replace-in-string similar to what I'm doing above.  It's not quite as fast as using the Satimage.osax, but it's much faster than iterating through each line, selecting, parsing, changing, and replacing the line.

Try it on some sample text:

one 1 two 2
one 1 two 2
one 1 two 2
one 1 two 2
one 1 two 2
one 1 two 2
one 1 two 2
one 1 two 2

Depending upon your comfort level this can be done a bit more simply with a Perl text filter:

#!/usr/bin/env perl -sw

my $i = 0;

while (<>) {
    if (m!(one )(1)!) {
        ++$i;
        s!(one )(1)!$1$i!;
        print;
    } else {
        print;
    }
}


--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages