Saturday, January 21, 2012

Re: Prefix lines with a counter

On Sat, Jan 21, 2012 at 12:05:18PM -0600, Tim Chase wrote:
> On 01/21/12 10:49, Chris Lott wrote:
> >Start with a list of items delineated by line breaks:
> >
> >foo
> >bar
> >baz
> >
> >And turn it into a markdown numbered list:
> >
> >1. foo
> >2. bar
> >3. baz
> >
> >I know that I *could* use 1. for every item, since that's easy enough
> >to do and Markdown knows what to do when rendering, but that's not so
> >good for human readers of plain text.
> >
> >Is there a way I can have vim prefix the lines with an ascending
> >counter like the above?

> Dr. Chip's VisIncr.vim script may be what you want:

> http://www.vim.org/scripts/script.php?script_id=670

> You _can_ do things like

> :'<,'>s/^/\=(1+line('.')-line("'<")).'. '

> to number un-numbered lines (like you describe), or

> '<,'>s/^\(\d\+\. *\)\=/\=(1+line('.')-line("'<")).'. '

> to re-number lines that may (or may not) have already been numbered.

yet another way to approach this is to play around with the
ListItem function and mapping described in map.txt under

:h map-<expr>

it has the added advantage that you can add the numbers when and
where you want, eg. when an item spans multiple lines and you
don't want numbers on every line, AND you can add a function to
start the numbers with whatever number you like -- very flexible

sc

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