reformat in vim for a nice column layout

2020-01-29 23:42发布

I have this dataset in a csv file

1.33570301776, 3.61194e-06, 7.24503e-06, -9.91572e-06, 1.25098e-05, 0.0102828, 0.010352, 0.0102677, 0.0103789, 0.00161604, 0.00167978, 0.00159998, 0.00182596, 0.0019804, 0.0133687, 0.010329, 0.00163437, 0.00191202, 0.0134425 
1.34538754675, 3.3689e-06, 9.86066e-06, -9.12075e-06, 1.18058e-05, 0.00334344, 0.00342207, 0.00332897, 0.00345504, 0.00165532, 0.00170412, 0.00164234, 0.00441903, 0.00459294, 0.00449357, 0.00339737, 0.00166596, 0.00451926, 0.00455153
1.34808186291, -1.99011e-06, 6.53026e-06, -1.18909e-05, 9.52337e-06, 0.00158065, 0.00166529, 0.0015657, 0.0017022, 0.000740644, 0.00078635, 0.000730052, 0.00219736, 0.00238191, 0.00212762, 0.00163783, 0.000750669, 0.00230171, 0.00217917

As you can see, the numbers are formatted differently and misaligned. Is there a way in vim to quickly align the columns properly, so that the result is this

1.33570301776,  3.61194e-06, 7.24503e-06, -9.91572e-06, 1.25098e-05, 0.0102828,  0.010352,   0.0102677,  0.0103789, 0.00161604, 0.00167978, 0.00159998, 0.00182596, 0.0019804,  0.0133687,  0.010329,   0.00163437, 0.00191202, 0.0134425 
1.34538754675,  3.3689e-06,  9.86066e-06, -9.12075e-06, 1.18058e-05, 0.00334344, 0.00342207, 0.00332897, 0.00345504,0.00165532, 0.00170412, 0.00164234, 0.00441903, 0.00459294, 0.00449357, 0.00339737, 0.00166596, 0.00451926, 0.00455153
1.34808186291, -1.99011e-06, 6.53026e-06, -1.18909e-05, 9.52337e-06, 0.00158065, 0.00166529, 0.0015657,  0.0017022, 0.000740644,0.00078635, 0.000730052,0.00219736, 0.00238191, 0.00212762, 0.00163783, 0.000750669,0.00230171, 0.00217917

That would be great to copy and paste sections with ctrl-v. Any hints?

标签: vim reformat
12条回答
Root(大扎)
2楼-- · 2020-01-29 23:54

Pretty old question, but I've recently availed myself of an excellent vim plugin that enables table formatting either on the fly or after-the-fact (as your use case requires):

https://github.com/dhruvasagar/vim-table-mode

查看更多
家丑人穷心不美
3楼-- · 2020-01-29 23:56

If you're on some kind of UNIX (Linux, etc), you can cheat and filter it through the column(1) command.

:%!column -t

The above will parse on delimiters inside string literals which is wrong, so you will likely need pre-processing steps and specifying the delimiter for this file for example:

%!sed 's/","/\&/' | column -t -s '&'
查看更多
做个烂人
4楼-- · 2020-01-29 23:59

also if you have very long columns it can be handy to disable default wrapping

:set nowrap
:%!column -t

(note in debian you also have a further option for column -n which if you want to split multiple adjacent delimiters)

查看更多
可以哭但决不认输i
5楼-- · 2020-01-30 00:00

As sunny256 suggested, the column command is a great way of doing this on Unix/Linux machines, but if you want to do it in pure Vim (so that it can be used in Windows as well), the easiest way is to install the Align plugin and then do:

:%Align ,
:%s/\(\s\+\),\s/,\1/g

The first line aligns the entries on the commas and the second moves the comma so that it's flush with the preceding value. You may be able to use AlignCtrl to define a custom mapping that does the whole lot in one go, but I can never remember how to use it...

Edit

If you don't mind two spaces between entries and you want to do this in one command, you can also do:

:%Align ,\zs
查看更多
闹够了就滚
6楼-- · 2020-01-30 00:01

Sometimes we want to align just two columns. In that case, we don't need any plugins and can use pure Vim functionality like this:

  1. Choose a separator. In OP's post this is a comma, in my example this is =.
  2. Add spaces before/after it. I use s/=/= ...spaces... / in visual selection for this.
  3. Locate to the longest word and place cursor after it.
  4. Remove all the extra whitespace using dw and vertical movement.

Example of this technique demonstrated below:

Example

I don't find myself needing to align things often enough to install another plugin, so this was my preferred way of accomplishing it - especially that it doesn't require much thinking.

查看更多
来,给爷笑一个
7楼-- · 2020-01-30 00:01

I wrote python script that allows users to basicly columize any type of text also outside of vim. Not sure if this will work for windows or mac users.

columnice.py gist

Usage when in vim.

:'<,'>!columnice =

This will use the equals sign as the delimeter. The delimeter is not thrown away though.

查看更多
登录 后发表回答