Turn Vim into Excel: Tips for Editing Tabular Data (2013)
alangrow.com
alangrow.com
I'm using it every day for so many things, importing and exporting of data, calculations, writing offers to clients, there's an infinite amount of good things to be done with spreadsheets within your text document.
Embedding vim-style editing in an IDE or less extensible editor might get you 90% of what you want from Vim, but that 10% can be a dealbreaker depending on your workflow. Without easy extensibility, you might not be able to get that last 10% at all.
I'd say evil starts out a little better, maybe 95% out of the box, but now you have all the power of emacs to make up that last 5% and then start to go even further if you're not content then.
That being said, there are two features that Excel - or any other spreadsheet software for that matter - provides, which are missing from any Vim solution I have found:
1. Hiding overly long cells.
In most tables I see most of the cells have reasonably short lengths, but almost always there are a few overly long outliers. What I want is a way to manually set the column width and then all outliers cut-off. When vim's conceal feature came out I hoped someone would make a plugin to that effect, but as far as I know no one ever did. Looking into the problem myself a while ago, I found that it's not trivial to do.
2. Quick and easy filtering.
I find the column filters in Excel pretty useful. The first value they provide is that they give an overview what values are actually used in the column. Second value is to temporarily hide all the rows one is not interested in. They are also very easy to use. I would like to have some easy way to do that in Vim just for viewing and without actually manipulating the file.
- in2csv: extracts Excel worksheets into CSV
- csvsql: produces (and executes) the SQL needed to create a table and insert the data from a CSV
I found these scripts to be profoundly useful when working with govt data and needing to provide a automated workflow that collects and collates their Excel spreadsheets and converts them into CSV and SQLite databases.
There are tons of interactive tools that can be used with it too if you want that.
Excel is so much more than tabular data. It is a directed acyclic graph, a functional, reactive programming lanaguage with immutable data structures, and a display layer for the results of your computation.
So, for a CSV, you could do something like this in perl
:% !perl -F/,/ -ane '$total += $F[3] if $F[4] =~ /Merchandise/; END {print $total;}'
This command will filter every line in the vim buffer through the perl one-liner. The perl one-liner splits the line by comma, and adds the amount in the fourth column to the total amount if the fifth column contains the word "Merchandise". The lines in the vim buffer are replaced by the computed total. I actually use this to determine how much I spend for each category in my Discover Card statement after downloading it as a CSV.But I'm happily reaching out to gawk most of the time for CSV processing.
Is it possible to just use some random character, maybe even from different charset, to act as delimiter for importing/exporting tabular data?
In fact, the Microsoft version of CSV is a textbook example of how not to design a textual file format. Its problems begin with the case in which the separator character (in this case, a comma) is found inside a field. The Unix way would be to simply escape the separator with a backslash, and have a double escape represent a literal backslash. This design gives us a single special case (the escape character) to check for when parsing the file, and only a single action when the escape is found (treat the following character as a literal). The latter conveniently not only handles the separator character, but gives us a way to handle the escape character and newlines for free. CSV, on the other hand, encloses the entire field in double quotes if it contains the separator. If the field contains double quotes, it must also be enclosed in double quotes, and the individual double quotes in the field must themselves be repeated twice to indicate that they don't end the field.
The bad results of proliferating special cases are twofold. First, the complexity of the parser (and its vulnerability to bugs) is increased. Second, because the format rules are complex and underspecified, different implementations diverge in their handling of edge cases. Sometimes continuation lines are supported, by starting the last field of the line with an unterminated double quote — but only in some products! Microsoft has incompatible versions of CSV files between its own applications, and in some cases between different versions of the same application (Excel being the obvious example here).
About exporting using random characters, I have found that ~ (tilde) is almost never seen in actual data, hence it can be used as a field separator in most cases. Another safe choice is the | (pipe) symbol. (In Windows, you can change the field separator in the "Region Settings" - a rather weird place to have that setting.)
But even if they did, you still always need to handle the case where one might legitimately want to use the delimiter character inside a field value.
Using xlsx rules out vim of course, and much as I'm a fan of vim I think it's plain foolishness to endorse it as a spreadsheet program. Vim is fine for ad-hoc viewing and quick-and-dirty edits, especially if the file is full of numbers and uncomplicated text, but it's really easy to break a csv that has fancy escaping in it.
I have heard that there was a saying that the only job of a Mac air was to open emacs.