Search code examples
mysqlvimcommand-lineeditorvi

Retrieve lost file using Vi in MySQL


I would like to know how to retrieve a file using Vi in MySQL. I logged in using:

mysql -uuser -p -hserver -A database

Then I do:

\e

The editor opens and I type my query of 200 lines, then I :wq and \G (if I save the file it says: /tmp/sql9SbYQZ saved) and I see the result.

Now, if I make a mistake or run a different query and I try to type \e again, the query is lost.

ll /tmp/sql9SbYQZ
ls: /tmp/sql9SbYQZ: No such file or directory

Is there a way to retrieve the lost file?


Solution

  • Here's what I added to my .vimrc in order to save the current query in case i made a mistake.

    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.

    Here's also a link you might like: http://vim.wikia.com/wiki/Open_the_last_edited_file