Jesse Hogan
Jesse Hogan

Reputation: 3243

How to get mysql's edit (\e) feature to preserve formating on arbitrary lines

When using the mysql command line client, I like to use \e (edit) to put the previous sql statement into Vim for editing. When I do this, it contains all the nice line breaks and indentations I added from the last time I edited the sql. However, I can only get this feature with the very last query that I entered. If I use the up arrow key to find the sql I want to edit in Vim, then re-enter it, then use \e, I can get it back in Vim, but all line breaks and indentation I used to format the sql are lost.

How can I get my previous sql back in Vim, along with its nicely formatted indentations and line breaks, even if it isn't the very last line in mysql's command history?

Upvotes: 2

Views: 1887

Answers (2)

dotancohen
dotancohen

Reputation: 31471

When composing complex queries in the MySQL client using vim vim \e, you can actually save different versions of the query via :w foo.

The file will be stored in the current directory (the directory you were in when you ran mysql). Though this will not let you edit any arbitrary previous query with formatting intact, it will let you save and edit with formatting various canonical queries from which you could continue evolving your final query. I personally find this limitation to be helpful as it helps me structure the direction in which I develop the query.

Upvotes: 0

Peter Rincker
Peter Rincker

Reputation: 45107

As an alternative, executing queries from Vim instead of editing queries from mysql. There are quite a few plugins that help do this:

  • dbtext.vim - Provides database access to many DBMS (Oracle, Sybase, Microsoft, MySQL, DBI,..)
  • mysqlquery - Simple and Easy to use utility for executing MySQL queries
  • vim-simpledb - Execution of postgresql or mysql commands from vim buffer
  • vim-pipe - Send a vim buffer through a command and instantly see the output
  • clam.vim - A lightweight Vim plugin for working with shell commands

Both vim-pipe and clam.vim run any shell command and are not limited to mysql in any way.

I made my own mysql runner, Cupcake. However I never bothered packaging it up as a plugin as I found there where already many alternatives out there and it doesn't really add anything new.

Upvotes: 2

Related Questions