ft-csv.txt 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. *ft-csv.txt* For Vim version 7.3 Last Change: Wed, 25 Jul 2012
  2. Author: Christian Brabandt <cb@256bit.org>
  3. Version: 0.26
  4. Homepage: http://www.vim.org/scripts/script.php?script_id=2830
  5. The VIM LICENSE applies to the CSV filetype plugin (see |copyright|).
  6. NO WARRANTY, EXPRESS OR IMPLIED. USE AT-YOUR-OWN-RISK.
  7. *csv-toc*
  8. 1. Introduction.................................|csv-intro|
  9. 2. Installation.................................|csv-installation|
  10. 3. CSV Commands.................................|csv-commands|
  11. 3.1 WhatColumn..............................|WhatColumn_CSV|
  12. 3.2 NrColumns...............................|NrColumns_CSV|
  13. 3.3 SearchInColumn..........................|SearchInColumn_CSV|
  14. 3.4 HiColumn................................|HiColumn_CSV|
  15. 3.5 ArrangeColumn...........................|ArrangeColumn_CSV|
  16. 3.6 UnArrangeColumn.........................|UnArrangeColumn_CSV|
  17. 3.7 DeleteColumn............................|DeleteColumn_CSV|
  18. 3.8 InitCSV.................................|InitCSV|
  19. 3.9 Header..................................|Header_CSV|
  20. 3.10 Sort...................................|Sort_CSV|
  21. 3.11 CopyColumn.............................|Copy_CSV|
  22. 3.12 MoveColumn.............................|MoveCol_CSV|
  23. 3.13 Sum of a column........................|SumCol_CSV|
  24. 3.14 Create new records ....................|NewRecord_CSV|
  25. 3.15 Change the delimiter...................|NewDelimiter_CSV|
  26. 3.16 Check for duplicate records............|Duplicate_CSV|
  27. 3.17 Normal mode commands...................|csv-mapping|
  28. 3.18 Convert CSV file.......................|csv-convert|
  29. 3.19 Dynamic filters........................|csv-filter|
  30. 3.20 Analyze a column.......................|csv-analyze|
  31. 3.21 Vertical Folding.......................|csv-vertfold|
  32. 3.22 Transposing columns....................|csv-transpose|
  33. 3.23 Transforming into a table..............|csv-tabularize|
  34. 4. CSV Filetype configuration...................|csv-configuration|
  35. 4.1 Delimiter...............................|csv-delimiter|
  36. 4.2 Column..................................|csv-column|
  37. 4.3 HiGroup.................................|csv-higroup|
  38. 4.4 Strict Columns..........................|csv-strict|
  39. 4.5 Multibyte Chars.........................|csv-mbyte|
  40. 4.6 Concealing..............................|csv-conceal|
  41. 4.7 Newlines................................|csv-newline|
  42. 4.8 Highlight column automatically..........|csv-hicol|
  43. 4.9 Fixed width columns.....................|csv-fixedwidth|
  44. 4.9.1 Manual setup
  45. 4.9.2 Setup using a Wizard
  46. 4.10 CSV Header lines.......................|csv-header|
  47. 4.11 Number format..........................|csv-nrformat|
  48. 4.12 Move folded lines......................|csv-move-folds|
  49. 4.13 Using Comments.........................|csv-comments|
  50. 5. Functions....................................|CSV-Functions|
  51. 5.1 CSVPat()................................|CSVPat()|
  52. 6. CSV Tips and Tricks..........................|csv-tips|
  53. 6.1 Statusline..............................|csv-stl|
  54. 6.2 Slow ArrangeCol.........................|csv-slow|
  55. 6.3 Defining custom aggregate functions.....|csv-aggregate-functions|
  56. 6.4 Autocommand on opening/closing files....|csv-arrange-autocmd|
  57. 6.5 CSV Syntax error........................|csv-syntax-error|
  58. 7. CSV Changelog................................|csv-changelog|
  59. ==============================================================================
  60. 1. Introduction *csv-intro*
  61. This plugin is used for handling column separated data with Vim. Usually those
  62. files are called csv files and use the ',' as delimiter, though sometimes they
  63. use e.g. the '|' or ';' as delimiter and there also exists fixedwidth columns.
  64. The aim of this plugin is to ease handling these kinds of files.
  65. This is a filetype plugin for CSV files. It was heavily influenced by
  66. the Vim Wiki Tip667 (http://vim.wikia.com/wiki/VimTip667), though it
  67. works differently. For instructions on installing this file, type
  68. :help add-local-help |add-local-help| inside Vim. For a screenshot, of
  69. how the plugin can be used, see http://www.256bit.org/~chrisbra/csv.gif
  70. ==============================================================================
  71. 2. Installation *csv-installation*
  72. In order to have vim automatically detect csv files, you need to have
  73. |ftplugins| enabled (e.g. by having this line in your |.vimrc| file: >
  74. :filetype plugin on
  75. <
  76. The plugin already sets up some logic, to detect CSV files. By default,
  77. the plugin recognizes *.csv and *.dat files as csv filetype. In order that the
  78. csv filetype plugin is loaded correctly, vim needs to be enabled to load
  79. |filetype-plugins|. This can be ensured, by putting a line like this into your
  80. |.vimrc|: >
  81. :filetype plugin on
  82. <
  83. (see also |filetype-plugin-on|).
  84. In case this did not work, you need to setup vim like this:
  85. To have Vim automatically detect csv files, you need to do the following.
  86. 1) Create your user runtime directory if you do not have one yet. This
  87. directory needs to be in your 'runtime' path. In Unix this would
  88. typically the ~/.vim directory, while in Windows this is usually your
  89. ~/vimfiles directory. Use :echo expand("~") to find out, what Vim thinks
  90. your user directory is.
  91. To create this directory, you can do: >
  92. :!mkdir ~/.vim
  93. <
  94. for Unix and >
  95. :!mkdir ~/vimfiles
  96. <
  97. for Windows.
  98. 2) In that directory you create a file that will detect csv files. >
  99. if exists("did_load_csvfiletype")
  100. finish
  101. endif
  102. let did_load_csvfiletype=1
  103. augroup filetypedetect
  104. au! BufRead,BufNewFile *.csv,*.dat setfiletype csv
  105. augroup END
  106. <
  107. You save this file as "filetype.vim" in your user runtime diretory: >
  108. :w ~/.vim/filetype.vim
  109. <
  110. 3) To be able to use your new filetype.vim detection, you need to restart
  111. Vim. Vim will then load the csv filetype plugin for all files whose
  112. names end with .csv.
  113. ==============================================================================
  114. 3. Commands *csv-commands*
  115. The CSV ftplugin provides several Commands:
  116. 3.1 WhatColumn *WhatColumn_CSV*
  117. --------------
  118. If you would like to know, on which column the cursor is, use >
  119. :WhatColumn
  120. Use the bang attribute, if you have a heading in the first line and you want
  121. to know the name of the column in which the cursor is: >
  122. :WhatColumn!
  123. <
  124. 3.2 NrColumns *NrColumns_CSV*
  125. --------------
  126. :NrColumns outputs the maximum number of columns available. It does this by
  127. testing the first 10 lines for the number of columns. This usually should be
  128. enough. If you use the '!' attribute, it outputs the number of columns in the
  129. current line.
  130. 3.3 SearchInColumn *SearchInColumn_CSV*
  131. ------------------
  132. Use :SearchInColumn to search for a pattern within a specific column. The
  133. usage is: >
  134. :SearchInColumn [<nr>] /{pat}/
  135. <
  136. So if you would like to search in Column 1 for the word foobar, you enter >
  137. :SearchInColumn 1 /foobar/
  138. Instead of / as delimiter, you can use any other delimiter you like. If you
  139. don't enter a column, the current column will be used.
  140. 3.4 HiColumn *HiColumn_CSV*
  141. ------------
  142. :HiColumn <nr> can be used to highlight Column <nr>. Currently the plugin uses
  143. the WildMenu Highlight Group. If you would like to change this, you need to
  144. define the variable |g:csv_hiGroup|.
  145. If you do not specify a <nr>, HiColumn will highlight the column on which the
  146. cursor is. Use >
  147. :HiColumn!
  148. to remove any highlighting.
  149. If you want to automatically highlight a column, see |csv-hicol|
  150. *:ArrangeColumn*
  151. 3.5 ArrangeColumn *ArrangeColumn_CSV*
  152. -----------------
  153. If you would like all columns to be visually arranged, you can use the >
  154. :[range]ArrangeColumn[!]
  155. command. Beware, that this will change your file and depending on the size of
  156. your file may slow down Vim significantly. This is highly experimental.
  157. :ArrangeCommand will try to vertically align all columns by their maximum
  158. column size.
  159. Use the bang attribute to force recalculating the column width. This is
  160. slower, but especially if you have modified the file, this will correctly
  161. calculate the width of each column so that they can be correctly aligned. If
  162. no column width has been calculated before, the width will be calculated, even
  163. if the '!' has not been given.
  164. If [range] is not given, it defaults to the current line.
  165. Note, this can be very slow on large files or many columns (see
  166. |csv-slow| on how to increase performance for this command). To prevent you
  167. from accidently changing your csv file, the buffer will be set 'readonly'
  168. afterwards. Note: this command does not work for fixed width columns
  169. |csv-fixedwidth|
  170. See also |csv-arrange-autocmd| on how to have vim automaticaly arrange a CSV
  171. file upon entering it.
  172. 3.6 UnArrangeColumn *UnArrangeColumn_CSV*
  173. -----------------
  174. If you would like to undo a previous :ArrangeColumn command, you can use this
  175. command: >
  176. :[range]UnArrangeColumn
  177. Beware, that is no exact undo of the :ArrangeColumn command, since it strips
  178. away all leading blanks for each column. So if previously a column contained
  179. only some blanks, this command will strip all blanks.
  180. If [range] is given, it defaults to the current line.
  181. 3.7 DeleteColumn *DeleteColumn_CSV*
  182. ----------------
  183. The command :DeleteColumn can be used to delete a specific column. >
  184. :DeleteColumn 2
  185. will delete column 2.
  186. If you don't specify a column number, it will delete the column on which the
  187. cursor is.
  188. You can also specify a search string. The plugin will then delete all columns
  189. that match the pattern: >
  190. :DeleteColumn /foobar
  191. <
  192. will delete all columns where the pattern "foobar" matches.
  193. 3.8 InitCSV *InitCSV*
  194. -----------
  195. Reinitialize the Plugin. Use this, if you have changed the configuration
  196. of the plugin (see |csv-configuration| ).
  197. 3.9 Header lines *Header_CSV*
  198. ----------------
  199. This command splits the csv-buffer and adds a window, that holds a small
  200. fraction of the csv file. This is useful, if the first line contains some kind
  201. of a heading and you want always to display it. This works similar to fixing a
  202. certain line at the top. As optional argument, you can give the number of
  203. columns from the top, that shall be displayed. By default, 1 is used (You can
  204. define youre own default by setting the b:csv_headerline variable, see
  205. |csv-header|). Use the '!' to close this window. So this >
  206. :Header 3
  207. opens at the top a split window, that holds the first 3 lines, is fixed
  208. and horizontally 'scrollbind'ed to the csv window and highlighted using the
  209. CSVHeaderLine highlighting.
  210. To close the header window, use >
  211. :Header!
  212. Note, this won't work with linebreaks in the column.
  213. Note also, that if you already have a horizontal header window (|VHeader_CSV|),
  214. this command will close the horizontal Header window. This is because of a
  215. limitation of Vim itsself, which doesn't allow to sync the scrolling between
  216. two windows horizontally and at the same time have another window only sync
  217. its scrolling vertically.
  218. Note: this command does not work for fixed width columns |csv-fixedwidth|
  219. *VHeader_CSV*
  220. If you want a vertical header line, use :VHeader. This works similar to the
  221. |Header_CSV| command, except that it will open a vertical split window with
  222. the first column always visible. It will always open the first column in the
  223. new split window. Use the '!' to close the window.
  224. Note, this won't work with linebreaks in the column.
  225. Note also: this command does not work for fixed width columns |csv-fixedwidth|
  226. *VHeaderToggle_CSV* *HeaderToggle_CSV*
  227. Use the :HeaderToggle and :VHeaderToggle command to toggle displaying the
  228. horizontal or vertical header line.
  229. 3.10 Sort *Sort_CSV*
  230. --------
  231. This command can be used to sort the csv file on a certain column. If no range
  232. is given, is sorts the whole file. Specify the column number to sort on as
  233. argument. Use the '!' attribute to reverse the sort order. For example, the
  234. following command sorts line 1 til 10 on the 3 column >
  235. :1,10Sort 3
  236. While this command >
  237. :1,10Sort! 3
  238. reverses the order based on column 3.
  239. Instead of a column, you can give the flag 'n' to have it sort numerically.
  240. When no column number is given, it will sort by the column, on which the
  241. cursor is currently.
  242. 3.11 Copy Column *Copy_CSV*
  243. ----------------
  244. If you need to copy a specific column, you can use the command :Column >
  245. :[N]Column [a]
  246. Copy column N into register a. This will copy all the values, that are
  247. not folded-away (|csv-filter|) and skip comments.
  248. If you don't specify N, the column of the current cursor position is used.
  249. If no register is given, the default register
  250. |quotequote| is used.
  251. 3.12 Move A Column *MoveCol_CSV*
  252. ------------------
  253. You can move one column to the right of another column by using the
  254. :MoveColumn command >
  255. :[range]MoveColumn [source] [dest]
  256. This moves the column number source to the right of column nr destination. If
  257. both arguments are not given, move the column on which the cursor is to the
  258. right of the current last column. If [range] is not given, MoveColumn moves
  259. the entire column, otherwise, it moves the columns only for the lines within
  260. the range, e.g. given that your first line is a header line, which you don't
  261. want to change >
  262. :2,$MoveColumn 1 $
  263. this would move column 1 behind the last column, while keeping the header line
  264. as is.
  265. 3.13 Sum of a Column *SumCol_CSV*
  266. --------------------
  267. You can let Vim output the sum of a column using the :SumCol command >
  268. :[range]SumCol [nr] [/format/]
  269. This outputs the result of the column <nr> within the range given. If no range
  270. is given, this will calculate the sum of the whole column. If <nr> is not
  271. given, this calculates the sum for the column the cursor is on. Note, that the
  272. delimiter will be stripped away from each value and also empty values won't be
  273. considered.
  274. By default, Vim uses the a numerica format that uses the '.' as decimal
  275. separator while there is no thousands separator. If youre file contains
  276. the numbers in a different format, you can use the /format/ option to specify
  277. a different thousands separator or a different decimal separator. The format
  278. needs to be specified like this:
  279. /x:y/
  280. where 'x' defines the thousands separator and y defines the decimal
  281. separator and each one is optional. This means, that >
  282. :SumCol 1 /:,/
  283. uses the default thousands separator and ',' as the decimal separator and >
  284. :SumCol 2 / :./
  285. uses the Space as thousands separator and the '.' as decimal separator.
  286. Note, if you Vim is compiled without floating point number format (|+float|),
  287. Vim will only aggregate the integer part and therefore won't use the 'y'
  288. argument in the /format/ specifier.
  289. See also |csv-aggregate-functions|
  290. 3.14 Create new Records *NewRecord_CSV*
  291. -----------------------
  292. If you want to create one or several records, you can use the :NewRecord
  293. command: >
  294. :[range]NewRecord [count]
  295. This will create in each line given by range [count] number of new empty
  296. records. If [range] is not specified, creates a new line below the line the
  297. cursor is on and if count is not given, it defaults to 1.
  298. 3.15 Change the delimiter *NewDelimiter_CSV*
  299. -------------------------
  300. If you want to change the field delimiter of your file you can use the
  301. :NewDelimiter command: >
  302. :NewDelimiter char
  303. This changes the field delimiter of your file to the new delimiter "char".
  304. 3.16 Check for duplicate records *Duplicate_CSV*
  305. --------------------------------
  306. If you want to check the file for duplicate records, use the command: >
  307. :Duplicate columnlist
  308. <
  309. Columnlist needs to be a numeric comma-separated list of all columns that you
  310. want to check. You can also use a range like '2-5' which means the plugin
  311. should check columns 2,3,4 and 5.
  312. If the plugin finds a duplicate records, it outputs its line number (but it
  313. only does that at most 10 times).
  314. 3.17 Normal mode commands *csv-mapping*
  315. -------------------------
  316. The csv filetype plugin redefines the following keys as:
  317. <C-Right> or L or W Move [count] field forwards
  318. <C-Left> or E or H Move [count] field backwards
  319. <Up> or K Move [count] lines upwards within the same column
  320. <Down> or J Move [count] lines downwards within the same column
  321. <Enter> Dynamically fold all lines away, that don't match
  322. the value in the current column. See |csv-filter|
  323. <Space> Dynamically fold all lines away, that match
  324. the value in the current column. See |csv-filter|
  325. <BS> Remove last item from the dynamic filter.
  326. See |csv-filter|
  327. also, the csv plugin defines these text-object:
  328. if Inner Field (contains everything up to the delimiter)
  329. af Outer Field (contains everything up to and including
  330. the delimiter)
  331. Note, that the <BS>, <CR>, K and J overlap Vim's default mapping for |<CR>|,
  332. |<BS>|, |J| and |K| respectively. Therefore, this functionality has been
  333. mapped to a sane default of <Localleader>J and <LocalLeader>K. If you haven't
  334. changed the |<Leader>| or |<LocalLeader>| variables, those the <Localleader>
  335. is equival to a single backslash '\', e.g. \K would run the lookup function on
  336. the word under the cursor and \J would join this line with the previous line.
  337. *ConvertData_CSV*
  338. 3.18 Converting a CSV File *csv-convert*
  339. --------------------------
  340. You can convert your CSV file to a different format with the command >
  341. ConvertData
  342. Use the the ! attribute, to convert your data without the delimiter.
  343. This command will interactively ask you for the definition of 3 variables.
  344. After which it will convert your csv file into a new format, defined by those
  345. 3 variables and open the newly created file in a new window. Those 3 variables
  346. define how the text converted.
  347. First, You need to define what has to be done, before converting your column
  348. data. That is done with the "pre convert" variable. The content of this
  349. variable will be put in front of the new document.
  350. Second, you define, what has to be put after the converted content of your
  351. column data. This happens with the "post convert" variable. Basically the
  352. contents of this variable will be put after processing the columns.
  353. Last, the columns need to be converted into your format. For this you can
  354. specify a printf() format like string, that defines how your data will be
  355. converted. You can use '%s' to specify placeholders, which will later be
  356. replaced by the content of the actual column.
  357. For example, suppose you want to convert your data into HTML, then you first
  358. call the >
  359. :ConvertData
  360. At this point, Vim will ask you for input. First, you need to specify, what
  361. needs to be done before processing the data:
  362. Pre convert text: <html><body><table> `
  363. This would specify to put the HTML Header before the actual data can be
  364. processed. If the variable g:csv_pre_convert is already defined, Vim will
  365. already show you its' content as default value. Simply pressing Enter will use
  366. this data. After that, Vim asks, what the end of the converted file needs to
  367. look like:
  368. Post convert text: </table></body></html> `
  369. So here you are defining how to finish up the HTML file. If the variable
  370. g:csv_post_convert is already defined, Vim will already show you its' content
  371. as default value which you can confirm by pressing Enter. Last, you define,
  372. how your columns need to be converted. Again, Vim asks you for how to do that:
  373. Converted text, use %s for column input: `
  374. <tr><td>%s</td><td>%s</td><td>%s</td></tr>
  375. This time, you can use '%s' expandos. They tell Vim, that they need to be
  376. replaced by the actual content of your file. It does by going from the first
  377. column in your file and replacing it with the corresponding %s in that order.
  378. If there are less '%s' expandos then columns in your file, Vim will skip the
  379. columns, that are not used. Again If the variable g:csv_convert is already
  380. defined, Vim will already show you its' content as default value which you can
  381. confirm by pressing Enter.
  382. After you hit Enter, Vim will convert your data and put it into a new window.
  383. It may look like this:
  384. <html><body><table> `
  385. <tr><td>1,</td><td>2,</td><td>3,</td></tr> `
  386. <tr><td>2,</td><td>2,</td><td>4,</td></tr> `
  387. </table></body></html> `
  388. Note, this is only a proof of concept. A better version of converting your
  389. data to HTML is bundled with Vim (|:TOhtml|).
  390. But may be you want your data converted into SQL-insert statements. That could
  391. be done like this: >
  392. ConvertData!
  393. <
  394. Pre convert text: `
  395. (Leave this empty. It won't be used).
  396. Post convert text: Commit; `
  397. After inserting the data, commit it into the database.
  398. Converted text, use %s for column input: `
  399. Insert into table foobar values ('%s', '%s', %s); `
  400. Note, that the last argument is not included within single quotation marks,
  401. since in this case the data is assumed to be integer and won't need to be
  402. quoted for the database.
  403. After hitting Enter, a new Window will be opened, which might look like this:
  404. Insert into table foobar values('Foobar', '2', 2011); `
  405. Insert into table foobar values('Bar', '1', 2011); `
  406. Commit; `
  407. Since the command was used with the bang attribute (!), the converted data
  408. doesn't include the column delimiters.
  409. Now you can copy it into your database, or further manipulate it.
  410. 3.19 Dynamic filters *csv-filter*
  411. --------------------
  412. If you are one a value and only want to see lines, that have the same value in
  413. this column, you can dynamically filter the file and fold away all lines not
  414. matching the value in the current column. To do so, simply press <CR> (Enter).
  415. Now Vim will fold away all lines, that don't have the same value in this
  416. particular row. Note, that leading blanks and the delimiter is removed and the
  417. value is used literally when comparing with other values. If you press <Space>
  418. on the value, all fields having the same value will be folded away.
  419. The way this is done is, that the value from the column is extracted and a
  420. regular expression for that field is generated from it. In the end this
  421. regular expression is used for folding the file.
  422. A subsequent <CR> or <Space> on another value, will add this value to the
  423. current applied filter (this is like using the logical AND between the
  424. currently active filter and the new value). To remove the last item from the
  425. filter, press <BS> (backspace). If all items from the filter are removed,
  426. folding will be disabled.
  427. If some command messes up the folding, you can use |zX| to have the folding
  428. being reinitialized.
  429. By default, the first line is assumed to be the header and won't be folded
  430. away. See also |csv-header|.
  431. If you have set the g:csv_move_folds variable and the file is modifiable, all
  432. folded lines will be moved to the end of the file, so you can view all
  433. non-folded lines as one consecutive area (see also |csv-move-folds|)
  434. *:Filter* *Filter_CSV*
  435. To see the active filters, you can use the :Filter command. This will show you
  436. a small summary, of what filters are active and looks like this:
  437. Nr Match Col Name Value `
  438. ===================================================== `
  439. 01 - 07 Price 23.10 `
  440. 02 + 08 Qty 10 `
  441. This means, there are two filters active. The current active filter is on
  442. column 7 (column name is Price) and all values that match 23.10 will be folded
  443. away AND all values that don't match a value of 10 in the QTY column will also
  444. be folded away.
  445. When removing one item from the filter by pressing <BS>, it will always remove
  446. the last item (highest number in NR column) from the active filter values.
  447. Note, that depending on your csv file and the number of filters you used,
  448. applying the filter might actually slow down vim, because a complex regular
  449. expression is generated that is applied by the fold expression. Look into the
  450. @/ (|quote_/|) register to see its value.
  451. Use |zX| to apply the current value of your search register as filter. Use >
  452. :Filters!
  453. to reapply all values from the current active filter and fold non-matching
  454. items away.
  455. *Analyze_CSV*
  456. 3.20 Analyze a Column *csv-analyze*
  457. ---------------------
  458. If you'd like to know, how the values are distributed among a certain column,
  459. you can use the :Analyze command. So >
  460. :Analyze 3
  461. outputs the the distribution of the top 5 values in column 3. This looks like
  462. this:
  463. Nr Count % Value `
  464. ============================= `
  465. 01 20 50% 10 `
  466. 02 10 25% 2 `
  467. 03 10 25% 5 `
  468. This tells you, that the the value '10' in column 3 occurs 50% of the time
  469. (exactly 20 times) and the other 2 values '2' and '5' occur only 10 times, so
  470. 25% of the time.
  471. *VertFold_CSV*
  472. 3.21 Vertical Folding *csv-vertfold*
  473. ---------------------
  474. Sometimes, you want to hide away certain columns to better view only certain
  475. columns without having to horizontally scroll. You can use the :VertFold
  476. command to hide certain columns: >
  477. :VertFold [<nr>]
  478. <
  479. This will hide all columns from the first until the number entered. It
  480. currently can't hide single columns, because of the way, syntax highlighting
  481. is used. This command uses the conceal-feature |:syn-conceal| to hide away
  482. those columns. If no nr is given, hides all columns from the beginning till
  483. the current column.
  484. Use >
  485. :VertFold!
  486. to display all hidden columns again.
  487. *Transpose_CSV*
  488. 3.22 Transposing a column *csv-transpose*
  489. -------------------------
  490. Transposing means to exchange rows and columns. You can transpose the csv
  491. file, using the: >
  492. :[range]Transpose
  493. <
  494. command. If [range] is not given, it will transpose the complete file,
  495. otherwise it will only transpose the lines in the range given. Note, comments
  496. will be deleted and transposing does not work with fixed-width columns.
  497. *CSV-Tabularize*
  498. 3.23 Transforming into a table *csv-tabularize*
  499. ------------------------------
  500. You can also transform your csv data into a visual table, using the: >
  501. :Tabularize
  502. <
  503. command. This will make a frame around your csv data and substitute all
  504. delimiters by '|', so that it will look like a table.
  505. e.g. consider this data: >
  506. First,Second,Third
  507. 10,5,2
  508. 5,2,10
  509. 2,10,5
  510. 10,5,2
  511. This will be transformed into: >
  512. |---------------------|
  513. | First| Second| Third|
  514. |------|-------|------|
  515. | 10| 5| 2|
  516. | 5| 2| 10|
  517. | 2| 10| 5|
  518. | 10| 5| 2|
  519. |---------------------|
  520. ==============================================================================
  521. 4. CSV Configuration *csv-configuration*
  522. The CSV plugin tries to automatically detect the field delimiter for your
  523. file, cause although often the file is called CSV (comma separated values), a
  524. semicolon is actually used. The column separator is stored in the buffer-local
  525. variable b:delimiter. This delimiter is heavily used, because you need
  526. it to define a column. Almost all commands use this variable therefore.
  527. 4.1 Delimiter *csv-delimiter*
  528. -------------
  529. To override the automatic detection of the plugin and define the separator
  530. manually, use: >
  531. :let g:csv_delim=','
  532. to let the comma be the delimiter. This sets the buffer local delimiter
  533. variable b:delimiter.
  534. If your file does not consist of delimited columns, but rather is a fixed
  535. width csv file, see |csv-fixedwidth| for configuring the plugin appropriately.
  536. If you changed the delimiter, you should reinitiliaze the plugin using
  537. |InitCSV|
  538. 4.2 Column *csv-column*
  539. ----------
  540. The definition, of what a column is, is defined as buffer-local variable
  541. b:col. By default this variable is initialized to: >
  542. let b:col='\%(\%([^' . b:delimiter . ']*"[^"]*"[^' . b:delimiter . ']*'
  543. \. b:delimiter . '\)\|\%([^' . b:delimiter . ']*\%(' . b:delimiter
  544. \. '\|$\)\)\)'
  545. This should take care of quoted delimiters within a column. Those should
  546. obviously not count as a delimiter. This regular expression is quite
  547. complex and might not always work on some complex cases (e.g. linebreaks
  548. within a field, see RFC4180 for some ugly cases that will probably not work
  549. with this plugin).
  550. If you changed the b:delimiter variable, you need to redefine the b:col
  551. variable, cause otherwise it will not reflect the change. To change the
  552. variable from the comma to a semicolon, you could call in your CSV-Buffer
  553. this command: >
  554. :let b:col=substitute(b:col, ',', ';', 'g')
  555. Check with :echo b:col, if the definition is correct afterwards.
  556. You can also force the plugin to use your own defined regular expression as
  557. column. That regular expression should include the delimiter for the columns.
  558. To define your own regular expression, set the g:csv_col variable: >
  559. let g:csv_col='[^,]*,'
  560. This defines a column as a field delimited by the comma (where no comma can be
  561. contained inside a field), similar to how |csv-strict| works.
  562. You should reinitialize the plugin afterwards |InitCSV|
  563. 4.3 Highlighting Group *csv-higroup*
  564. ----------------------
  565. By default the csv ftplugin uses the WildMenu highlighting Group to define how
  566. the |HiColumn| command highlights columns. If you would like to define a
  567. different highlighting group, you need to set this via the g:csv_hiGroup
  568. variable. You can e.g. define it in your |.vimrc|: >
  569. :let g:csv_hiGroup = "IncSearch"
  570. You need to restart Vim, if you have changed this variable or use |InitCSV|
  571. The |hl-Title| highlighting is used for the Header line that is created by the
  572. |Header_CSV| command. If you prefer a different highlighting, set the
  573. g:csv_hiHeader variable to the prefered highlighting: >
  574. let g:csv_hiHeader = 'Pmenu'
  575. <
  576. This would set the header window to the |hl-Pmenu| highlighting, that is used
  577. for the popup menu. To disable the custom highlighting, simply |unlet| the
  578. variable: >
  579. unlet g:csv_hiHeader
  580. You should reinitialize the plugin afterwards |InitCSV|
  581. 4.4 Strict Columns *csv-strict*
  582. ------------------
  583. The default regular expression to define a column is quite complex
  584. (|csv-column|). This slows down the processing and makes Vim use more memory
  585. and it could still not fit to your specific use case.
  586. If you know, that in your data file, the delimiter cannot be contained inside
  587. the fields quoted or escaped, you can speed up processing (this is quite
  588. noticeable when using the |ArrangeColumn_CSV| command) by setting the
  589. g:csv_strict_columns variable: >
  590. let g:csv_strict_columns = 1
  591. This would define a column as this regex: >
  592. let b:col = '\%([^' . b:delimiter . ']*' . b:delimiter . '\|$\)'
  593. Much simpler then the default column definition, isn't it?
  594. See also |csv-column| and |csv-delimiter|
  595. You can disable the effect if you |unlet| the variable: >
  596. unlet g:csv_strict_columns
  597. You should reinitialize the plugin afterwards |InitCSV|
  598. For example when opening a CSV file you get the Error |E363|: pattern uses
  599. more memory than 'maxmempattern'. In this case, either increase the
  600. 'maxmempattern' or set the g:csv_strict_columns variable.
  601. 4.5 Multibyte Chars *csv-mbyte*
  602. -------------------
  603. Unfortunately, when using the |ArrangeColumn_CSV| command, multibyte chars
  604. make some trouble, because internally Vim uses bytes to specify the width of a
  605. column. The CSV plugin tries to workaround that, by calculating the byte width
  606. of each column, before aligning them. This is quite expensive and can slow
  607. down processing. If you know, your data file only contains pure ASCII chars
  608. (or you simply don't care, if some lines a off a little bit), set the
  609. g:csv_no_multibyte variable: >
  610. let g:csv_no_multibyte = 1
  611. And to force calculating the byte width of each column |unlet| the variable: >
  612. unlet g:csv_no_multibyte
  613. You should reinitialize the plugin afterwards |InitCSV|
  614. 4.6 Concealing *csv-syntax* *csv-conceal*
  615. -------------------
  616. The CSV plugin comes with a function to syntax highlight csv files. Basically
  617. allt it does is highlight the columns and the header line.
  618. By default, the delimiter will not be displayed, if Vim supports |conceal| of
  619. syntax items and instead draws a vertical line. If you don't want that, simply
  620. set the g:csv_noconceal variable in your .vimrc >
  621. let g:csv_no_conceal = 1
  622. and to disable it, simply unlet the variable >
  623. unlet g:csv_no_conceal
  624. You should reinitialize the plugin afterwards |InitCSV|
  625. Note: You can also set the 'conceallevel' option to control how the concealed
  626. chars will be displayed.
  627. If you want to customize the syntax colors, you can define your own groups.
  628. The CSV plugin will use already defined highlighting groups, if they are
  629. already defined, otherwise it will define its own defaults which should be
  630. visible with 8, 16, 88 and 256 color terminals. For that it uses the
  631. CSVColumnHeaderOdd and CSVColumnHeaderEven highlight groups for syntax
  632. coloring the first line. All other lines get either the CSVColumnOdd or
  633. CSVColumnEven highlighting.
  634. In case you want to define your own highlighting groups, you can define your
  635. own syntax highlighting like this in your |.vimrc| >
  636. hi CSVColumnEven term=bold ctermbg=4 guibg=DarkBlue
  637. hi CSVColumnOdd term=bold ctermbg=5 guibg=DarkMagenta
  638. hi CSVColumnHeaderEven ...
  639. hi CSVColumnHeaderOdd ...
  640. <
  641. Note, these changes won't take effect, until you restart Vim.
  642. 4.7 Newlines *csv-newline*
  643. ------------
  644. RFC4180 allows newlines in double quoted strings. By default, the csv-plugin
  645. won't recognize newlines inside fields. It is however possible to make the
  646. plugin aware of newlines within quoted strings. To enable this, set >
  647. let g:csv_nl = 1
  648. and to disable it again, simply unset the variable >
  649. unlet g:csv_nl
  650. It is a good idea to reinitialize the plugin afterwards |InitCSV|
  651. Note, this might not work correctly in all cases. The syntax highlighting
  652. seems to change on cursor movements. This could possibly be a bug in the
  653. syntax highlighting engine of Vim. Also, |WhatColumn_CSV| can't handle
  654. newlines inside fields and will most certainly be wrong.
  655. 4.8 Highlight column automatically *csv-hicol*
  656. ----------------------------------
  657. You can let vim automatically highlight the column on which the cursor is.
  658. This works by defining an |CursorMoved| autocommand to always highlight the
  659. column, when the cursor is moved in normal mode. Note, this does not update
  660. the highlighting, if the Cursor is moved in Insert mode. To enable this,
  661. define the g:csv_highlight_column variable like this >
  662. let g:csv_highlight_column = 'y'
  663. and to disable it again, simply unset the variable >
  664. unlet g:csv_highlight_column
  665. It is a good idea to reinitialize the plugin afterwards |InitCSV|
  666. 4.9 Fixed width columns *csv-fixedwidth*
  667. -----------------------
  668. Sometimes there are no real columns, but rather the file is fixed width with
  669. no distinct delimiters between each column. The CSV plugin allows you to
  670. handle such virtual columns like csv columns, if you define where each column
  671. starts.
  672. Note: Except for |ArrangeColumn_CSV| and the |Header_CSV| commands, all
  673. commands work in either mode. Those two commands won't do anything in the case
  674. of fixedwidth columns, since they don't really make sense here.
  675. 4.9.1 Manual setup
  676. ------------------
  677. You can do this, by setting the buffer-local variable
  678. b:csv_fixed_width like this >
  679. let b:csv_fixed_width="1,5,9,13,17,21"
  680. This defines that each column starts at multiples of 4. Be sure, to issue
  681. this command in the buffer, that contains your file, otherwise, it won't
  682. have an effect, since this is a buffer-local option (|local-option|)
  683. After setting this variable, you should reinitialize the plugins using
  684. |InitCSV|
  685. *CSVFixed*
  686. 4.9.2 Setup using a Wizard
  687. --------------------------
  688. Alternatively, you can setup the fixed width columns using the :CSVFixed
  689. command. This provides a simple wizard to select each column. If you enter
  690. the command: >
  691. :CSVFixed
  692. <
  693. The first column will be highlighted and Vim outputs:
  694. <Cursor>, <Space>, <ESC>, <BS>, <CR>...
  695. This means, you can now use those 5 keys to configure the fixed-width columns:
  696. <Cursor> Use Cursor Left (<Left>) and Cursor Right (<Right>) to move the
  697. highlighting bar.
  698. <Space> If you press <Space>, this column will be fixed and remain
  699. highlighted and there will be another bar, you can move using
  700. the Cursor keys. This means this column will be considered to be
  701. the border between 2 fixed with columns.
  702. <ESC> Abort
  703. <BS> Press the backspace key, to remove the last column you fixed with
  704. the <Space> key.
  705. <CR> Use Enter to finish the wizard. This will use all fixed columns
  706. to define the fixed width columns of your csv file. The plugin
  707. will be initialized and syntax highlighting should appear.
  708. Note: This only works, if your Vim has the 'colorcolumn' option available
  709. (This won't work with Vim < 7.3 and also not with a Vim without +syntax
  710. feature).
  711. 4.10 CSV Header lines *csv-header*
  712. ---------------------
  713. By default, dynamic filtering |csv-filter| will not fold away the first line.
  714. If you don't like that, you can define your header line using the variable
  715. b:csv_fold_headerline, e.g. >
  716. let b:csv_headerline = 0
  717. to disable, that a header line won't be folded away. If your header line
  718. instead is on line 5, simply set this variable to 5. This also applies to the
  719. |Header_CSV| command.
  720. 4.11 Number format *csv-nrformat*
  721. ------------------
  722. When using the |SumCol_CSV| command, you can specify a certain number format
  723. using the /x:y/ argument. You can however also configure the plugin to detect
  724. a different number format than the default number format (which does not
  725. support a thousands separator and uses the '.' as decimal separator).
  726. To specify a different thousands separator by default, use >
  727. let b:csv_thousands_sep = ' '
  728. to have the space use as thousands separator and >
  729. let b:csv_decimal_sep = ','
  730. to use the comma as decimal separator.
  731. 4.12 Move folded lines *csv-move-folds*
  732. ----------------------
  733. If you use dynamic filters (see |csv-filter|), you can configure the plugin to
  734. move all folded lines to the end of the file. This only happens if you set the
  735. variable >
  736. let g:csv_move_folds = 1
  737. <
  738. and the file is modifiable. This let's you see all non-folded records as a
  739. consecutive area without being disrupted by folded lines.
  740. (Note, that this might currently not work
  741. correctly all the times, as there seems to be a bug within Vim currently).
  742. 4.13 Using comments *csv-comments*
  743. -------------------
  744. Strictly speaking, in csv files there can't be any comments. You might however
  745. still wish to comment or annotate certain sections in your file, so the CSV
  746. plugin supports Comments.
  747. Be default, the CSV plugin will use the 'commentstring' setting to identify
  748. comments. If this option includes the '%s' it will consider the part before
  749. the '%s' as leading comment marker and the part behind it as comment
  750. delimiter.
  751. You can however define your own comment marker, using the variable
  752. g:csv_comment. Like with the 'commentstring' setting, you can use '%s'
  753. expandos, that will denote where the actual comment text belongs. To define
  754. your own comment string, put this in your |.vimrc| >
  755. :let g:csv_comment = '#'
  756. <
  757. Which will use the '#' sign as comment leader like in many scripting
  758. languages.
  759. After setting this variable, you should reinitialize the plugins using
  760. |InitCSV|
  761. ==============================================================================
  762. 5. Functions *CSV-Funtions*
  763. The csv plugins also defines some functions, that can be used for scripting
  764. when a csv file is open
  765. 5.1 CSVPat() *CSVPat()*
  766. ------------
  767. CSVPat({column}[, {pattern}])
  768. This function returns the pattern for the selected column. If only columns is
  769. given, returns the regular expression used to search for the pattern '.*' in
  770. that column (which means the content of that column). Alternatively, an
  771. optional pattern can be given, so the return string can be directly feeded to
  772. the |/| or |:s| command, e.g. type: >
  773. :s/<C-R>=CSVPat(3, 'foobar')<cr>/baz
  774. where the <C-R> means pressing Control followed by R followed by =
  775. (see |c_CTRL-R_=|). A prompt will apear, with the '=' as the first character
  776. on which you can enter expressions.
  777. In this case enter CSVPat(3, 'foobar') which returns the pattern to search for
  778. the string 'foobar' in the third column. After you press enter, the returned
  779. pattern will be put after the :s command so you can directly enter / and the
  780. substitute string.
  781. ==============================================================================
  782. 6. CSV Tips and Tricks *csv-tips*
  783. Here, there you'll find some small tips and tricks that might help when
  784. working with CSV files.
  785. 6.1 Statusline *csv-stl*
  786. --------------
  787. Suppose you want to include the column, on which the cursor is, into your
  788. statusline. You can do this, by defining in your .vimrc the 'statusline' like
  789. this: >
  790. function MySTL()
  791. if has("statusline")
  792. hi User1 term=standout ctermfg=0 ctermbg=11 guifg=Black guibg=Yellow
  793. let stl = ...
  794. if exists("*CSV_WCol")
  795. let csv = '%1*%{&ft=~"csv" ? CSV_WCol() : ""}%*'
  796. else
  797. let csv = ''
  798. endif
  799. return stl.csv
  800. endif
  801. endfunc
  802. set stl=%!MySTL()
  803. <
  804. This will draw in your statusline right aligned the current column and max
  805. column (like 1/10), if you are inside a CSV file. The column info will be
  806. drawn using the User1 highlighting (|hl-User1|), that has been defined in the
  807. second line of the function. In the third line of your function, put your
  808. desired 'statusline' settings as |expression|. Note the section starting with
  809. 'if exists(..)' guards against not having loaded the filetype plugin.
  810. *CSV_WCol*
  811. The CSV_WCol() function controls, what will be outputed. In the simplest case,
  812. when no argument is given, it simply returns on which column the cursor is.
  813. This would look like '1/10' which means the cursor is on the first of 10
  814. columns. If you rather like to know the name of the column, simply give as
  815. parameter to the function the string "Name". This will return the column name
  816. as it is printed on the first line of that column. This can be adjusted, to
  817. have the column name printed into the statusline (see |csv-stl| above) by
  818. replacing the line >
  819. let csv = '%1*%{&ft=~"csv" ? CSV_WCol() : ""}%*'
  820. <
  821. by e.g.
  822. let csv = '%1*%{&ft=~"csv" ? CSV_WCol("Name") . " " . CSV_WCol() : ""}%*'
  823. which will output "Name 2/10" if the cursor is in the second column
  824. which is named "Name".
  825. 6.2 Slow ArrangeCol *csv-slow*
  826. -------------------
  827. Processing a csv file using |ArrangeColumn_CSV| can be quite slow, because Vim
  828. needs to calculate the width for each column and then replace each column by
  829. itself widened by spaces to the optimal length. Unfortunately, csv files tend
  830. to be quite big. Remember, for a file with 10,000 lines and 50 columns Vim
  831. needs to process each cell, which accumulates to 500,000 substitutions. It
  832. might take some time, until Vim is finished.
  833. You can speed up things a little bit, if you omit the '!' attribute to the
  834. |ArrangeColumn| (but this will only work, if the width has been calculated
  835. before, e.g. by issuing a :1ArrangeColumn command to arrange only the first
  836. line. Additionally you can also configure how this command behaves by setting
  837. some configuration variables.
  838. Here are some performance meassurements on how the various
  839. configuration settings influence the |ArrangeColumn_CSV| command on a file
  840. with 34 columns (on a 2.2GHz Core2Duo processor):
  841. Lines | default | strict¹ | multibyte² | strict + multibyte³
  842. ------------------------------------------------------------------------
  843. 1000 | 6.93 s | 5.53 s | 6.76 s | 5.66 s
  844. 5000 | 15.2 s | 8.52 s | 14.27 s | 8.56 s
  845. 10000 | 36.2 s | 24.67 s | 36.11 s | 24.26 s
  846. 50000 | 162,23 s | 93.36 s | 152.25 s | 141.18 s
  847. ¹ setting the g:csv_strict_columns variable (|csv-strict|)
  848. ² setting the g:csv_no_multibyte variable (|csv-mbyte|)
  849. ³ setting the g:csv_no_multibyte variable and g:csv_strict_columns variable
  850. Note, this was performed on a quite fast processor. If you need to work with
  851. large files, be sure to have enough memory available, cause Vim needs to read
  852. in the whole file into memory. You can also try the LargeFile plugin available
  853. at http://www.vim.org/scripts/script.php?script_id=1506 which tunes several
  854. Vim options (like |syn-off|, 'undolimits', 'fdm' and others).
  855. 6.3 Defining custom aggregate functions *csv-aggregate-functions*
  856. ---------------------------------------
  857. The CSV plugin already defines the |SumCol_CSV| command, to let you calculate
  858. the sum of all values of a certain column within a given range. This will
  859. consider all values within the range, that are not folded away (|csv-filter|),
  860. and also skip comments and the header lines. The delimiter will be deleted
  861. from each field.
  862. But it may be, that you don't need the sum, but would rather want to have the
  863. average of all values within a certain column. You can define your own
  864. function and let the plugin call it for a column like this:
  865. 1) You define your own custom function in the after directory of your
  866. vim runtime path |after-directory| (see also #2 below) >
  867. fun! My_CSV_Average(col)
  868. let sum=0
  869. for item in a:col
  870. let sum+=item
  871. endfor
  872. return sum/len(a:col)
  873. endfun
  874. <
  875. This function takes a list as argument, and calculates the average for
  876. all items in the list. You could also make use of Vim's |eval()|
  877. function and write your own Product function like this >
  878. fun! My_CSV_Product(col)
  879. return eval(join(a:col, '*'))
  880. endfun
  881. <
  882. 2) Now define your own custom command, that calls your custom function for
  883. a certain column >
  884. command! -buffer -nargs=? -range=% AvgCol
  885. \ :echo csv#EvalColumn(<q-args>,
  886. \ "My_CSV_Average", <line1>,<line2>)
  887. <
  888. This command should best be put into a file called csv.vim and save
  889. it into your ~/.vim/after/ftplugin/ directory. Create directories
  890. that don't exist yet. For Windows, this would be the
  891. $VIMRUNTIME/vimfiles/after/ftplugin directory.
  892. 3) Make sure, your |.vimrc| includes a filetype plugin setting like this >
  893. filetype plugin on
  894. <
  895. This should make sure, that all the necessary scripts are loaded by
  896. Vim.
  897. After restarting Vim, you can now use your custom command definition
  898. :AvgCol. Use a range, for the number of lines you want to evaluate and
  899. optionally use an argument to specify which column you want to be
  900. evaluated >
  901. :2,$AvgCol 7
  902. <
  903. This will evaluate the average of column seven (assuming, line 1 is the
  904. header line, which should not be taken into account).
  905. 6.4 Autocommand on opening/closing files *csv-arrange-autocmd*
  906. ----------------------------------------
  907. If you want your CSV files to always be displayed like a table, you can
  908. achieve this using the |ArrangeColumn_CSV| command and some autocommands.
  909. Define these autocommands in your |.vimrc| >
  910. aug CSV_Editing
  911. au!
  912. au BufRead,BufWritePost *.csv :%ArrangeColumn
  913. au BufWritePre *.csv :%UnArrangeColumn
  914. aug end
  915. Upon Entering a csv file, Vim will visually arrange all columns and before
  916. writing, those columns will be collapsed again. The BufWritePost autocommand
  917. makes sure, that after the file has been written successfully, the csv file
  918. will again be visually arranged.
  919. You can also simply set the variable >
  920. let g:csv_autocmd_arrange = 1
  921. <
  922. in your vimrc and an autocmd will be installed, that visually arranges your
  923. csv file whenever you open them for editing.
  924. Note, this is highly experimental and especially on big files, this might
  925. take a while.
  926. 6.5 Syntax error when opening a CSV file *csv-syntax-error*
  927. ----------------------------------------
  928. This happens usually, when the syntax script is read before the filetype
  929. plugin, so the plugin did not have a chance to setup the column delimiter
  930. correctly.
  931. The easy way to fix it, is to reverse the order of the :syntax on (|:syn-on|)
  932. and :filetype plugin (|:filetype-plugin-on|) statements in your |.vimrc|
  933. Alternatively, you can simply call |InitCSV| and ignore the error.
  934. ==============================================================================
  935. 7. CSV Changelog *csv-changelog*
  936. 0.26 Jul 25, 2012 {{{1
  937. - Better handling of setting filetype specific options
  938. - |CSV-Tabularize|
  939. - fix some small errors
  940. 0.25 May 17, 2012 {{{1
  941. - |SearchInColumn_CSV| should match non-greedily, patch by Matěj Korvas,
  942. - better argument parsing for |SearchInColumn_CSV|, patch by Matěj Korvas,
  943. thanks!
  944. 0.24 Apr 12, 2012 {{{1
  945. - Allow to transpose the file (|csv-transpose|, suggested by Karan Mistry,
  946. thanks!)
  947. - |DeleteColumn_CSV| allows to specify a search pattern and all matching
  948. columns will be deleted (suggested by Karan Mistry, thanks!)
  949. 0.23 Mar 25, 2012 {{{1
  950. - Don't error out, when creating a new file and syntax highlighting
  951. script can't find the delimiter
  952. (ftplugin will still give a warning, so).
  953. - Don't pollute the search register when loading a file
  954. - Give Warning when number format is wrong
  955. - Don't source ftdetect several times (patch by Zhao Cai, thanks!)
  956. - |NewDelimiter_CSV| to change the delimiter of the file
  957. - |Duplicate_CSV| to check for duplicate records in the file
  958. - Issue https://github.com/chrisbra/csv.vim/issues/13 fixed (missing quote,
  959. reported by y, thanks!)
  960. - |CSVPat()| function
  961. - 'lz' does not work with |:silent| |:s| (patch by Sergey Khorev, thanks!)
  962. - support comments (|csv_comment|, suggested by Peng Yu, thanks!)
  963. 0.22 Nov 08, 2011 {{{1
  964. - Small enhancements to |SumCol_CSV|
  965. - :Filters! reapplys the dynamic filter
  966. - Apply |csv-aggregate-functions| only to those values, that are
  967. not folded away.
  968. - |SumCol_CSV| can use a different number format (suggested by James Cole,
  969. thanks! (also |csv-nrformat|
  970. - Documentation updates (suggested by James Cole and Peng Yu)
  971. - More code cleanup and error handling
  972. https://github.com/chrisbra/csv.vim/issues/9 reported Daniel Carl, thanks!
  973. https://github.com/chrisbra/csv.vim/issues/8 patch by Daniel Carl, thanks!
  974. - New Command |NewRecord_CSV| (suggest by James Cole, thanks!)
  975. - new textobjects InnerField (if) and outerField (af) which contain the field
  976. without or with the delimiter (suggested by James Cole, thanks!)
  977. - |csv-arrange-autocmd| to let Vim automatically visually arrange the columns
  978. using |ArrangeColumn_CSV|
  979. - |csv-move-folds| let Vim move folded lines to the end
  980. - implement a Menu for graphical Vim
  981. 0.21 Oct 06, 2011 {{{1
  982. - same as 0.20 (erroneously uploaded to vim.org)
  983. 0.20 Oct 06, 2011 {{{1
  984. - Implement a wizard for initializing fixed-width columns (|CSVFixed|)
  985. - Vertical folding (|VertFold_CSV|)
  986. - fix plugin indentation (by Daniel Karl, thanks!)
  987. - fixed missing bang parameter for HiColumn function (by Daniel Karl, thanks!)
  988. - fixed broken autodection of delimiter (reported by Peng Yu, thanks!)
  989. 0.19 Sep 26, 2011 {{{1
  990. - Make |:ArrangeColumn| more robust
  991. - Link CSVDelimiter to the Conceal highlighting group for Vim's that have
  992. +conceal feature (suggested by John Orr, thanks!)
  993. - allow the possibility to return the Column name in the statusline |csv-stl|
  994. (suggested by John Orr, thanks!)
  995. - documentation updates
  996. - Allow to dynamically add Filters, see |csv-filter|
  997. - Also display what filters are active, see |:Filter|
  998. - Analyze a column for the distribution of a value |csv-analyze|
  999. - Implement UnArrangeColumn command |UnArrangeColumn_CSV|
  1000. (suggested by Daniel Karl in https://github.com/chrisbra/csv.vim/issues/7)
  1001. 0.18 Aug 30, 2011 {{{1
  1002. - fix small typos in documentation
  1003. - document, that 'K' and 'J' have been remapped and the originial function is
  1004. available as \K and \J
  1005. - Delimiters should not be highlighted within a column, only when used
  1006. as actual delimiters (suggested by Peng Yu, thanks!)
  1007. - Performance improvements for |:ArrangeColumn|
  1008. 0.17 Aug 16, 2011 {{{1
  1009. - small cosmetic changes
  1010. - small documentation updates
  1011. - fold away changelog in help file
  1012. - Document, that |DeleteColumn_CSV| deletes the column on which the cursor
  1013. is, if no column number has been specified
  1014. - Support csv fixed width columns (|csv-fixedwidth|)
  1015. - Support to interactively convert your csv file to a different
  1016. format (|csv-convert|)
  1017. 0.16 Jul 25, 2011 {{{1
  1018. - Sort on the range, specified (reported by Peng Yu, thanks!)
  1019. - |MoveCol_CSV| to move a column behind another column (suggested by Peng Yu,
  1020. thanks!)
  1021. - Document how to use custom functions with a column
  1022. (|csv-aggregate-functions|)
  1023. - Use g:csv_highlight_column variable, to have Vim automatically highlight the
  1024. column on which the cursor is (|csv-hicol|)
  1025. - Header/VHeader command should work better now (|Header_CSV|, |VHeader_CSV|)
  1026. - Use setreg() for setting the register for the |Column_CSV| command and make
  1027. sure it is blockwise.
  1028. - Release 0.14 was not correctly uploaded to vim.org
  1029. 0.14 Jul 20, 2011 {{{1
  1030. - really use g:csv_no_conceal variable (reported by Antonio Ospite, thanks!)
  1031. - Force redrawing before displaying error messages in syntax script (reported
  1032. by Antonio Ospite, thanks!)
  1033. - Make syntax highlighting work better with different terminals (Should work
  1034. now with 8, 88 and 256 color terminals, tested with linux konsole, xterm and
  1035. rxvt) (https://github.com/chrisbra/csv.vim/issues/4)
  1036. - Automatically detect '|' as field separator for csv files
  1037. 0.13 Mar 14, 2011 {{{1
  1038. - documentation update
  1039. - https://github.com/chrisbra/csv.vim/issues#issue/2 ('splitbelow' breaks
  1040. |Header_CSV|, fix this; thanks lespea!)
  1041. - https://github.com/chrisbra/csv.vim/issues#issue/3 ('gdefault' breaks
  1042. |ArrangeColumn_CSV|, fix this; thanks lespea!)
  1043. - https://github.com/chrisbra/csv.vim/issues#issue/1 (make syntax highlighting
  1044. more robust, thanks lespea!)
  1045. - fix some small annoying bugs
  1046. - WhatColumn! displays column name
  1047. 0.12 Feb 24, 2011 {{{1
  1048. - bugfix release:
  1049. - don't use |:noa| when switching between windows
  1050. - make sure, colwidth() doesn't throw an error
  1051. 0.11 Feb 24, 2011 {{{1
  1052. - new command |Copy_CSV|
  1053. - |Search_CSV| did not find anything in the last column if no delimiter
  1054. was given (reported by chroyer)
  1055. - |VHeader_CSV| display the first column as Header similar to how
  1056. |Header_CSV| works
  1057. - |HeaderToggle_CSV| and |VHeaderToggle_CSV| commands that toggle displaying
  1058. the header lines/columns
  1059. 0.10 Feb 23, 2011 {{{1
  1060. - Only conceal real delimiters
  1061. - document g:csv_no_conceal variable
  1062. - document g:csv_nl variable
  1063. - document conceal feature and syntax highlighting
  1064. - Normal mode command <Up>/<Down> work like K/J
  1065. - More robust regular expression engine, that can also handle newlines inside
  1066. quoted strings.
  1067. - Slightly adjusted syntax highlighting
  1068. 0.9 Feb 19, 2011 {{{1
  1069. - use conceal char depending on encoding
  1070. - Map normal mode keys also for visual/select and operator pending mode
  1071. 0.8 Feb 17, 2011 {{{1
  1072. - Better Error handling
  1073. - HiColumn! removes highlighting
  1074. - Enable NrColumns, that was deactivated in v.0.7
  1075. - a ColorScheme autocommand makes sure, that the syntax highlighting is
  1076. reapplied, after changing the colorscheme.
  1077. - SearchInColumn now searches in the current column, if no column has been
  1078. specified
  1079. - A lot more documentation
  1080. - Syntax Highlighting conceales delimiter
  1081. - small performance improvements for |ArrangeColumn_CSV|
  1082. 0.7 Feb 16, 2011 {{{1
  1083. - Make the motion commands 'W' and 'E' work more reliable
  1084. - Document how to setup filetype plugins
  1085. - Make |WhatColumn_CSV| work more reliable (report from
  1086. http://vim.wikia.com/Script:3280)
  1087. - DeleteColumn deletes current column, if no argument given
  1088. - |ArrangeColumn_CSV| handles errors better
  1089. - Code cleanup
  1090. - Syntax highlighting
  1091. - 'H' and 'L' move forward/backwards between csv fields
  1092. - 'K' and 'J' move upwards/downwards within the same column
  1093. - |Sort_CSV| to sort on a certain column
  1094. - |csv-tips| on how to colorize the statusline
  1095. 0.6 Feb 15, 2011 {{{1
  1096. - Make |ArrangeColumn_CSV| work more reliable (had problems with multibyte
  1097. chars before)
  1098. - Add |Header_CSV| function
  1099. - 'W' and 'E' move forward/backwards between csv fields
  1100. - provide a file ftdetect/csv.vim to detect csv files
  1101. 0.5 Apr 20 2010 {{{1
  1102. - documentation update
  1103. - switched to a public repository: http://github.com/chrisbra/csv.vim
  1104. - enabled GLVS (see |GLVS|)
  1105. 0.4a Mar 11 2010 {{{1
  1106. - fixed documentation
  1107. 0.4 Mar 11 2010 {{{1
  1108. - introduce |InitCSV|
  1109. - better Error handling
  1110. - HiColumn now by default highlights the current column, if no argument is
  1111. specified.
  1112. 0.3 Oct, 28 2010 {{{1
  1113. - initial Version
  1114. vim:tw=78:ts=8:ft=help:norl:et:fdm=marker:fdl=0