This port is a lightly-tested side project. Use at your own risk.
If functionality is important to you, please use the original rainbow_csv.
I love rainbow_csv, but I wanted :RainbowAlign
to run faster. Porting to lua
made it a few times faster.
With packer.nvim:
use {
'cameron-wags/rainbow_csv.nvim',
config = function()
require 'rainbow_csv'.setup()
end,
-- optional lazy-loading below
module = {
'rainbow_csv',
'rainbow_csv.fns'
},
ft = {
'csv',
'tsv',
'csv_semicolon',
'csv_whitespace',
'csv_pipe',
'rfc_csv',
'rfc_semicolon'
}
}
With lazy.nvim:
{
'cameron-wags/rainbow_csv.nvim',
config = true,
ft = {
'csv',
'tsv',
'csv_semicolon',
'csv_whitespace',
'csv_pipe',
'rfc_csv',
'rfc_semicolon'
},
cmd = {
'RainbowDelim',
'RainbowDelimSimple',
'RainbowDelimQuoted',
'RainbowMultiDelim'
}
}
No additional steps required - Rainbow CSV will work out of the box.
Main features:
There are 4 ways to enable csv columns highlighting:
:set ft=csv
The core functionality of the plugin is written in pure vimscript, no additional libraries required.
Rainbow CSV has 7 built-in CSV filetypes and infinite number of autogenerated filetypes.
Each Rainbow CSV filetype is mapped to a separator and "policy" which describes additional properties e.g. if separator can be escaped inside double quoted field.
If user uses :RainbowDelim or :RainbowMultiDelim to select a separator that doesn't map to one of the built-in filetypes, Rainbow CSV will dynamically generate the filetype syntax file and put it into the "syntax" folder.
List of built-in filetypes:
Filetype | Separator | Extension | Properties |
---|---|---|---|
csv | , (comma) | .csv | Ignored inside double-quoted fields |
tsv | \t (TAB) | .tsv .tab | |
csv_semicolon | ; (semicolon) | Ignored inside double-quoted fields | |
csv_whitespace | whitespace | Consecutive whitespaces are merged | |
csv_pipe | | (pipe) | ||
rfc_csv | , (comma) | Same as "csv" but allows multiline fields | |
rfc_semicolon | ; (semicolon) | Same as "csv_semicolon" but allows multiline fields |
In most cases the built-in autodetection algorithm should correctly detect correct CSV dialect for all CSV tables that you open in Vim, but if you have disabled the autodetection algorithm or don't want to rely on it for some reason, you can manually associate file extensions with available csv dialects.
Example: to associate ".dat" extension with "csv_pipe" dialect and ".csv" extension with "csv_semicolon" add the folowing lines to your .vimrc:
autocmd BufNewFile,BufRead *.csv set filetype=csv_semicolon
autocmd BufNewFile,BufRead *.dat set filetype=csv_pipe
In rare cases some CSV files can contain double-quoted fields spanning multiple lines.
To work with such files you can set filetype to either "rfc_csv" or "rfc_semicolon" e.g. :set ft=rfc_csv
.
Syntax highlighting for rfc_csv and rfc_semicolon dialects can sometimes go out of sync with the file content, use :syntax sync fromstart
command in that case.
rfc_csv and rfc_semicolon are fully supported by RBQL which among other things allows you to easily convert them to line-by-line CSV by replacing newlines in fields with sequences of 4 spaces or something like that.
rfc_csv and rfc_semicolon take their name from RFC 4180 memo with which they are fully compatible.
Rainbow CSV comes with built-in lightweight RBQL SQL-like query engine.
To run an RBQL query either press F5 (:RbSelect command) or enter the query in vim command line e.g. :Select a1, a2
As soon as you finish entering ":select" (or ":update") and press whitespace, the plugin will show column names in the status line.
Demonstration of rainbow_csv highlighting and RBQL queries
In this demo python expressions were used, but JavaScript is also available.
You can use rainbow highlighting and RBQL even for non-csv/tsv files.
E.g. you can highlight records in log files, one-line xmls and other delimited records.
You can even highlight function arguments in your programming language using comma or comma+whitespaces as a delimiter for :RainbowDelim or :RainbowMultiDelim commands.
And you can always turn off the rainbow highlighting using :NoRainbowDelim command.
Key mappings are only assigned for files with active rainbow highlighting
Key | Action |
---|---|
F5 | Start query editing for the current csv file |
F5 | Execute currently edited query |
F7 | Copy query result set to the parent buffer |
You can disable these mappings by setting let g:disable_rainbow_key_mappings = 1
Mark current file as a table and highlight it's columns in rainbow colors. Character under the cursor will be used as a delimiter. The delimiter will be saved in a config file for future vim sessions.
You can also use this command for non-csv files, e.g. to highlight function arguments
in source code in different colors. To return back to original syntax highlighting run :NoRainbowDelim
Same as :RainbowDelim, but works with multicharacter separators.
Visually select the multicharacter separator (e.g. ~#~
) and run :RainbowMultiDelim command.
Disable rainbow columns highlighting for the current file.
Mark the character under the cursor as the comment prefix, e.g. #
. By default Rainbow CSV doesn't highlight comments in CSV files.
You can also use :RainbowCommentMulti
to mark a visual selection as a multicharacter comment prefix
Disable all comments for the current CSV file.
This command is especially useful when you have set g:rainbow_comment_prefix
variable and want to exclude just one particular file.
The linter checks the following:
Align CSV columns with whitespaces.
Don't run this command if you treat leading and trailing whitespaces in fields as part of the data.
You can edit aligned CSV file in Vim column-edit mode (Ctrl+v).
Remove leading and trailing whitespaces from all fields. Opposite to RainbowAlign
Allows to enter RBQL select query as vim command. e.g. :Select a1, a2 order by a1
Allows to enter RBQL update query as vim command. e.g. :Update a1 = a1 + " " + a2
Assign any name to the current table. You can use this name in join operation instead of the table path. E.g.
JOIN customers ON a1 == b1
intead of:
JOIN /path/to/my/customers/table ON a1 == b1
Set to 1
to stop showing info about the column under the cursor in Vim command line
Example:
let g:disable_rainbow_hover = 1
Max frequency column hover info is calculated when moving the cursor rapidly.
Set to 0
to disable this behavior.
Example:
let g:rainbow_hover_debounce_ms = 300
Set to 1 to stop modifying 'statusline' settings when rainbow csv features are enabled. Example:
let g:disable_rainbow_statusline = 1
Default: ["\t", ",", ";", "|"]
List of separators to try for content-based autodetection
You can add or remove values from the list. Example:
let g:rcsv_delimiters = ["\t", ",", "^", "~#~"]
Set to 1
to disable CSV autodetection mechanism
Example:
let g:disable_rainbow_csv_autodetect = 1
Manual delimiter selection would still be possible. You can also manually associate specific file extensions with 'csv' or 'tsv' filetypes
Default: ''
A string to use as a comment prefix for all CSV files you open in Vim.
This setting is helpful if you are dealing with lots of CSV files which consistently use the same comment prefix e.g. '#'
or '>>'
If you want to enable comments on file-by-file basis, use the :RainbowComment
or :RainbowCommentMulti
commands instead.
To cancel the effect of g:rainbow_comment_prefix
just for the current file use :NoRainbowComment
command.
Default: 30
Autodetection will fail if buffer has more than g:rcsv_max_columns columns.
You can increase or decrease this limit.
List of color name pairs to customize rainbow highlighting.
Each entry in the list is a pair of two colors: the first color is for terminal mode, the second one is for GUI mode.
Example:
let g:rcsv_colorpairs = [['red', 'red'], ['blue', 'blue'], ['green', 'green'], ['magenta', 'magenta'], ['NONE', 'NONE'], ['darkred', 'darkred'], ['darkblue', 'darkblue'], ['darkgreen', 'darkgreen'], ['darkmagenta', 'darkmagenta'], ['darkcyan', 'darkcyan']]
Default: 10
This settings is only relevant for rfc_csv and rfc_semicolon dialects.
If some multiline records contain more lines that this value, hover info will not work correctly. It is not recommended to significantly increase this value because it will have negative impact on hover info performance
Disable default key mappings introduced by the extension
Default: python Supported values: 'python', 'js'
Scripting language to use in RBQL expressions.
Default: utf-8 Supported values: 'utf-8', 'latin-1'
CSV files encoding for RBQL.
Default: input
Supported values: tsv, csv, input
Format of RBQL result set tables.
Essentially format is a pair: delimiter + quoting policy.
This setting for example can be used to convert files between tsv and csv format:
:let g:rbql_output_format='tsv'
3. :Select *
:let g:rbql_output_format='csv'
3. :Select *
Set to 1
to use system python interpreter for RBQL queries instead of the python interpreter built into your vim/neovim editor.
If most of the CSV files that you work with have headers, you can set this value to 1. In this case RBQL will treat first records in files as headers by default.
Example: :let g:rbql_with_headers = 1
You can also adjust (or override) this setting by adding WITH (header)
or WITH (noheader)
to the end of your RBQL queries.
RBQL is an eval-based SQL-like query engine for (not only) CSV file processing. It provides SQL-like language that supports SELECT queries with Python or JavaScript expressions.
RBQL is best suited for data transformation, data cleaning, and analytical queries.
RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries.
All keywords have the same meaning as in SQL queries. You can check them online
RBQL for CSV files provides the following variables which you can use in your queries:
UPDATE query produces a new table where original values are replaced according to the UPDATE expression, so it can also be considered a special type of SELECT query.
RBQL supports the following aggregate functions, which can also be used with GROUP BY keyword:
COUNT, _ARRAYAGG, MIN, MAX, SUM, AVG, VARIANCE, MEDIAN
Limitation: aggregate functions inside Python (or JS) expressions are not supported. Although you can use expressions inside aggregate functions.
E.g. MAX(float(a1) / 1000)
- valid; MAX(a1) / 1000
- invalid.
There is a workaround for the limitation above for _ARRAYAGG function which supports an optional parameter - a callback function that can do something with the aggregated array. Example:
SELECT a2, ARRAY_AGG(a1, lambda v: sorted(v)[:5]) GROUP BY a2
- Python; SELECT a2, ARRAY_AGG(a1, v => v.sort().slice(0, 5)) GROUP BY a2
- JS
Join table B can be referenced either by its file path or by its name - an arbitrary string which the user should provide before executing the JOIN query.
RBQL supports STRICT LEFT JOIN which is like LEFT JOIN, but generates an error if any key in the left table "A" doesn't have exactly one matching key in the right table "B".
Table B path can be either relative to the working dir, relative to the main table or absolute.
Limitation: JOIN statements can't contain Python/JS expressions and must have the following form: _<JOIN_KEYWORD> (/path/to/table.tsv | tablename ) ON a... == b... [AND a... == b... [AND ... ]]
SELECT EXCEPT can be used to select everything except specific columns. E.g. to select everything but columns 2 and 4, run: SELECT * EXCEPT a2, a4
Traditional SQL engines do not support this query mode.
UNNEST(list) takes a list/array as an argument and repeats the output record multiple times - one time for each value from the list argument.
Example: SELECT a1, UNNEST(a2.split(';'))
RBQL does not support LIKE operator, instead it provides "like()" function which can be used like this:
SELECT * where like(a1, 'foo%bar')
You can set whether the input (and join) CSV file has a header or not using the environment configuration parameters which could be --with_headers
CLI flag or GUI checkbox or something else.
But it is also possible to override this selection directly in the query by adding either WITH (header)
or WITH (noheader)
statement at the end of the query.
Example: select top 5 NR, * with (header)
RBQL supports User Defined Functions
You can define custom functions and/or import libraries in two special files:
~/.rbql_init_source.py
- for Python~/.rbql_init_source.js
- for JavaScriptSELECT TOP 100 a1, int(a2) * 10, len(a4) WHERE a1 == "Buy" ORDER BY int(a2) DESC
SELECT a.id, a.weight / 1000 AS weight_kg
SELECT * ORDER BY random.random()
- random sortSELECT len(a.vehicle_price) / 10, a2 WHERE int(a.vehicle_price) < 500 and a['Vehicle type'] in ["car", "plane", "boat"] limit 20
- referencing columns by names from header and using Python's "in" to emulate SQL's "in"UPDATE SET a3 = 'NPC' WHERE a3.find('Non-playable character') != -1
SELECT NR, *
- enumerate records, NR is 1-basedSELECT * WHERE re.match(".*ab.*", a1) is not None
- select entries where first column has "ab" patternSELECT a1, b1, b2 INNER JOIN ./countries.txt ON a2 == b1 ORDER BY a1, a3
- example of join querySELECT MAX(a1), MIN(a1) WHERE a.Name != 'John' GROUP BY a2, a3
- example of aggregate querySELECT *a1.split(':')
- Using Python3 unpack operator to split one column into many. Do not try this with other SQL engines!SELECT TOP 100 a1, a2 * 10, a4.length WHERE a1 == "Buy" ORDER BY parseInt(a2) DESC
SELECT a.id, a.weight / 1000 AS weight_kg
SELECT * ORDER BY Math.random()
- random sortSELECT TOP 20 a.vehicle_price.length / 10, a2 WHERE parseInt(a.vehicle_price) < 500 && ["car", "plane", "boat"].indexOf(a['Vehicle type']) > -1 limit 20
- referencing columns by names from headerUPDATE SET a3 = 'NPC' WHERE a3.indexOf('Non-playable character') != -1
SELECT NR, *
- enumerate records, NR is 1-basedSELECT a1, b1, b2 INNER JOIN ./countries.txt ON a2 == b1 ORDER BY a1, a3
- example of join querySELECT MAX(a1), MIN(a1) WHERE a.Name != 'John' GROUP BY a2, a3
- example of aggregate querySELECT ...a1.split(':')
- Using JS "destructuring assignment" syntax to split one column into many. Do not try this with other SQL engines!Rainbow CSV name and original implementation was significantly influenced by rainbow_parentheses Vim plugin.
There also exists an old vim syntax file csv_color which, despite it's name, can highlight only *.tsv files.
And, of course, there is csv.vim