Wikia

Vim Tips Wiki

Changes: Align numbers at decimal point

Edit

Back to page

m (Reverted edits by Emprego.curitiba (talk | block) to last version by JohnBot)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 5: Line 5:
 
|previous=892
 
|previous=892
 
|next=894
 
|next=894
|created=March 10, 2005
+
|created=2005
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Michael Fitz
 
|author=Michael Fitz
Line 94: Line 94:
   
 
==Comments==
 
==Comments==
See [[VimTip139]] for AlignMaps which provides the <tt>\anum</tt> (actually <Leader>anum) to do something similar:
+
See [[VimTip139]] for AlignMaps which provides the <code>\anum</code> (actually <Leader>anum) to do something similar:
 
<pre>
 
<pre>
 
123
 
123
Line 103: Line 103:
 
</pre>
 
</pre>
   
It doesn't append the zeros, just does a numeric alignment. <tt>\anum</tt> also handles the use of commas instead of periods, European style.
+
It doesn't append the zeros, just does a numeric alignment. <code>\anum</code> also handles the use of commas instead of periods, European style.
   
 
----
 
----

Latest revision as of 05:54, July 13, 2012

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 893 Printable Monobook Previous Next

created 2005 · complexity intermediate · author Michael Fitz · version 5.7


Suppose you have some numbers in an ugly format:

123
2.5678
-13.44
100.5
  +47.11

You want to have them nice aligned with 5 decimals written out.

First, we align any line left to the beginning:

:%s§^\s*§§

123
2.5678
-13.44
100.5
+47.11

Now we split at the decimal-point (if any) and shift the fractional part wide to the right and add five '0' at the end (because we want 5 fractional digts).

:%s§\([-+]\?\d\+\)\.\?\(\d*$\)§\1                        !\200000§

123                        !00000
2                        !567800000
-13                        !4400000
100                        !500000
+47                        !1100000

This tricky substitue aligns the fractional part at column 15:

:%s§\%15c\s*!§!§

123           !00000
2             !567800000
-13           !4400000
100           !500000
+47           !1100000

Now we shift the integral part back by exchanging it with leading spaces (and replacing '!' by decimal-point):

:%s§\(^\S*\)\(\s*\)!§\2\1.§

           123.00000
             2.567800000
           -13.4400000
           100.500000
           +47.1100000

Now we truncate each fractional part to 5 digits:

:%s§\%21c\d*§§

           123.00000
             2.56780
           -13.44000
           100.50000
           +47.11000

Finally we add a '+'-sign where it's missing:

:%s§\s\(\d\)§+\1§

          +123.00000
            +2.56780
           -13.44000
          +100.50000
           +47.11000

I usually use the (german) paragraph-sign '§' to surround the substitute-patterns, because this letter is very seldom used in any IT-related context.

CommentsEdit

See VimTip139 for AlignMaps which provides the \anum (actually <Leader>anum) to do something similar:

123
  2.5678
-13.44
100.5
+47.11

It doesn't append the zeros, just does a numeric alignment. \anum also handles the use of commas instead of periods, European style.


Around Wikia's network

Random Wiki