Quantcast

:set nu -> how to copy lines with line numbers?

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

:set nu -> how to copy lines with line numbers?

krzysztof cierpisz

when I mark in visual mode, only the text itself (what do we need
normally) is marked.

sometimes I would like to select the interesting lines with the line
numbers from the left margin.

how to copy them along with the text?

thanks,
chris
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Andreas Bernauer-5

krzysztof cierpisz wrote:
> when I mark in visual mode, only the text itself (what do we need
> normally) is marked.
>
> sometimes I would like to select the interesting lines with the line
> numbers from the left margin.
>
> how to copy them along with the text?

One possibility: use :print (see :help :print)

Select lines you want to copy, say ':print' and select the lines with your
mouse, along with the line numbers.

--
Andreas.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Christian Brabandt
In reply to this post by krzysztof cierpisz

Hi krzysztof!

On Mo, 06 Apr 2009, krzysztof cierpisz wrote:

> sometimes I would like to select the interesting lines with the line
> numbers from the left margin.
>
> how to copy them along with the text?

You have several alternatives. First you could use the TOhtml command
to let vim generate a html file, which includes the line numbers, if I
am not mistaken. You could then copy and paste from that html file.

Another possibility would be to replace each line by its line number
plus it's content, so you could copy it with the line number included.  
This can be done using the following substitute command:
:s/^/\=printf("%2d ", line('.'))/
You obviously want to undo this replace after copying using e.g. undo
or :s/^\d\+ //

Or you create your own command to take care of copying the desired
text together with the line numbers. Something like this should work:

fu! YankNu(first, last)
        let start=a:first
        let l=[]
        while start <= a:last
                let l = add(l,printf("%".len(a:last) . "d %s",start , " " . getline(start)))
                let start+=1
        endwhile
        return join(l,"\n") . "\n"
endfu

com! -nargs=0 -range Yank :let @+=YankNu(<line1>,<line2>)

Now select your text and enter :Yank
The function then copies the range into your +-register (which should
correspond to your System's clipboard, if your vim was compiled with
clipboard functionality [:help should output +clipboard, instead of
-clipboard] and can access it)


regards,
Christian
--
:wq!

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

krzysztof cierpisz


>
> fu! YankNu(first, last)
>         let start=a:first
>         let l=[]
>         while start <= a:last
>                 let l = add(l,printf("%".len(a:last) . "d %s",start , " " . getline(start)))
>                 let start+=1
>         endwhile
>         return join(l,"\n") . "\n"
> endfu
>
> com! -nargs=0 -range Yank :let @+=YankNu(<line1>,<line2>)
>

the function works great,

btw. I use 2html but this one is quicker, when I do not need the
beautiful highlighting

thanks,
chris
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Matt Wozniski-2
In reply to this post by Andreas Bernauer-5

On Mon, Apr 6, 2009 at 10:31 AM, Andreas Bernauer wrote:
>
> One possibility: use :print (see :help :print)

That's clever... definitely what I'll be doing from now on in gvim.
FWIW, in terminal vim, you can just hold down "shift" while
clicking-and-dragging to override "set mouse" and let the terminal
emulator handle the mouse, so line numbers will get selected.

~Matt

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Ken Bloom-2
In reply to this post by krzysztof cierpisz

On Mon, 06 Apr 2009 06:13:23 -0700, krzysztof cierpisz wrote:

> when I mark in visual mode, only the text itself (what do we need
> normally) is marked.
>
> sometimes I would like to select the interesting lines with the line
> numbers from the left margin.
>
> how to copy them along with the text?

Open the file with console vim in a terminal, :set nu, and use the mouse.

--Ken

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Matt Wozniski-2

On Mon, Apr 6, 2009 at 7:32 PM, Ken Bloom wrote:

>
> On Mon, 06 Apr 2009 06:13:23 -0700, krzysztof cierpisz wrote:
>
>> when I mark in visual mode, only the text itself (what do we need
>> normally) is marked.
>>
>> sometimes I would like to select the interesting lines with the line
>> numbers from the left margin.
>>
>> how to copy them along with the text?
>
> Open the file with console vim in a terminal, :set nu, and use the mouse.

Unless you use ":set mouse=n" or ":set mouse=a", in which case you
need to use shift-click-and-drag instead of click-and-drag to force
xterm to handle the mouse instead of vim.  And, of course, that
doesn't play nicely with vertically split windows or the like - I
forgot to mention that in my last email.

~Matt

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Tim Chase
In reply to this post by krzysztof cierpisz

> when I mark in visual mode, only the text itself (what do we need
> normally) is marked.
>
> sometimes I would like to select the interesting lines with the line
> numbers from the left margin.
>
> how to copy them along with the text?

You can use

   :redir @a
   :sil! '<,'>#
   :redir END

to pull the numbered lines into register "a".  This can be
wrapped into a mapping:

  :vnoremap <f4> :<home>redir @a<bar>sil! <end>#<bar>redir END<cr>

assuming you always want to tromp the same register.  If you use
it frequently, you might designate a preferred register such as
"n" for "(n)umbered" and then only use it for that purpose.
Alternatively, :redir allows you to redirect to other sinks if
you prefer.

-tim





--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Paul Nguyen
In reply to this post by krzysztof cierpisz
hi, I thought I'd contribute my 2 cents.  I've used VIM for umpteen years, and have found that if the solution to a VIM issue is too complex, I  might use it once or twice but then forget it.  I've developed many scripts, plugins, and methods, only to find that...........they become forgotten after some time if I don't use them regularly.

Alas, I have the answer to your question, and stumbled on it by accident:

On my Windows VIM GUI, :set nu, and then hold down Ctrl-Shift while highlighting the desired text with the mouse.  This yanks the line numbers into the buffer.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: :set nu -> how to copy lines with line numbers?

John Beckett-2
Paul Nguyen wrote:
> On my Windows VIM GUI, :set nu, and then hold down Ctrl-Shift
> while highlighting the desired text with the mouse.  This
> yanks the line numbers into the buffer.

:help gui-mouse-modeless

Never heard of that, although it turns out that I do use it when
output is displayed. For example, after :g/pattern/ in gvim it
is possible to drag the mouse to select the output (no need for
Ctrl or Shift) then press Ctrl-Y to copy the text to the
clipboard.

In a Vim window, Ctrl-Shift is required, but then you have to
get into the command line (:) and press Ctrl-Y.

John

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Bee-16
Tim Chase wrote about using:

  :redir @a
  :sil! '<,'>#
  :redir END

:help :#
:help :number

I added that to my vim notes file.
It has another advantage,
I normally have relative line numbers on.
Using '<,'># prints the lines with absolute numbers.

I use the + register (if your vim is compiled with +clipboard)
so I can simply 'paste' into an email.

:redir @+ | sil! '<,'># | redir END

Bill

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: :set nu -> how to copy lines with line numbers?

Gilson Filho
With file generated with :TOHtml not is good?

--
_________________________________________________
Gilson Filho
Desenvolvedor Web
http://gilsondev.com

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Loading...