Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the mysql cli in edit mode (\e).

> Select * from \e

Goes to vim and I finish typing my command.

Select * from test_table;

Only to realize I've forgotten to use the database I need. Is there a way I can preserve this script in edit mode, switch back to the regular mysql mode, run my command, and then come back to edit?

Another example would be: I forget how a column name is spelled and I need to look it up after I've started typing a command in edit mode.


share|improve this question
up vote 1 down vote accepted

Well, sort of.

In vim, save your work to a different file...

:w myfile.sql [enter]

Then exit vim with an artificial error so the mysql client won't bother to re-read the temporary file.

:cq [enter]

Then clear the input buffer in mysql.

\c [enter]

Then do what you've gotta do.

Then re-launch vim

\e [enter]

The editor will have your last command in it. Clear that out and then paste the temporary file back into the buffer.

:r myfile.sql [enter]

Keep editing, then save and exit and your new content will be returned to mysql for execution.

share|improve this answer
It sounds like there is no other way... although I thought I remembered an old colleague doing something without having to use your method. If no one else offers an answer in a couple days, I'll mark yours as the correct answer. Thanks! – Steve Tauber Sep 1 '13 at 18:40
Sounds good. I checked the source of client/ and didn't see anything but if someone else has ideas for alternate approaches, I'll be interested to see them, too. – Michael - sqlbot Sep 2 '13 at 2:07
Thank you again for your help :) – Steve Tauber Sep 3 '13 at 1:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.