On Wed, Feb 08, 2012 at 07:13:20PM -0600, Tim Chase wrote:
> On 02/08/12 16:17, SamLT wrote:
> >my text looks like this:
> >---- snippet starting at line 13 ---
> >
> >AAAAA
> >
> >1
> >
> >BBB
> >
> >7
> >
> >CCCC
> >
> >11
> >
> >---- end of snippet(line 27) ---
> >
> >To give you an idea, I want to make a table of content which would look
> >like this
> >
> >    AAAAA.............1
> >    BBB...............7
> >    CCCC.............11
> 
> With minimal monkeying, I came up with something like
> 
>  :%s/^\(\w\+\)\_s*\(\d\+\)\_s*\ze\n/\=submatch(1).repeat('.',
> 20-(strlen(submatch(1))+strlen(submatch(2)))).submatch(2)
> 
> for whatever range you want (I just pasted in your text, so "%" was
> an adequate range).  Adjust the "20" for whatever target width you
> aim to hit.  If you have non-keyword characters in your title lines
> such as "AAA AAA", you'd have to tweak the regexp to snag those too,
> something like:
> 
> :%s/^\(\w\+\%(\s\+\w\+\)*\)\_s*\(\d\+\)\_s*\ze\n/\=submatch(1).repeat('.',
> 20-(strlen(submatch(1))+strlen(submatch(2)))).submatch(2)

mmmh, I guess I still have a lot to learn to be able throw something
like that out;)

for the time being, I just insert many dot (.), then I remove the ones
"in excess?", e.g.:

s/\d\+$/............................&/
then
s/^.\{60\}\zs\.*\(\d\+\)/\1/

> 
> Both simply use the expression-evaluation capabilities of :s to
> calculate the desired number of "." characters to insert between the
> 1st captured part of the match (the title) and the 2nd captured part
> of the match (the page number).
> 
>   :help sub-replace-special
>   :help repeat()
>   :help submatch()
> 
> >while in the 'gQ' mode the command:
> >84,96g/^[A-Z]/s/^/   /\
> >.,.+2j!\
> >s/\d\+$/.............&/
> >
> >gives me the following error:
> >"E488: Trailing characters"
> 
> I've always had difficulty with attempting multi-line commands
> passed to a ":g" command.  You can try it with vertical bars on one
> line instead:
> 
>   :g/^[A-Z]/s/^/   /|,+2j!|s/\d\+$/.........&/
> 
> which should work.
> 
> -tim
> 
> 
> 

On Wed, Feb 08, 2012 at 09:40:55PM -0800, Ben Fritz wrote:
> 
> 
> On Feb 8, 4:17 pm, SamLT <[email protected]> wrote:
> 
> > in 'Q' mode, the equivalent(_I_ think) command:
> > :13,27g/^[A-Z]/s/^/   /\
> > .,.+2j!\
> > s/\d\+$/.............&/
> >
> >
> > while in the 'gQ' mode the command:
> > 84,96g/^[A-Z]/s/^/   /\
> > .,.+2j!\
> > s/\d\+$/.............&/
> >
> > gives me the following error:
> > "E488: Trailing characters"
> >
> 
> Are you trying to string together multiple commands on multiple lines
> using a '\' at the end of the line? If so, that doesn't work in Vim.
> I'm don't think line continuation works in an interactive Vim command
> line, but in a script you put the '\' at the BEGINNING of the 2nd line
> of a continued line, instead of at the end of the first.
> 
> I think you want, as Tim suggests, to use an '|' instead of a '\' and
> a newline to separate your commands which are to be run together.


Thank you both, this is indeed what I needed. I thought the | was
handled the same way with :s and :g, but it's actually not the
case('confusing' as the :help :| says!).

Have a nice day/night:)


sam




> 
> -- 
> 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

-- 
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 via email to