Wednesday, February 8, 2012

Re: 'command-list' in 'Q' and 'gQ' ex-mode

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)

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

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

No comments: