How do I align columns of text into a neatly formatted table in Vim?
Answer
:'<,'>!column -t
Explanation
When working with data that has uneven spacing — such as variable assignments, CSV-like data, or configuration entries — you can select the lines and pipe them through the external column -t command to instantly align everything into neat columns.
How it works
:'<,'>— the visual selection range (automatically inserted when you press:in visual mode)!— filter the selection through an external commandcolumn -t— a Unix utility that formats input into a table by detecting whitespace-separated columns and padding them evenly
The selected text is replaced in-place with the aligned output.
Example
Before (select these lines in visual mode):
name age city
Alice 30 Portland
Bob 7 San Francisco
Charlie 55 NYC
After pressing :'<,'>!column -t:
name age city
Alice 30 Portland
Bob 7 San
Charlie 55 NYC
Note that column -t splits on whitespace by default. For other delimiters, use the -s flag: :'<,'>!column -t -s ',' for CSV data.
Tips
- Use
column -t -s '|'to align Markdown tables separated by pipes - This works on any system with the
columnutility (standard on Linux and macOS) - Combine with
sortfor sorted, aligned output::'<,'>!sort | column -t - Undo with
uif the result is not what you expected