# Copy/paste line numbers with code

**URL:** <https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424>\
**Category:** Ruby\
**Created:** [September 12, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424 "2007-09-12T16:01:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/1 "2007-09-12T16:01:09Z")

</div>

I’m writing up my project report and want to include line numbers with  
the code that i paste into my word doc writeup. Does anyone know of a  
ruby editor for windows that will allow this? I use SciTE and Eclipse  
already but can’t see how to do it using those, so if someone knows a  
way for those that would be ideal.

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/2 "2007-09-12T16:51:20Z")

</div>

Max W. wrote:

> I’m writing up my project report and want to include line numbers with  
> the code that i paste into my word doc writeup. Does anyone know of a  
> ruby editor for windows that will allow this? I use SciTE and Eclipse  
> already but can’t see how to do it using those, so if someone knows a  
> way for those that would be ideal.
> 
> thanks  
> max

I have used a bit of Ruby code to do this:

code = File.open(“Song.rb”)  
code.each {  
|line| puts “#{code.lineno}. #{line}”  
}  
code.close

present your .rb file to replace “Song.rb” and copy code with line  
number from your Console or SciTE

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/3 "2007-09-12T16:54:19Z")

</div>

Hu Yoi wrote:

> Max W. wrote:
> 
> > I’m writing up my project report and want to include line numbers with  
> > the code that i paste into my word doc writeup. Does anyone know of a  
> > ruby editor for windows that will allow this? I use SciTE and Eclipse  
> > already but can’t see how to do it using those, so if someone knows a  
> > way for those that would be ideal.
> > 
> > thanks  
> > max
> 
> I have used a bit of Ruby code to do this:
> 
> code = File.open(“Song.rb”)  
> code.each {  
> |line| puts “#{code.lineno}. #{line}”  
> }  
> code.close
> 
> present your .rb file to replace “Song.rb” and copy code with line  
> number from your Console or SciTE

thanks…i thought about doing something like this but it’s a last  
desperate option - if i make any changes i need to run this again on the  
file for example. There must be a text editor out there that does this?  
(and knows how to colour ruby) Anyone know?

don’t meant to sound ungrateful though, thanks! 🙂

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/4 "2007-09-12T17:09:54Z")

</div>

On 9/12/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m writing up my project report and want to include line numbers with  
> the code that i paste into my word doc writeup. Does anyone know of a  
> ruby editor for windows that will allow this? I use SciTE and Eclipse  
> already but can’t see how to do it using those, so if someone knows a  
> way for those that would be ideal.

I don’t know SciTE or Eclipse, but with vim it would be trivial to  
write a macro to pipe the data through cat -n or nl (you can get these  
for Win32), copy the data to the clipboard, and then remove the line  
numbers.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/5 "2007-09-12T17:07:52Z")

</div>

On Sep 12, 2007, at 9:54 AM, Max W. wrote:

> There must be a text editor out there that does this?  
> (and knows how to colour ruby) Anyone know?

TextMate can:

[http://macromates.com/](http://macromates.com/)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/6 "2007-09-12T17:11:37Z")

</div>

James G. wrote:

> On Sep 12, 2007, at 9:54 AM, Max W. wrote:
> 
> > There must be a text editor out there that does this?  
> > (and knows how to colour ruby) Anyone know?
> 
> TextMate can:
> 
> [http://macromates.com/](http://macromates.com/)
> 
> James Edward G. II

i know ☹

On Sep 12, 2007, at 9:01 AM, Max W. wrote:

> Does anyone know of a ruby editor for windows that will allow this?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/7 "2007-09-12T17:57:14Z")

</div>

James! He’s using Scite, I doubt he has access to TextMate  
(unfortunately)  
You might try E-Texteditor, a rough clone of Textmate for Windows.  
It’s rough though, I can’t believe they charge for it in its current  
state. It uses TM Bundles but doesn’t come close in terms of grace,  
beauty or ease of use.  
Your next option would be to use some form of Emacs. Steep learning  
curve but can make you very productive.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/8 "2007-09-12T18:08:16Z")

</div>

On Sep 12, 2007, at 10:55 AM, John J. wrote:

> James! He’s using Scite, I doubt he has access to TextMate  
> (unfortunately)

I know. But when he asked if anyone knew of an editor… 😉

To be semi-helpful, this should be easy enough to setup in vi or  
emacs. Both are available on Windows.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 13, 2007, 11:43am UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/9 "2007-09-13T11:43:33Z")

</div>

> I’m writing up my project report and want to include line numbers with  
> the code that i paste into my word doc writeup. Does anyone know of a  
> ruby editor for windows that will allow this?

Not directly, but here are a few ideas:

- jEdit ([http://www.jedit.org/](http://www.jedit.org/)) is a programmable editor (programmable  
in  
BeanShell-Java and Python), and there are many extensions available. For  
example  
[http://taat.pl/en/jedit/#macros](http://taat.pl/en/jedit/#macros) lists a macro Lines\_to\_numbered.bsh  
which  
is supposed (I haven’t tried it) to write line numbers in front of each  
line. You call the macro, copy the buffer, and undo the changes.  
Actually, if you don’t mind diving into BeanShell programming, you could  
write your own macro doing this.

- Notepad++ is similar in this respect. Here you can use the plugin  
“TextFX Tools”, which has a command “number lines”, which puts line  
numbers in front of the selected text, though in just one standard  
format (with leading zeroes), and I don’t know how easy it is to change  
this. Again, you can copy the numbered file to the clipboard and remove  
the line numbers with Control-Z afterwards. You can download it from  
[Notepad++](http://notepad-plus.sourceforge.net/de/site.htm)

- You could put the line numbering process outside of the editor, either  
by writing a trivial Ruby program which creates a file with numbers out  
of a text file, or using some of the utilities which are already  
available  
(I use, for example, Cygwin, which has the nl command for numbering the  
lines). Many editors allow running an external program on the buffer -  
examples are (again) jEdit, but also the vi family and, AFIK, Scite too.

HTH

Ronald

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/10 "2007-09-12T18:45:44Z")

</div>

James G. wrote:

> On Sep 12, 2007, at 10:55 AM, John J. wrote:
> 
> > James! He’s using Scite, I doubt he has access to TextMate  
> > (unfortunately)
> 
> I know. But when he asked if anyone knew of an editor… 😉
> 
> To be semi-helpful, this should be easy enough to setup in vi or  
> emacs. Both are available on Windows.
> 
> James Edward G. II

Feels like a sledgehammer to crack a nut to be honest…I tried  
e-texteditor, it doesn’t look like it does it either. Must say i’m  
surprised that there don’t seem to be _any_ windows editors that will do  
this. oh well, thanks for the help guys.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 13, 2007, 11:47am UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/11 "2007-09-13T11:47:33Z")

</div>

Ronald F. wrote:

> - Notepad++ is similar in this respect. Here you can use the plugin  
> “TextFX Tools”, which has a command “number lines”, which puts line  
> numbers in front of the selected text, though in just one standard  
> format (with leading zeroes), and I don’t know how easy it is to change  
> this. Again, you can copy the numbered file to the clipboard and remove  
> the line numbers with Control-Z afterwards. You can download it from  
> [Notepad++](http://notepad-plus.sourceforge.net/de/site.htm)
> 
> - You could put the line numbering process outside of the editor, either  
> by writing a trivial Ruby program which creates a file with numbers out  
> of a text file, or using some of the utilities which are already  
> available  
> (I use, for example, Cygwin, which has the nl command for numbering the  
> lines). Many editors allow running an external program on the buffer -  
> examples are (again) jEdit, but also the vi family and, AFIK, Scite too.
> 
> HTH
> 
> Ronald

Thanks Ronald, i’ll look at Notepad++. Currently, i’m doing the  
following, which actually works quite well -

Set up a column in excel with ‘001’, ‘002’, etc.  
Paste text from eclipse into the next column in excel.  
Copy both columns back into a dummy file in eclipse (the apostrophes in  
the numbers are necessary for eclipse to colour code correctly here)  
Then paste the required chunks of code out of eclipse into word -  
colouring all nicely preserved.

thanks for the advice everyone. i’m still surprised that this isn’t a  
standard feature of editors though!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 13, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/copy-paste-line-numbers-with-code/114424/12 "2007-09-13T14:25:13Z")

</div>

> Thanks Ronald, i’ll look at Notepad++.

Good idea. It’s really a good editor and it does syntax-colour  
Ruby (just mention this so this thread won’t go _completely_ OT 😉

BTW, I gave you by mistake the URL of the German Notepad++ site.  
The English one is, as you maybe already found out by yourself,  
[http://notepad-plus.sourceforge.net/uk/site.htm](http://notepad-plus.sourceforge.net/uk/site.htm)

> thanks for the advice everyone. i’m still surprised that  
> this isn’t a  
> standard feature of editors though!

Probably it is not used so often 😉

BTW, I’m not proficient in Word, but since it is also has kind  
of a programming language built into (some sort of BASIC), maybe  
someone already wrote a Macroprogramm for this - did you check  
that already?

Ronald
