m (Fixed formatting) |
m (Added duplicate flag and set category) |
||
Line 1: | Line 1: | ||
{{review}} |
{{review}} |
||
+ | {{Duplicate|139|894|570}} |
||
{{Tip |
{{Tip |
||
|id=893 |
|id=893 |
||
Line 126: | Line 127: | ||
---- |
---- |
||
<!-- parsed by vimtips.py in 0.530612 seconds--> |
<!-- parsed by vimtips.py in 0.530612 seconds--> |
||
+ | [[Category:Usage]] |
Revision as of 21:24, 18 September 2007
Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
Tip: #893 - Align numbers at decimal point
Created: March 10, 2005 6:37 Complexity: intermediate Author: Michael Fitz Version: 5.7 Karma: 3/5 Imported from: Tip#893
To see the _really_ alignment you should use a fixedsized font (eg. FixedSys on Windows) or mark this all and copy it into vim!
Suppose, you've gotten some numbers in a ugly format:
---BEG 123 2.5678 -13.44 100.5 +47.11 ---END
You want to have them nice aligned with 5 decimals written out. First, we align any line left to the beginning:
:%s§^\s*§§ ---BEG 123 2.5678 -13.44 100.5 +47.11 ---END
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).
(The decimal-point has been replaced by '!' for simpleness: you are not forced to escape '!' :-)
:%s§\([-+]\?\d\+\)\.\?\(\d*$\)§\1 !\200000§ ---BEG 123 !00000 2 !567800000 -13 !4400000 100 !500000 +47 !1100000 ---END
This tricky substitue aligns the fractional part at column 15:
:%s§\%15c\s*!§!§ ---BEG 123 !00000 2 !567800000 -13 !4400000 100 !500000 +47 !1100000 ---END
Now we shift the integral part back by exchanging it with leading spaces (and by the mean replacing '!' by decimal-point):
:%s§\(^\S*\)\(\s*\)!§\2\1.§ ---BEG 123.00000 2.567800000 -13.4400000 100.500000 +47.1100000 ---END
Now we truncate each fractional part to 5 digits:
:%s§\%21c\d*§§ ---BEG 123.00000 2.56780 -13.44000 100.50000 +47.11000 ---END
Finally we add a '+'-sign where it's missing:
:%s§\s\(\d\)§+\1§ ---BEG +123.00000 +2.56780 -13.44000 +100.50000 +47.11000 ---END
Looks now very nice, doesn't it?
Comments
Addendum to this tip: I usually use the (german) paragraph-sign '§' to surround the substitute-patterns, because this letter is very seldom used in any IT-related context ;-)
Anonymous , March 10, 2005 6:40
FYI: AlignMaps 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.
NdrOchip--AT--ScampbellPfamily.AbizM - NOSPAM
, March 10, 2005 12:50