someone have some code to number lines

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

someone have some code to number lines

Billy N. Patton
I need to number lines.
Ex:  I have

aaaa
bbbbb
cccc
dddd
...

I need to do
1 aaaa
2 bbbbb
3 cccc
4 dddd

or
4 aaaa
3 bbbbb
2 cccc
1 dddd


Need to supply a range (via Shift v)
The supply a numbering range 1-4 or 4-1
I have to do this constantly.  Most of the time it's between 16 and 64
lines.  I have files that contain only these nubers in a column but
would like a more automated method.

--
    ___  _ ____       ___       __  __
   / _ )(_) / /_ __  / _ \___ _/ /_/ /____  ___
  / _  / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/   \_,_/\__/\__/\___/_//_/
            /___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455,  [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: someone have some code to number lines

Tim Chase-2
> Need to supply a range (via Shift v)
> The supply a numbering range 1-4 or 4-1

The common way is to use the expression stuff as described at

:help sub-replace-special
:he eval.txt
:he line()

For a simple example, it would be something like

:%s/^/\=line(".")." "

which will simply insert the current line number at the beginning
of each line in the range.

For more complex work, beginning counting at one, but starting on
something *other* than the first line would be something to the
following:

:'<,'>s/^/(1+line(".") - line("'<"))." "

For doing inverted line numbering, you'll have to do inverting
math.  Something like

:'<,'>s/^/\=(1+line("'>") - line("."))." "

These can easily be mapped as needed.

HTH,

-tim







Reply | Threaded
Open this post in threaded view
|

Re: someone have some code to number lines

Alan G Isaac
In reply to this post by Billy N. Patton
On Fri, 24 Jun 2005, "Billy N. Patton" apparently wrote:
> Need to supply a range (via Shift v)
> The supply a numbering range 1-4 or 4-1

For the first case
:!cat -n
For the second case (untested)
:!tac|cat -n |tac

hth,
Alan Isaac




Reply | Threaded
Open this post in threaded view
|

Re: someone have some code to number lines

Steve Hall-4
In reply to this post by Billy N. Patton
From: "Billy N. Patton", Jun 24, 2005 10:53 AM
>
> I need to number lines.

I wrote a line numbering script:

  http://www.vim.org/scripts/script.php?script_id=605

It doesn't count backwards, but it can start from any arbitrary number
and justifies spacings. You could easily add a switch and a few lines
to toggle counting up or down.


--
Steve Hall  [ digitect mindspring com ]
:: Cream... something good to put in your Vim!
::   http://cream.sourceforge.net