Only curiosity: Optimizing a vimtip (modified)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Only curiosity: Optimizing a vimtip (modified)

meino.cramer
Hi,

 I read of a vimtip, that one can move/copy lines of a text which
 match a cvertina pattern to line 0 (top) of the text.

 This is a nice trick to gather material for a kinda quick'n'dirty
 "Table of contents" it has one drwaback: The copied lines are in
 reversed order.

 Surely it is possible to write a fairly simple function with a
 counter, which keeps track to what line something is copied.

 But it would be interesting whether it is possible to achieve this
 with more "condensed" tricks without writing a function in
 beforehand.

 Thank you very much for any idea/hack/trick in advance!
 
 Keep hacking!
 mcc

Reply | Threaded
Open this post in threaded view
|

Re: Only curiosity: Optimizing a vimtip (modified)

A.J.Mechelynck
Meino Christian Cramer wrote:

> Hi,
>
>  I read of a vimtip, that one can move/copy lines of a text which
>  match a cvertina pattern to line 0 (top) of the text.
>
>  This is a nice trick to gather material for a kinda quick'n'dirty
>  "Table of contents" it has one drwaback: The copied lines are in
>  reversed order.
>
>  Surely it is possible to write a fairly simple function with a
>  counter, which keeps track to what line something is copied.
>
>  But it would be interesting whether it is possible to achieve this
>  with more "condensed" tricks without writing a function in
>  beforehand.
>
>  Thank you very much for any idea/hack/trick in advance!
>  
>  Keep hacking!
>  mcc
>
>

1. Place a mark on the first line. You have a choice of 26 names, I'll assume
t for "table of contents".

2. Copy your headings (e.g. with a :g command) to 't-1
(apostrophe-tee-minus-one, i.e., just above the marked line).

3. (Optional, and version 7 only) :delmark t


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Only curiosity: Optimizing a vimtip (modified)

Dave Roberts
In reply to this post by meino.cramer
Meino Christian Cramer wrote:

> Hi,
>
>  I read of a vimtip, that one can move/copy lines of a text which
>  match a cvertina pattern to line 0 (top) of the text.
>
>  This is a nice trick to gather material for a kinda quick'n'dirty
>  "Table of contents" it has one drwaback: The copied lines are in
>  reversed order.
>
>  Surely it is possible to write a fairly simple function with a
>  counter, which keeps track to what line something is copied.
>
>  But it would be interesting whether it is possible to achieve this
>  with more "condensed" tricks without writing a function in
>  beforehand.
>
>  Thank you very much for any idea/hack/trick in advance!
>  
>  Keep hacking!
>  mcc
>
>  

You could use the reverse lines trick to reorder the copied lines. Use
the following to put the lines at the top of the file:
:let cnt=0|g/(regexp)/copy 0|let cnt=cnt+1

Then re-reverse the copied lines at the top of the file:
:exe "1,".cnt."g/^/m0"

Easy enough to put that into a function passing the 'regexp' to it...

- Dave
Reply | Threaded
Open this post in threaded view
|

Re: Only curiosity: Optimizing a vimtip (modified)

Benji Fisher
On Tue, Oct 24, 2006 at 08:47:46AM -0400, Dave Roberts wrote:

> Meino Christian Cramer wrote:
> >Hi,
> >
> > I read of a vimtip, that one can move/copy lines of a text which
> > match a cvertina pattern to line 0 (top) of the text.
> >
> > This is a nice trick to gather material for a kinda quick'n'dirty
> > "Table of contents" it has one drwaback: The copied lines are in
> > reversed order.
> >
> > Surely it is possible to write a fairly simple function with a
> > counter, which keeps track to what line something is copied.
> >
> > But it would be interesting whether it is possible to achieve this
> > with more "condensed" tricks without writing a function in
> > beforehand.
> >
> > Thank you very much for any idea/hack/trick in advance!
> >
> > Keep hacking!
> > mcc
> >
> >  
>
> You could use the reverse lines trick to reorder the copied lines. Use
> the following to put the lines at the top of the file:
> :let cnt=0|g/(regexp)/copy 0|let cnt=cnt+1
>
> Then re-reverse the copied lines at the top of the file:
> :exe "1,".cnt."g/^/m0"
>
> Easy enough to put that into a function passing the 'regexp' to it...
>
> - Dave

     Another possibility is to copy all of the lines to the end of the
file (:copy $  instead of  :copy 0 ).  Then, having saved line("$")
before adding lines, you know how many lines to :move to the top.

<plug>
Some time ago, answering a similar question on this list, I wrote the
Pippo() function and added it to foo.vim, my file of example vim
functions:  http://www.vim.org/script.php?script_id=72
This is more of an index function than a table-of-contents function:  it
searches for "words" (matching a given regular expression) and appends
them to the end of the file, one per line.
</plug>

HTH --Benji Fisher