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
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).
I can also work around it by a couple manual commands but, as said, I'm looking for something simple, more straightforward.
[*] E.g. with Awk: sub(/#/,++c)
"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.
On Tue, 7 Jul 2026 21:55:02 -0000 (UTC), Eli the Bearded wrote:
"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.
IsnrCOt there some version of Vim that uses Lua as its extension
language?
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
[...]
"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
On 2026-07-07 23:55, Eli the Bearded wrote:
I would use a recursive macro for one large file, always formatted the(I didn't understand your preference for Vi here, "instead of Vim".)
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.
Is there in Vim a built-in simple (=non-scripting) way to*SKIP* [ 4 lines 1 level deep]
automatically number text entities by serial numbers; e.g. with an
input of either
Lorem # ipsum
Lorem # ipsum
Lorem # ipsum
to create (for example)
Lorem 1 ipsum
Lorem 2 ipsum
Lorem 3 ipsum
I can also work around it by a couple manual commands but, as said, I'm looking for something simple, more straightforward.
:%s, # ,\=" " . line(".") . " "
You owe me 10min.
p.s. Also, vimBASIC isa bitch.
In comp.editors, Eric Pozharski <apple.universe@posteo.net> wrote:
:%s, # ,\=" " . line(".") . " "
You owe me 10min.
p.s. Also, vimBASIC isa bitch.
What if # should start at 1 even if the first "Lorem # ipsum" does not
appear until line 20?
Elijah
------
did not know you could trigger scripting from a substitute command
In comp.editors, Eric Pozharski <apple.universe@posteo.net> wrote:*SKIP* [ 1 line 0 level deep]
:%s, # ,\=" " . line(".") . " "
You owe me 10min.
What if # should start at 1 even if the first "Lorem # ipsum" does not
appear until line 20?
On 2026-07-08 23:17, Eli the Bearded wrote:
In comp.editors, Eric Pozharski <apple.universe@posteo.net> wrote:
???:%s, # ,\=" " . line(".") . " "
You owe me 10min.
???p.s. Also, vimBASIC isa bitch.
Meanwhile, since the external command approach was
so easy, and the external command so trivially
short, I slightly extended my original Awk code -
sub(/#/,++c) - and have now
Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
writes:
Meanwhile, since the external command approach was
so easy, and the external command so trivially
short, I slightly extended my original Awk code -
sub(/#/,++c) - and have now
What follows is not a new answer to the OP's
original question, just some random curiosities.
May I first note that the approach of piping to an
external command is not a hack nor a kludge, but a
core idea of using a UNIX computing environment.
I personally do enjoy it, very much, and it's why
an ed-like editor, or at most classic vi, is all I
usually need to do all my programming and writing
work. UNIX is the IDE. In this specific use case
I do think piping to an awk script is a reasonable
and elegant solution.
[...] For example:
~/.edit/nl
#!/bin/sh
# replace the string passed
# as arg1 with counter numbers.
awk "sub(/$1/,++c)"
then:
chmod +x ~/.edit/nl
then use a shell alias to call your editor, with
the added PATH directory, possibly putting this in
your ~/.bashrc file:
alias vi='PATH="$HOME/.edit/:$PATH" vi'
And now, in vi, you can just run:
:'<,'>!nl '\#'
or
:'<,'>!nl NUM
replacing # or NUM with numbers. As long as you
stick with POSIX tools, then your custom edit
scripts are going to be very portable, and no need
to worry about updates or management or complexity
of dozens of vim scripts and plugins. POSIX tools
tend to be very stable over time, you might go a
decade before a minor breaking change happens here
or there. And to have them on another machine is
a simple matter of scp for the .edit directory, or
even just typing the commands in yourself if you
don't need them frequently.
I have little scripts [...]
Emacs has a feature I enjoy, [...]
Just some thoughts,
May I first note that the approach of piping to an external command
is not a hack nor a kludge, but a core idea of using a UNIX
computing environment. I personally do enjoy it, very much, and it's
why an ed-like editor, or at most classic vi, is all I usually need
to do all my programming and writing work. UNIX is the IDE.
On Fri, 10 Jul 2026 02:15:33 +0300, Lumin Etherlight wrote:
May I first note that the approach of piping to
an external command is not a hack nor a kludge,
but a core idea of using a UNIX computing
environment. I personally do enjoy it, very
much, and it's why an ed-like editor, or at most
classic vi, is all I usually need to do all my
programming and writing work. UNIX is the IDE.
True enough. But there is a right way and a wrong way to do it.
The wrong way is to spawn a shell which then executes a command
string. This requires the command string to have any shell specials
properly escaped, which is a complication you can really do without,
and is often mishandled by lazy programmers.
The right way is to spawn the external command directly, and feed it
an array of command-line arguments, that donrCOt need any escaping of
shell specials, because there is no shell intermediary involved.
Lawrence DrCOOliveiro <ldo@nz.invalid> writes:
The wrong way is to spawn a shell which then executes a command
string. This requires the command string to have any shell specials
properly escaped, which is a complication you can really do
without, and is often mishandled by lazy programmers.
The right way is to spawn the external command directly, and feed
it an array of command-line arguments, that donrCOt need any escaping
of shell specials, because there is no shell intermediary involved.
Why is it the wrong way? I understand there are caveats to it, but
there are also advantages. I prefer the spawning of an intermediary
shell in such cases, as it allows me to use shell features, such as
various shell expansions, especially shell variables, plus allowing
easy redirect of content to temporary files, or reading and
filtering stuff from other files into my current file, and piping
the text from one command to another.
[rest of wall of text deleted]--- Synchronet 3.22a-Linux NewsLink 1.2
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 46:34:26 |
| Calls: | 1,100 |
| Files: | 1,339 |
| Messages: | 275,493 |