Save Last MySQL Command

Posted on Sunday May 06, 2012 / by Eric Potvin

When developing or debugging MySQL queries are not common if you don't want to display the query on one line. When using shell, and not a non GUI program, you might have to run the last command executed from the buffer (or temporary files) using the shortcut \e.

Now, if you accidentally overwrite this query without entering in the edit mode, you will have to paste again the query and modify all over again what you did. This can be painful and here's a solution.

The idea is very simple, map a command to save the current buffer to a specific file then if you make a mistake, you can always re-open the saved file.

nmap <F7> :w! /tmp/query.sql\| wq!<CR>

This will create a map to the F7 key (you can change it of course). So every time you open a file either using edit or \e, you change it use the F7 key.

This will save a backup of your current query to /tmp/query.sql and then save and close the temporary file. This way, if you make a mistake, you just re-open the backup file and try again.