I wanted to replace Tab character with the following, depending on what is on either side of the tab. This is for quickly converting pasted in text (to vim 7.4) from a spreadsheet (xls/xlsx) that has update requests. Some fields are obviously text. Others could be text or numeric.
This function (if/when it works) allows quick conversion of the spreadsheet spec into part of a SQL command. So, the user must look at the metadata and determine if the field is character or numeric. There are 4 case for each Tab and what is on either side of it: CC (char, char), CN (char, num), NC (num, char), and NN (num, num). I had no problem mapping each of these, but getting it into a function to perform these operations runs into trouble. I get a "trailing characters" error. Here is my script (including control characters so you can see):
$
:map <F1> :%s/^I/,/^M$
:map <F2> :%s/^I/,'/^M$
:map <F3> :%s/^I/',/^M$
:map <F4> :%s/^I/','/^M$
:map nn :%s/^I/,/^M$
:map nc :%s/^I/,'/^M$
:map cn :%s/^I/',/^M$
:map cc :%s/^I/','/^M$
:map qb :%s/^/'/^M$
:map qe :%s/$/'/^M$
:map sp :%s/''/' '/^M$
:map ac :%s/^I/','/g^M:%s/^/'/g^M:%s/$/'/g^M$
:map pr :%s/^.*$/(&),/g^MG$x^[$
$
:function! QuoteSQL(...)$
: let parsed = split(a:000[0], '\zs')$
: let i = 1$
: while i < len(a:000[0])$
: if parsed[i-1] == 'C' && parsed[i] == 'C'$
:normal %s/^I/','/^M$
: elseif parsed[i-1] == 'C' && parsed[i] == 'N'$
:normal %s/^I/',/^M$
: elseif parsed[i-1] == 'N' && parsed[i] == 'C'$
:normal %s/^I/,'/^M$
: else$
:normal %s/^I/,/^M$
: endif$
: let i=i+1$
: endwhile$
:endfunction$
The call looks like this:
:call QuoteSQL("NNCCCCNCCNN")
- Each N or C refers to the fields in order. Numeric fields get no quotes and character fields get single quotes. Like I said I had no problem mapping such commands a cc, cn, nc, and nn. Inside the function I could in fact echo things successfully like echo "CC" etc. When I tried to put the operation I want based on each condition it fails. If the 1st 2 in the comparison are C and C, then the Tab is replaced by ,
, etc. I will add steps to put needed quotes at the beginning and end of the lines as well. Then the parenthesis and commas. Imagine if you will that a char field will contain a 'C' and a numeric field will contain and 'N' (not a number I know - this is just for example to show output). Output looks like this:
('C','C',N,'C',N,N,'C'),
('C','C',N,'C',N,N,'C'),
.
.
How can I get my function to take the CNNNCNNCNCNCNNC type argument and apply it to the file I have opened in vim? Thanks.
{}
button to format your code.