Can I execute a MySQL query from Vim visual selection with output in a new buffer

linux, mysql, vim

Solution

The `Dbext` plugin supports this behavior.

Visually select the SQL statement, and run `:DBExecRangeSQL` to execute it.

The result will be returned into a new split at the bottom of your current viewport.

There are lots and lots of options for controlling the output window. See `:help dbext` for the glorious details.

Update 1.May.2012

Version 15.0 of the plugin has been released with this functionality built in.

The default `-t` flag can be overridden

Default setting:

let g:dbext_default_MYSQL_extra = '-t'

Overridden for batch setting

let g:dbext_default_MYSQL_extra = '--batch --raw'

Dbext hard-codes the `-t` option to MySQL, but if that line is removed from dbext.vim, on line 2278 in DB_MYSQL_execSql (of my current version), you can pass the --batch and --raw options:

:DBSetOption MYSQL_cmd_options='--batch --raw'

To restore tabular output:

:DBSetOption MYSQL_cmd_options='-t'

I tested this successfully on my installation.

Problem

Given a file which consists of multiple line MySQL queries, eg ``` SELECT foo, bar, etc FROM blah WHERE something or other LIMIT etc ``` Is there any way I can visually select a query in Vim, pipe it through MySQL, and see the query and result in a new buffer? Clarification: I don't want tabular output, but something that can be further processed in vim or imported into a spreadsheet (like the tab-separated output that you get from mysql --batch) (Ubuntu Linux).

Original source

Related problems