2

I have a table of values (actually written in LaTeX markup) and I can select one column only using Ctrl+Q

For example,

% Select the second column only ....
1.75130211563   & 0.0299693961831 \\
1.72144412106   & 0.0181406611688 \\
1.92102386609   & 0.0247758598737 \\
1.56512790839   & 0.0137107006809 \\
1.75263937567   & 0.017155656704  \\
1.99501744437   & 0.39550649953   \\
1.96862597164   & 0.030198328601  \\
...

I'd like to reduce the number of decimal places of the selected numbers only (i.e. for each number selected I apply round(NUMBER * 100)/100 to get, for example, the number rounded to 2 decimal places). To do this, I need to first have a variable to refer to NUMBER (the number on that line) as well as replace the current selection with the output.

How do I do this?

Also if this isn't possible, I can copy the column into an actual spreadsheet program and edit it there, but how do I paste it back in place?

Update: I've accepted an answer. It isn't as neat as I had hoped, but it does make sense. Thanks!

Another update: To paste a column in from an external spreadsheet, paste the column into a :new buffer, select the data using Ctrl+Q and yank into a register. Move to the top line and appropriate column in the table of data and paste in with P.

3
  • 1
    Can you update your question with an example of how the actual text looks like? BTW, the rccol package can do rounding directly in LaTeX, see rccol Jul 13, 2011 at 14:19
  • Ah thanks, that's good to know. However I'm keen to see if Vim can handle this neatly since this type of task has cropped up outside of editing LaTeX documents.
    – Brendan
    Jul 13, 2011 at 14:25
  • See :h :normal-range, :h expand() (and :h <cWORD>), :h t, :h printf() (you can use printf("%.2f", number) to print number with two digits after decimal dot).
    – ZyX
    Jul 13, 2011 at 14:40

2 Answers 2

3

Are you looking for this ?

:'<,'>s#\%V\d*\.\d\+#\=round(str2float(submatch(0))*100)/100#g
4
  • I'm not sure how this is used, when I select a visual block and then hit : to enter commands, '<,'> is automatically inserted. Writing the above afterwards causes an error, replacing the command with the above cause the whole document to be affected...
    – Brendan
    Jul 13, 2011 at 15:34
  • % above means whole file, remove that and go for the visual selection markers only i.e. '<,'> Jul 13, 2011 at 15:47
  • 3
    To restrict to the visual block you also need to include \%V in the search field: :'<,'>s#\%V\(\d*\.\d\+\)#\=round(str2float(submatch(1))*100)/100#g (otherwise all occurences on selected lines will be substituted). Jul 13, 2011 at 15:51
  • 1
    I've edited the answer to include Prince Goulash's suggestion - it now works great.
    – Brendan
    Jul 18, 2011 at 14:26
1

This seems to work:

:'<,'>:s/\d\+\.\d\+/\=printf("%.2f", str2float(submatch(0)))/

I.e. visually select the first column and issue the command above.

% Select the second column only ....
1.75   & 0.0299693961831 \\
1.72   & 0.0181406611688 \\
1.92   & 0.0247758598737 \\
1.57   & 0.0137107006809 \\
1.75   & 0.017155656704  \\
2.00   & 0.39550649953   \\
1.97   & 0.030198328601  \\
2
  • This kind of works, unfortunately tabs seem to mess things up a bit in the actual file I am editing. I have set a large tabstop so that columns do not overlap and the selection is only numbers (i.e. no spaces etc.), however the above command for some reason fails on rows where the previous columns values are quite wide (i.e. enough to cause a column to overlap if the tabstop was slightly smaller). Is Vim reading the selection information using shiftwidth or something?
    – Brendan
    Jul 13, 2011 at 15:45
  • Vim is not selecting anything based on shiftwidth. You can see what Vim is selecting by yanking your selection and pasting it into a blank area of the document. If that doesn't help you figure it out perhaps you could post a problematic table. Jul 13, 2011 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.