CSV Files in Vim

4.7
(36)

No matter what kind of work you do in Vim, you’re likely to come across CSV text files. The Comma Separated Value format is ubiquitous despite the availability of many other modern formats. Instead of turning to Excel or Google Sheets to view and manipulate CSV files, consider a plugin which will improve the Vim experience in CSVs. Two popular ones are csv.vim and Rainbow CSV. In this article, I will cover Rainbow CSV, which is lighter weight and extremely useful.

Vim CSV Syntax Highlighting

After installing Rainbow CSV, you should automatically notice CSV files appear much more colorful. The plugin enables syntax highlighting which cycles through the colors of your color scheme, ensuring each column can be distinguished visually from the next. The plugin supports many other delimiters other than commas. The highlighting can even be used for non-table file formats such as log files!

There are two other extremely useful features: status line column notation and querying capabilities. In the demo below I show all three of these features in action. You’ll notice that as I move my cursor through the file, the column name of the data under my cursor is noted in the status line. Rainbow CSV sources this from the first line of the file.

Querying CSV Files in Vim

There is also a SQL-like querying syntax made by possible by the plugin. This is a simple and effective way to dig through a CSV file without leaving Vim. No need to import to a database or open Excel, you can query quickly with commands such as:

:Select a1, a8 order by a8 desc

The syntax a1, a2, a3, etc. refers to the column number (one-indexed). The status line notation of the present column shows the column number to make building these queries easier. In the demo below, I use this exact query to select out the name and cholesterol level of foods:

You can do a lot more querying with Rainbow CSV, including joining across files and even updating. The querying language used is somewhat of a standard called RBQL and there is robust documentation.

How useful was this tip?

Average rating 4.7 / 5. Vote count: 36

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts