How to insert incremental numbers?

71 views
Skip to first unread message

Nate

unread,
Dec 16, 2007, 3:33:46 PM12/16/07
to vim_use
Hello,

I would like to know how to insert a numbered list at any column.
UltraEdit for Windows has an easy way to do this. You go to "column"
mode and select the columns, then say "Insert Numbered List".

What's an easy way to do this in Vim?

For example, I have a list of rows:

A X Fred
B X Barney
C X Wilma

And I want to replace the "X" on each line with 1 on the first line, 2
on the second, etc. so that I have the resulting list:

A 1 Fred
B 2 Barney
C 3 Wilma

Can I do this easily in Vim?

Thanks,

--Nate

Nate

unread,
Dec 16, 2007, 5:46:36 PM12/16/07
to vim_use
I did some more investigation, and I have found the following scripts
that I will investigate:
http://www.vim.org/scripts/script.php?script_id=156
http://www.vim.org/scripts/script.php?script_id=842

If anyone has a different way, please let me know.

--Nate

John Little

unread,
Dec 16, 2007, 5:56:44 PM12/16/07
to vim...@googlegroups.com
Hi

> If anyone has a different way, please let me know.

While not simple, the following idiom is very flexible:

:let i = 1
:g/X/s//\=i/|let i+=1

Usually I'll do a search first, to check I've got the right places.

HTH, John

Nate

unread,
Dec 16, 2007, 6:15:40 PM12/16/07
to vim_use
> While not simple, the following idiom is very flexible:
>
> :let i = 1
> :g/X/s//\=i/|let i+=1

Thanks for the tip. I like that rather than downloading vim scripts.

Thanks,

--Nate

Nate

unread,
Dec 16, 2007, 6:26:29 PM12/16/07
to vim_use
John,

I found that this doesn't work. I'm running Vim 7.0 on OSX.

If I have a file with

X Fred
X Barney
X Wilma

And I execute

:let i = 1
:g/X/s//\=i/|let i+=1

I get the following text:

1 Fred
1 Barney
1 Wilma

I would like it to be

1 Fred
2 Barney
3 Wilma

Any ideas? Seems like the i+=1 is not being executed.

Thanks,

--Nate

Bill McCarthy

unread,
Dec 16, 2007, 6:54:07 PM12/16/07
to Nate
On Sun 16-Dec-07 5:26pm -0600, Nate wrote:

> I found that this doesn't work. I'm running Vim 7.0 on OSX.
>
> If I have a file with
>
> X Fred
> X Barney
> X Wilma
>
> And I execute
>
> :let i = 1
> :g/X/s//\=i/|let i+=1
>
> I get the following text:
>
> 1 Fred
> 1 Barney
> 1 Wilma

Not here - John's approach worked fine in both 7.0.243 and
7.1.175 on Windows XP (perhaps OSX is the problem).

In both cases I started with:

gvim -u NONE -i NONE -N

so standard gvim in non-compatibility mode with standard
options was used (and my viminfo file was not impacted).

--
Best regards,
Bill

Cyril Slobin

unread,
Dec 16, 2007, 7:00:46 PM12/16/07
to vim...@googlegroups.com
On 12/17/07, Nate <Natha...@gmail.com> wrote:

> I found that this doesn't work. I'm running Vim 7.0 on OSX.

Works well with both gvim 7.1.130 on Windows XP and vim 7.1.148
on Debian. So you problem is either 7.0-specific or OSX-specific.

--
Cyril Slobin <slo...@ice.ru> `When I use a word,' Humpty Dumpty said,
http://wagner.pp.ru/~slobin/ `it means just what I choose it to mean'

John Little

unread,
Dec 17, 2007, 3:35:10 AM12/17/07
to vim...@googlegroups.com
> On 12/17/07, Nate <Natha...@gmail.com> wrote:
>
> > I found that this doesn't work. I'm running Vim 7.0 on OSX.
>

I suspect you left out a slash, the one just before the bar.

HTH, John

rampion

unread,
Dec 17, 2007, 12:58:33 PM12/17/07
to vim_use
Works on OSX 10.4, with vim 7.0

I did do this wrong when I tried it initially, though.

:let i = 1
:'<,'>s/x/\=i/|let i += 1

produced

1 Fred
1 Wilma
1 Barney

with i set as 2 afterwards.

:let i = i
:g/X/s//\=i/|let i += 1

worked as expected though

1 Fred
2 Wilma
3 Barney

with i set as 4 afterwards.

Maybe you didn't use g// when you tried it?

On Dec 17, 3:35 am, "John Little" <john.b.lit...@gmail.com> wrote:

Charles E Campbell Jr

unread,
Dec 18, 2007, 12:14:22 PM12/18/07
to vim...@googlegroups.com
Nate wrote:

>I did some more investigation, and I have found the following scripts
>that I will investigate:
>http://www.vim.org/scripts/script.php?script_id=156
>http://www.vim.org/scripts/script.php?script_id=842
>
>If anyone has a different way, please let me know.
>
>

Well, you missed visincr.vim; 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

So, if you have something like

abc[1]
abc[1]
abc[1]
abc[1]

use ctrl-v and motion to highlight the 1s, press :I -- you'll end up
with an incremented column (1-2-3-4). Visincr also supports, as you can
see from the "Quick overview", incrementing with hex, octal, roman
numerals, dates, alphameric lists, etc.

Regards,
Chip Campbell


Gary Johnson

unread,
Dec 18, 2007, 3:19:58 PM12/18/07
to vim...@googlegroups.com

This doesn't seem to work for me. I downloaded and installed the
latest stable version from vim.sf.net. I then created the following
lines in an otherwise empty buffer,

xxxxx 1 yyyyy
xxxxx 1 yyyyy
xxxxx 1 yyyyy
xxxxx 1 yyyyy
xxxxx 1 yyyyy

moved the cursor to the first 1, typed Ctrl-v followed by 4 j's to
select the column of 1's, then typed :I and Enter. The result was
this:

1xxxx 1 yyyyy
2xxxx 1 yyyyy
3xxxx 1 yyyyy
4xxxx 1 yyyyy
5xxxx 1 yyyyy

I got the same results using vim 7.1.148 on Solaris and Linux and
vim 7.1 168 on Cygwin/WindowsXP. I thought it might be a plugin
interaction, so I started vim (on Solaris) as

vim -N -u NONE

then executed

:runtime plugin/visincr.vim

and repeated the experiment. This time I got the error message,

E492: Not an editor command: '<,'>I

The output of :scriptnames is:

1: /home/garyjohn/.vim/plugin/visincr.vim
2: /home/garyjohn/.vim/autoload/visincr.vim

Suggestions?

Regards,
Gary

Charles E Campbell Jr

unread,
Dec 18, 2007, 4:21:22 PM12/18/07
to vim...@googlegroups.com
Gary Johnson wrote:

Visincr is an autoload style plugin; ie. there's a
plugin/visincrPlugin.vim and there's an autoload/visincr.vim. The
plugin/visincrPlugin.vim is a small plugin, quickly loaded; the autoload
part contains the major portion of the script and is only loaded on demand.

Using vim -N -u NONE
:runtime plugin/visincrPlugin.vim

and then doing the ctrl-v, etc yielded the expected

xxxxx 1 yyyyy
xxxxx 2 yyyyy
xxxxx 3 yyyyy
xxxxx 4 yyyyy
xxxxx 5 yyyyy

Regards,
Chip Campbell

(I did the above using a linux system)

Gary Johnson

unread,
Dec 18, 2007, 4:42:49 PM12/18/07
to vim...@googlegroups.com

Thanks, Chip. That works for me, too. Now I just have to figure
out why it doesn't work in my normal configuration.

Regards,
Gary

Tony Mechelynck

unread,
Dec 18, 2007, 5:08:06 PM12/18/07
to vim...@googlegroups.com

My guess: if you have a plugin/visincr.vim (not plugin/visincrPlugin.vim and
not autoload/visincr.vim) it may be an older version. Try removing it (delete
it or move it elsewhere).


Best regards,
Tony.
--
Remember, drive defensively! And of course, the best defense is a good
offense!

Gary Johnson

unread,
Dec 18, 2007, 6:15:06 PM12/18/07
to vim...@googlegroups.com
On 2007-12-18, Tony Mechelynck <antoine.m...@gmail.com> wrote:
> Gary Johnson wrote:
> > On 2007-12-18, Charles E Campbell Jr <drc...@campbellfamily.biz> wrote:

> >> Using vim -N -u NONE
> >> :runtime plugin/visincrPlugin.vim
> >>
> >> and then doing the ctrl-v, etc yielded the expected
> >>
> >> xxxxx 1 yyyyy
> >> xxxxx 2 yyyyy
> >> xxxxx 3 yyyyy
> >> xxxxx 4 yyyyy
> >> xxxxx 5 yyyyy
> >
> > Thanks, Chip. That works for me, too. Now I just have to figure
> > out why it doesn't work in my normal configuration.

> My guess: if you have a plugin/visincr.vim (not plugin/visincrPlugin.vim and

> not autoload/visincr.vim) it may be an older version. Try removing it (delete
> it or move it elsewhere).

Thanks, Tony. I though of that, but I just checked visincr.vba and
the first file in it is plugin/visincr.vim. Further, I see the
following timestamps on the relevant files:

$ cd ~/.vim
$ find . -name \*visincr\* -exec ls -l {} \;
-rw-rw-r-- 1 garyjohn fw 19069 Dec 18 11:56 ./doc/visincr.txt
-rw-rw-r-- 1 garyjohn fw 26019 Dec 18 11:56 ./plugin/visincr.vim
-rw-rw-r-- 1 garyjohn fw 3174 Dec 18 11:56 ./plugin/visincrPlugin.vim
-rw-rw-r-- 1 garyjohn fw 26019 Dec 18 11:56 ./autoload/visincr.vim
-rw-rw-r-- 1 garyjohn fw 74469 Dec 18 11:49 ./visincr.vba

I had that problem with the netrw plugin, but it doesn't seem to be
the problem here.

Regards,
Gary

Charles E Campbell Jr

unread,
Dec 19, 2007, 8:54:19 AM12/19/07
to vim...@googlegroups.com
Gary Johnson wrote:

>On 2007-12-18, Tony Mechelynck <antoine.m...@gmail.com> wrote:
>
>
>>My guess: if you have a plugin/visincr.vim (not plugin/visincrPlugin.vim and
>>not autoload/visincr.vim) it may be an older version. Try removing it (delete
>>it or move it elsewhere).
>>
>>
>
>Thanks, Tony. I though of that, but I just checked visincr.vba and
>the first file in it is plugin/visincr.vim. Further, I see the
>following timestamps on the relevant files:
>
>$ cd ~/.vim
>$ find . -name \*visincr\* -exec ls -l {} \;
>-rw-rw-r-- 1 garyjohn fw 19069 Dec 18 11:56 ./doc/visincr.txt
>-rw-rw-r-- 1 garyjohn fw 26019 Dec 18 11:56 ./plugin/visincr.vim
>-rw-rw-r-- 1 garyjohn fw 3174 Dec 18 11:56 ./plugin/visincrPlugin.vim
>-rw-rw-r-- 1 garyjohn fw 26019 Dec 18 11:56 ./autoload/visincr.vim
>-rw-rw-r-- 1 garyjohn fw 74469 Dec 18 11:49 ./visincr.vba
>
>I had that problem with the netrw plugin, but it doesn't seem to be
>the problem here.
>
>

Hmm, there should not be a plugin/visincr.vim file -- so, I've uploaded
v18 to vim.sf.net. Has some more features, too.

Hope that helps!
Chip Campbell

Gary Johnson

unread,
Dec 19, 2007, 12:30:10 PM12/19/07
to vim...@googlegroups.com

Thanks. I removed all the *visincr* files from my ~/.vim hierarchy
and downloaded and installed the latest visincr.vba.gz from
vim.sf.net. No plugin/visincr.vim this time. However, the behavior
is the same: works fine with "vim -u NONE" but not with my normal
configuration. I'll experiment more as time permits.

Regards,
Gary

Gary Johnson

unread,
Dec 20, 2007, 2:44:07 AM12/20/07
to vim...@googlegroups.com

I found the setting that causes the problem, but I don't know why it
causes the problem.

set cedit=<Esc>

When I remove that line from my .vimrc, :I on a visually selected
column of 1's works fine. If I then make that setting, :I puts all
the numbers in column 1, as described previously.

Regards,
Gary

Charles E Campbell Jr

unread,
Dec 20, 2007, 8:57:21 AM12/20/07
to vim...@googlegroups.com
Gary Johnson wrote:

Thank you for finding this -- I'll see what I can do to work around it,
plus include it in my pluginkiller script (I use it to try out a number
of options which have given my plugins problems in the past).

Regards,
Chip Campbell

Charles E Campbell Jr

unread,
Dec 20, 2007, 9:13:41 AM12/20/07
to vim...@googlegroups.com
Gary Johnson wrote:

Visincr v20a now does a bypass of cedit settings (save, set to a
standard, restore) and seems to handle "set cedit=<Esc>" now. You can
get the latest visincr from my website:
http://mysite.verizon.net/astronaut/vim/index.html#VISINCR . I've also
updated pluginkiller so that it can find that setting when it causes
trouble.

Regards,
Chip Campbell

Gary Johnson

unread,
Dec 20, 2007, 11:11:47 AM12/20/07
to vim...@googlegroups.com
On 2007-12-20, Charles E Campbell Jr <drc...@campbellfamily.biz> wrote:
> Gary Johnson wrote:

> >I found the setting that causes the problem, but I don't know why it
> >causes the problem.
> >
> > set cedit=<Esc>
> >
> >When I remove that line from my .vimrc, :I on a visually selected
> >column of 1's works fine. If I then make that setting, :I puts all
> >the numbers in column 1, as described previously.
> >
> >
> Visincr v20a now does a bypass of cedit settings (save, set to a
> standard, restore) and seems to handle "set cedit=<Esc>" now. You can
> get the latest visincr from my website:
> http://mysite.verizon.net/astronaut/vim/index.html#VISINCR .

That fixed it! Thanks very much.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages