Here's the solution (It's a slightly modified version of the code found here: http://superuser.com/questions/266934/how-to-pass-vim-buffer-contents-through-shell-command-and-capture-the-output-to):
Paste the above code into your .vimrc, open a new vim, and...:function! FilterToNewWindow(script)let TempFile = tempname()let SaveModified = &modifiedexe 'w ' . TempFilelet &modified = SaveModifiedexe 'split ' . TempFileexe '.! ' . a:scriptexe 'setlocal buftype=nofile'exe 'setlocal bufhidden=hide'exe 'setlocal noswapfile'exe 'setlocal noet'exe 'setlocal ts=25'exe 'setlocal number'endfunctioncommand! MF call FilterToNewWindow('mysql --defaults-file=/etc/mysql/debian.cnf -t <DATABASENAME>')
- Enter a SQL command into current buffer
- Do :MF at the line you wish to use in mysql
- A new buffer will open up with the results of your query. (Actually, of course, it's the other way around, but who cares...)
- This buffer will be a scratch buffer, so you don't get an ugly error message when doing :q
You should use the mysql defaults file, otherwise your mysql credentials will end up in (I guess) your bash history. The -t argument will make mysql export its results in the nice table format used in the mysql command line client. setlocal number, noet and ts (tabsize) are not necessary, but I want them in my scratch buffer anyway.
Of course, you can use FilterToNewWindow() with all kinds of external programs. I'm sure I will put it to good use in the future.