renumbering lists automatically within vim

31 views
Skip to first unread message

Rahul

unread,
Jun 3, 2008, 3:55:14 PM6/3/08
to vim_use
Oftentimes I make a list, say of TODO for a code:

(1) blah blah
(2) more blah blah
(3) still more blah blah
(4)...
(5)...

Now I finish point (2) and delete the line from my list. I manually
renumber a long list for all points after (2). Does vim make this task
any easier?

--
Rahul

Benjamin Fritz

unread,
Jun 3, 2008, 4:01:27 PM6/3/08
to vim...@googlegroups.com

One very simple (though limited method) would be:

Visually select all the lines that require renumbering.
Enter the command, :'<,'>normal! <C-X>

where <C-X> is a literal CTRL-X character, entered by typing CTRL-V
CTRL-X (or CTRL-Q CTRL-X if CTRL-V is mapped to paste)

This will decrement the first number on each line you selected. CTRL-A
will increment.

See :help CTRL-X and :help CTRL-A

Charles E Campbell Jr

unread,
Jun 3, 2008, 3:55:38 PM6/3/08
to vim...@googlegroups.com
There's visincr...

You can get visincr from:

http://mysite.verizon.net/astronaut/vim/index.html#VISINCR
(cutting edge)
http://vim.sourceforge.net/scripts/script.php?script_id=670 (stable)

Quick overview:
:I [#] left justified incremented list
:II [# [zfill]] right justified incremented list
:IO [#] left justified octal incremented list
:IIO [# [zfill]] right justified octal incremented list
:IX [#] left justified hex. incremented lsit
:IIX [# [zfill]] right justified hex. incremented lsit
:IYMD [# [zfill]] year/month/day incremented list
:IMDY [# [zfill]] month/day/year incremented list
:IDMY [# [zfill]] day/month/year incremented list
:IA [#] alphameric incremented list
:ID [#] dayname incremented list
:IM [#] monthname incremented list


To install if you're using vim7.1:

1. Install a new version of visincr:
vim visincr.vba.gz
:so %
:q

Regards,
Chip Campbell

Tim Chase

unread,
Jun 3, 2008, 4:22:20 PM6/3/08
to vim...@googlegroups.com
> (1) blah blah
> (2) more blah blah
> (3) still more blah blah
> (4)...
> (5)...
>
> Now I finish point (2) and delete the line from my list. I manually
> renumber a long list for all points after (2). Does vim make this task
> any easier?

Abusing the "\=" replacement evaluation described at

:help sub-replace-special

you can do something like

:.,$s/(\zs\d\+\ze)/\=submatch(0)-1

which will decrement the contents of "(#)" from the current line
through the end of the file. You can tighten up the regexp or
range if you have nested items or other gotchas, e.g.

(1) one
(2) two
(3) three
(1) three-one
(2) three-two
(4) four

if you delete #2 and decrement from (3) through the end, you'll
end up with

(1) one
(2) three
(0) three-one
(1) three-two
(3) four

so you might want to tighten the regexp to something like

:,$s/^(\zs\d\+\ze)/\=submatch(0)-1

to only do those where the "(#)" come at the beginning of the line.

I think Dr. Chip's Vis.Vim script (or it might be a particular
"visincr.vim" script...I don't remember which) also supports
using control+A and control+X to increment/decrement a visual block.

-tim


Rahul

unread,
Jun 3, 2008, 5:37:44 PM6/3/08
to vim_use


On Jun 3, 3:22 pm, Tim Chase <v...@tim.thechases.com> wrote:

> I think Dr. Chip's Vis.Vim script (or it might be a particular
> "visincr.vim" script...I don't remember which) also supports
> using control+A and control+X to increment/decrement a visual block.

Thanks guys. It does seem a difficult problem then.

--
Rahul

Tim Chase

unread,
Jun 3, 2008, 6:49:02 PM6/3/08
to vim...@googlegroups.com
>> I think Dr. Chip's Vis.Vim script (or it might be a particular
>> "visincr.vim" script...I don't remember which) also supports
>> using control+A and control+X to increment/decrement a visual block.
>
> Thanks guys. It does seem a difficult problem then.

It's not so much a difficult problem as a nuanced problem that
has a number of different ways to go wrong depending on what you
want and what the starting conditions are. The visincr script
handles most of the fairly straight-forward cases nicely. My :s
commands using "\=" for the replacement expression were easily
tailored to exactly whatever you needed (need to add/subtract
more than 1? just change the "-1" to whatever you need). In this
case, the numbers were of the form "(#)", but in another case,
you might have "#)" or "[#]" so it's good to understand them so
you can adjust accordingly.

-tim

Bryan Ewbank

unread,
Jun 3, 2008, 10:21:51 PM6/3/08
to vim...@googlegroups.com
As a completely different solution, eliminate the numbers from the list - use dashes instead.  What value are the numbers adding?

I personally use '[_]' for todo items, and replace the _ with X when they are finished, but that's just me..
--Bryan
Reply all
Reply to author
Forward
0 new messages