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


Groups > alt.os.linux > #59208

Re: Adding line numbers to vi (or vim) file of data

From Guadalupe <Guadalupe@Guadalupe.com>
Newsgroups alt.os.linux
Subject Re: Adding line numbers to vi (or vim) file of data
Date 2019-10-07 13:08 -0400
Organization Aioe.org NNTP Server
Message-ID <qnfrde$jjt$1@gioia.aioe.org> (permalink)
References <qnd7jr$i69$1@gioia.aioe.org> <5d9b4af7$0$521$65785112@news.neostrada.pl>

Show all headers | View raw


 said thus to which I respond:

> https://vim.fandom.com/wiki/Insert_line_numbers
> (unless what you really want is the numbers to automatically change
> while you insert/delete lines.)

That's it!

 %s/^/\=printf('%-4d', line('.'))

For each line of the file [%]
Search for the beginning of the line [s/^]
And replace it with [/]
The evaluation of the following expression [\=]

Where the following expression is contained inside [(...)]
Print formatted a left-aligned decimal number padded to 4 columns [%-4d]
And then [, ]
The line itself [line('.')]

Where %-4d is a left-aligned decimal number padded with spaces
And where %4d is right aligned
And %04d inserts leading zeroes
And %d.\t inserts a left-aligned number followed by a period and tab

The article lists how to format a range of lines within the file
 :'<,'>s/^/\=printf("%d.\t", line(".") - line("'<") + 1)
Or, after running :let i = 1
 :'<,'>g/^\S/s/^/\=printf("%d.\t", Inc())

I will do it more consistently, just by adding "mark x" & "mark y".

First, I'd mark the top of the section to be numbered [mx]
Then, I'd mark the bottom of the section to be numbered [my]
Then I'd run the same command as before, from x to y, as shown below
 :'x,'ys/^/\=printf('%-4d', line('.'))
Where I'd just replace the whole file [%] with the section ['x,'y]

Thank you for showing how to add sequential line numbers inside of vi.

Back to alt.os.linux | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Adding line numbers to vi (or vim) file of data Guadalupe <Guadalupe@Guadalupe.com> - 2019-10-06 13:17 -0400
  Re: Adding line numbers to vi (or vim) file of data Paul <nospam@needed.invalid> - 2019-10-06 14:04 -0400
    Re: Adding line numbers to vi (or vim) file of data Guadalupe <Guadalupe@Guadalupe.com> - 2019-10-06 18:16 -0400
  Re: Adding line numbers to vi (or vim) file of data Chris Elvidge <chris@mshome.net> - 2019-10-06 19:58 +0100
  Re: Adding line numbers to vi (or vim) file of data Jasen Betts <jasen@xnet.co.nz> - 2019-10-07 08:12 +0000
    Re: Adding line numbers to vi (or vim) file of data gazelle@shell.xmission.com (Kenny McCormack) - 2019-10-07 12:32 +0000
      Re: Adding line numbers to vi (or vim) file of data Guadalupe <Guadalupe@Guadalupe.com> - 2019-10-07 09:56 -0400
        Re: Adding line numbers to vi (or vim) file of data gazelle@shell.xmission.com (Kenny McCormack) - 2019-10-07 14:31 +0000
  Re: Adding line numbers to vi (or vim) file of data Dan Purgert <dan@djph.net> - 2019-10-07 10:16 +0000
  Re: Adding line numbers to vi (or vim) file of data Real Troll <real.troll@trolls.com> - 2019-10-07 09:59 -0400
  Re: Adding line numbers to vi (or vim) file of data marrgol <marrgol@address.invalid> - 2019-10-07 16:25 +0200
    Re: Adding line numbers to vi (or vim) file of data Guadalupe <Guadalupe@Guadalupe.com> - 2019-10-07 13:08 -0400

csiph-web