Home

Awesome

vim-mysql-plugin

A highly customizable MySQL VIM plugin.

Mentioned in Awesome VIM

Prerequisite

This plugin works on the basis of MySQL client, therefore a MySQL client is required, use the following command to ensure that there is one available on your machine:

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.16, for osx10.11 (x86_64) using  EditLine wrapper

If the output is something like -bash: mysql: command not found, then you may need to install a MySQL client first.

Installation Options

  1. Install with Vundle (recommended)
  2. Install with Plug (for neovim)
  3. Manually Install

Install with Vundle

(recommended)

Add the following line to the ~/.vimrc file, after adding that, the file may look like this:

" ... some other configurations
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
" Add this plugin
Plugin 'git://github.com/kezhenxu94/vim-mysql-plugin.git'
call vundle#end()
" ... some other configurations

And remember to execute :PluginInstall in VIM normal mode.

Install with Plug

(for neovim)

Add the following to ~/.config/nvim/init.vim:

" ... some other configurations
Plugin 'https://github.com/kezhenxu94/vim-mysql-plugin.git'
" ... some other configurations

Then run :PlugInstall.

Manually Install

Be sure you have git installed and configured to authenticate to github.com via ssh. In your terminal...issue the following commands, one by one (this assumes you have git installed and configured):

cd ~;
git clone git@github.com:kezhenxu94/vim-mysql-plugin.git;
ls -lah ~/vim-mysql-plugin/plugin/vim-mysql-plugin.vim;
echo "let mapleader = '\'" >> ~/.vimrc;
echo "source ~/vim-mysql-plugin/plugin/vim-mysql-plugin.vim" >> ~/.vimrc;

Usage

There are two things to do after installation:

  1. Put your database credentials in ~/my.cnf
[client]
user=your_user_here

[clientAnysuffix]
database=mydb"
  1. Use vim to issue mysql commands!
-- --defaults-group-suffix=Anysuffix
-- -t
--

SELECT * FROM USER;
/SELECT<CR>
V
\rs

The following is a description of the commands including an explanation of the \SELECT<CR>V\rs sequence:

Query results appear in a split pane.

Remember to delimit your queries with semi-colons.

Command Reference

"Run Instruction" executes query and can be run from anywhere within the query.

"Explain" can be run from anywhere within the query.

"Selection" means select query before issuing command.

"Cursor" means place your cursor on the table to issue command.

"Selection" means select query before issuing command.

Usage Notes

If you already use .my.cnf, then add the new [clientAnySuffix] group at the end. As your configuration options will be read after the main [Client] ones, you do not need to repeat those if the values are the same, for example, to set up a section for a particular database using your normal credential, your .my.cnf might look like this:

[client]
user = mymysqlmamaria
password = neveryoumind

# ↑ that config was there before
# ↓ this config is what we added
[clientMyDb]
database = mydb

This is because database must be set, when the query is issued the database must already be selected.

Remember, add your sql statements following the three lines. Here is another sample sql.sql file:

-- --defaults-group-suffix=ExampleTest
-- -t
--

SELECT * FROM USER;

Here are more examples of how to run SELECT * FROM USER:

Remember, after typing the shortcut the VIM window will be splitted into two, the bottom of which will show the result of the statement. Switch windows by typing [Cntl-W]+[W]. Read more about split window navigation in VIM

Remember to delimit your queries with semi-colons.

Contribution

If you find it difficult to use this plugin, please open issues or help to improve it by creating pull requests.

Change log