8000 GitHub - carywreams/rainbow_csv: 🌈Rainbow CSV - Vim plugin: Highlight columns in CSV and TSV files and run queries in SQL-like language
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

🌈Rainbow CSV - Vim plugin: Highlight columns in CSV and TSV files and run queries in SQL-like language

License

Notifications You must be signed in to change notification settings

carywreams/rainbow_csv

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rainbow CSV

rainbow_csv

Installation

Use your favorite package manager.

Vundle: Plugin 'mechatroner/rainbow_csv'
VimPlug: Plug 'mechatroner/rainbow_csv'
dein: call dein#add('mechatroner/rainbow_csv')

No additional steps required - Rainbow CSV will work out of the box.

Overview

Main features:

  • Highlight CSV columns in different rainbow colors.
  • Provide info about column under the cursor
  • Provide SELECT and UPDATE queries in RBQL: SQL-like transprogramming query language.
  • Consistency check for csv files (CSVLint)
  • Align and Shrink CSV fields (add/remove trailing spaces in fields)
  • Cell-level cursor navigation

There are 4 ways to enable csv columns highlighting:

  1. CSV autodetection based on file content and/or extension
  2. Manual CSV delimiter selection with :RainbowDelim command with cursor over the delimiter
  3. Manual CSV delimiter selection with :RainbowMultiDelim for multi-character delimiters selecting them in "VISUAL" mode
  4. Explicitly activate one of the built-in filetypes, e.g. :set ft=csv

The core functionality of the plugin is written in pure vimscript, no additional libraries required.

Rainbow CSV Screenshot

Plugin description

Built-in and autogenerated filetypes

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 you run :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

Associating file extensions with CSV dialects

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

Working with multiline CSV fields

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.

Built-in RBQL query language

Rainbow CSV comes with built-in lightweight RBQL SQL-like query engine.
To run an RBQL query either use :Select command e.g. :Select a1, a2 or run :RainbowQuery command to enter query editing mode.

Demonstration of rainbow_csv highlighting and RBQL queries

demo_screencast

In this demo python expressions were used, but JavaScript is also available.

Rainbow highlighting for non-table files

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.

Commands

:RainbowDelim

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

:RainbowMultiDelim

Same as :RainbowDelim, but works with multicharacter separators.
Visually select the multicharacter separator (e.g. ~#~) and run :RainbowMultiDelim command.

:NoRainbowDelim

Disable rainbow columns highlighting for the current file.

:RainbowCellGoUp

Move cursor one cell up.
Consider mapping this to Ctrl+[Up Arrow], see the "Key Mappings" section.

:RainbowCellGoDown

Move cursor one cell down.
Consider mapping this to Ctrl+[Down Arrow], see the "Key Mappings" section.

:RainbowCellGoLeft

Move cursor one cell left.
Consider mapping this to Ctrl+[Left Arrow], see the "Key Mappings" section.

:RainbowCellGoRight

Move cursor one cell right.
Consider mapping this to Ctrl+[Right Arrow], see the "Key Mappings" section.

:RainbowComment

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

:NoRainbowComment

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.

:CSVLint

The linter checks the following:

  • consistency of double quotes usage in CSV rows
  • consistency of number of fields per CSV row

:RainbowAlign

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).

:RainbowShrink

Remove leading and trailing whitespaces from all fields. Opposite to RainbowAlign

:Select ...

Allows to enter RBQL select query as vim command.
E.g. :Select a1, a2 order by a1

:Update ...

Allows to enter RBQL update query as vim command.
E.g. :Update a1 = a1 + " " + a2

:RainbowQuery

Enter RBQL Query editing mode.
When in the query editing mode, execute :RainbowQuery again to run the query.
Consider mapping :RainbowQuery to <F5> key i.e. nnoremap <F5> :RainbowQuery<CR>

:RainbowName <name>

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

:RainbowCopyBack

This command only applicable for RBQL output files.
Replace the content of the original file that was used to run the RBQL query with the query result set data.

Key Mappings

Plugin does not create any new key mappings, but you can define your own in your .vimrc file.
All highlighted files have a special buffer variable b:rbcsv set to 1, so you can use this to define conditional csv-only key mappings.
For example, to conditionally map Ctrl+Arrow keys to cell navigation commands you can use this snippet:

nnoremap <expr> <C-Left> get(b:, 'rbcsv', 0) == 1 ? ':RainbowCellGoLeft<CR>' : '<C-Left>'
nnoremap <expr> <C-Right> get(b:, 'rbcsv', 0) == 1 ? ':RainbowCellGoRight<CR>' : '<C-Right>'
nnoremap <expr> <C-Up> get(b:, 'rbcsv', 0) == 1 ? ':RainbowCellGoUp<CR>' : '<C-Up>'
nnoremap <expr> <C-Down> get(b:, 'rbcsv', 0) == 1 ? ':RainbowCellGoDown<CR>' : '<C-Down>'

You can also map arrow keys unconditionally, but this will have no effect outside highlighted CSV files, e.g.

nnoremap <C-Right> :RainbowCellGoRight<CR>

Configuration

g:disable_rainbow_hover

Set to 1 to stop showing info about the column under the cursor in Vim command line
Example:

let g:disable_rainbow_hover = 1

g:rcsv_delimiters

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", ",", "^", "~#~"]

g:disable_rainbow_csv_autodetect

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

g:rainbow_comment_prefix

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.

g:rcsv_max_columns

Default: 30
Autodetection will fail if buffer has more than g:rcsv_max_columns columns.
You can increase or decrease this limit.

g:rcsv_colorpairs

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

g:multiline_search_range

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

g:rbql_backend_language

Default: 'python'
Supported values: 'python', 'js'

Scripting language to use in RBQL expressions.

g:rbql_encoding

Default: utf-8
Supported values: 'utf-8', 'latin-1'

CSV files encoding for RBQL.

g:rbql_output_format

Default: 'input'
Supported values: 'tsv', 'csv', 'input'

Format of RBQL result set tables.

  • input: same format as the input table
  • tsv: doesn't allow quoted tabs inside fields.
  • csv: is Excel-compatible and allows quoted commas.

Essentially format is a pair: delimiter + quoting policy.
This setting for example can be used to convert files between tsv and csv format:

  • To convert csv to tsv: 1. open csv file. 2. :let g:rbql_output_format='tsv' 3. :Select *
  • To convert tsv to csv: 1. open tsv file. 2. :let g:rbql_output_format='csv' 3. :Select *

g:rbql_use_system_python

Set to 1 to use system python interpreter for RBQL queries instead of the python interpreter built into your vim/neovim editor.

g:rbql_with_headers

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 (Rainbow Query Language) Description

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.

Official Site

Main Features

  • Use Python or JavaScript expressions inside SELECT, UPDATE, WHERE and ORDER BY statements
  • Supports multiple input formats
  • Result set of any query immediately becomes a first-class table on its own
  • No need to provide FROM statement in the query when the input table is defined by the current context.
  • Supports all main SQL keywords
  • Supports aggregate functions and GROUP BY queries
  • Supports user-defined functions (UDF)
  • Provides some new useful query modes which traditional SQL engines do not have
  • Lightweight, dependency-free, works out of the box

Limitations: