reformat in vim for a nice column layout

后端 未结 13 1078
野性不改
野性不改 2020-11-30 16:21

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,          


        
相关标签:
13条回答
  • 2020-11-30 16:52

    I just wrote tablign for this purpose. Install with

    pip3 install tablign --user
    

    Then simply mark the table in vim and do

    :'<,'>:!tablign
    

    0 讨论(0)
  • 2020-11-30 16:52

    I have this in my .vimrc.

    command! CSV set nowrap | %s/,/,|/g | %!column -n -t -s "|" 
    

    This aligns the columns while keeping the comma, which may be needed later for correct reading. For example, with Python Pandas read_csv(..., skipinitialspace=True), thanks Pandas guys for this smart option, otherwise in vim %s/,\s\+/,/g. It may be easier if your column has the option --output-separator I guess, my doesn't and I'm not sure why (my man page for column says 2004, on ubuntu 18.04, not sure ubuntu will get a new version). Anyway, this works for me, and comment if you have any suggestions.

    0 讨论(0)
  • 2020-11-30 16:57

    You could use the csv.vim plugin.

    :%ArrangeColumn
    

    However, this will not do exactly what you have asked: it will right adjust the contents of cells, whereas you have your values aligned by the decimal point or by the first digit.

    The plugin has many other useful commands for working with CSV files.

    0 讨论(0)
  • 2020-11-30 16:58

    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.

    0 讨论(0)
  • 2020-11-30 17:00

    Also, Tabularize is quite good http://vimcasts.org/episodes/aligning-text-with-tabular-vim/

    0 讨论(0)
  • 2020-11-30 17:00

    We now also have the fabulous EasyAlign plugin, written by junegunn.

    Demonstration GIF from its README:

    0 讨论(0)
提交回复
热议问题