Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.editors > #106925

Re: Vim - serial numbering of text items

From Eli the Bearded <*@eli.users.panix.com>
Newsgroups comp.editors
Subject Re: Vim - serial numbering of text items
Date 2026-07-07 21:55 +0000
Organization Some absurd concept
Message-ID <eli$2607071754@qaz.wtf> (permalink)
References <112igko$2n4ld$1@dont-email.me>

Show all headers | View raw


In comp.editors, Janis Papanagnou  <janis_papanagnou+ng@hotmail.com> wrote:
> Is there in Vim a built-in simple (=non-scripting) way to automatically
> number text entities by serial numbers; e.g. with an input of either
> 
> Lorem ipsum
> Lorem ipsum
> Lorem ipsum
> 
> or
> 
> Lorem # ipsum
> Lorem # ipsum
> Lorem # ipsum
> 
> to create (for example)
> 
> Lorem 1 ipsum
> Lorem 2 ipsum
> Lorem 3 ipsum

"Simple"? Probably not. It can be done. I've written tail recusive
macros to do things like this, grabbing a number to replace it on the
next line and then incrementing with ctrl-a, but recursive macros are
fraught with special cases to consider.

> I've thought about something like Ctrl-v to mark a column and then have
> some command to create the serial numbers, but while I can to
> 
>    Ctrl-v !seq 1 3
> 
> that command will, unfortunately, replace the whole lines' contents by
> the number sequence (and not only the marked column).

If you !nl instead you can number the lines and then move the number.
Using Gnu nl:

  !nl -bp'\#' -w1 -s: -d ''

Number lines with a # in them, use minimum number width of 1, put a :
after the number, disable page break number rules.

Then move numbers with something like:

:g/^[1-9][0-9]*:.*#/ s,^\([0-9]*\):\([^#]*\)#,\2\1,

> I can also work around it by a couple manual commands but, as said, I'm
> looking for something simple, more straightforward.

I would use a recursive macro for one large file, always formatted the
same, the two part nl and move for vi instead of vim or when formatting
makes a macro awkward, and a separate script (probably in perl) for a
regular operation.

> [*] E.g. with Awk:  sub(/#/,++c)

Not everything needs to be done by the editor.

Elijah
------
has never touched vimscript but that may have a way to do it

Back to comp.editors | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Vim - serial numbering of text items Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-07-07 11:24 +0200
  Re: Vim - serial numbering of text items Eli the Bearded <*@eli.users.panix.com> - 2026-07-07 21:55 +0000
    Re: Vim - serial numbering of text items Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-07-08 02:50 +0000
      Re: Vim - serial numbering of text items Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-07-08 07:02 +0200
    Re: Vim - serial numbering of text items Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-07-08 07:34 +0200
      Re: Vim - serial numbering of text items Eli the Bearded <*@eli.users.panix.com> - 2026-07-08 06:05 +0000
  Re: Vim - serial numbering of text items Eric Pozharski <apple.universe@posteo.net> - 2026-07-08 17:22 +0000
    Re: Vim - serial numbering of text items Eli the Bearded <*@eli.users.panix.com> - 2026-07-08 21:17 +0000
      Re: Vim - serial numbering of text items Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-07-09 03:36 +0200
        Re: Vim - serial numbering of text items Eric Pozharski <apple.universe@posteo.net> - 2026-07-09 19:02 +0000
        Re: Vim - serial numbering of text items Lumin Etherlight <lumin+usenet@etherlight.link> - 2026-07-10 02:15 +0300
          Re: Vim - serial numbering of text items Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-07-10 08:32 +0200
          Re: Vim - serial numbering of text items Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-07-10 06:45 +0000
            Re: Vim - serial numbering of text items Lumin Etherlight <lumin+usenet@etherlight.link> - 2026-07-11 03:23 +0300
              Re: Vim - serial numbering of text items Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-07-11 22:04 +0000
      Re: Vim - serial numbering of text items Eric Pozharski <apple.universe@posteo.net> - 2026-07-09 18:55 +0000

csiph-web