Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reformat in vim for a nice column layout

Tags:

vim

reformat

People also ask

How do I set 80 characters per line in Vim?

As of vim 7.3, you can use set colorcolumn=80 ( set cc=80 for short).

How do I sort a column in vim?

Sorting text in Vim is easy! Select the text, then press : , type sort , then hit enter! It'll sort the whole document by default, but you can enter a range too.


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 '&'

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.


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

This is a great answer using vim macros: https://stackoverflow.com/a/8363786/59384 - basically, you start recording a macro, format the first column, stop recording then repeat the macro for all remaining lines.

Copy/pasted from that answer:

qa0f:w100i <Esc>19|dwjq4@a

Note the single space after the 100i, and the <Esc> means "press escape"--don't type "<Esc>" literally.

Translation:

qa         -- record macro in hotkey a
0          -- go to beginning of line
f:         -- go to first : symbol
w          -- go to next non-space character after the symbol
100i <Esc> -- insert 100 spaces
19|        -- go to 19th column (value 19 figured out manually)
dw         -- delete spaces until : symbol
j          -- go to next line
q          -- stop recording macro
4@a        -- run the macro 4 times (for the remaining 4 lines)

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


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

Demonstration GIF from its README:


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.