Skip to content

Commit 4b71781

Browse files
Add simplified instructions for neophytes (like me) (#11)
1 parent 91c8a8b commit 4b71781

File tree

1 file changed

+111
-44
lines changed

1 file changed

+111
-44
lines changed

README.md

Lines changed: 111 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ mysql Ver 14.14 Distrib 5.7.16, for osx10.11 (x86_64) using EditLine wrapper
1515

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

18-
## Install
18+
## Installation Options
1919

20-
- Vundle (Recommended)
20+
1. [Install with Vundle](#install-with-vundle) (recommended)
21+
2. [Install with Plug](#install-with-plug) (for neovim)
22+
3. [Manually Install](#manually-install)
23+
24+
### Install with Vundle
25+
(recommended)
2126

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

@@ -33,7 +38,8 @@ call vundle#end()
3338

3439
And remember to execute `:PluginInstall` in VIM normal mode.
3540

36-
- Plug (for neovim)
41+
### Install with Plug
42+
(for neovim)
3743

3844
Add the following to `~/.config/nvim/init.vim`:
3945
```vimrc
@@ -44,84 +50,145 @@ Plugin 'https://github.com/kezhenxu94/vim-mysql-plugin.git'
4450

4551
Then run `:PlugInstall`.
4652

53+
### Manually Install
4754

48-
## How does it work
49-
50-
For the sake of security and convenience, this plugin utilizes a command line parameter of MySQL client called `defaults-group-suffix`, for more details about `defaults-group-suffix`, check out the documentation [here](https://dev.mysql.com/doc/refman/5.5/en/option-file-options.html#option_general_defaults-group-suffix); but now just put your configuration in the file `~/.my.cnf` like this:
55+
Be sure you have `git` installed and configured to [authenticate to github.com via ssh](https://docs.github.com/en/authentication/connecting-to-github-with-ssh).
56+
In your terminal...issue the following commands, one by one (this assumes you have `git` installed and configured):
5157

52-
```conf
53-
[ClientExampleTest]
54-
host = localhost
55-
user = root
56-
password = root
57-
default_character_set = utf8
58-
database = mysql
59-
60-
[ClientExampleProd]
61-
host = localhost
62-
user = root
63-
password = root
64-
default_character_set = utf8
65-
database = mysql
58+
```
59+
cd ~;
60+
git clone [email protected]:kezhenxu94/vim-mysql-plugin.git;
61+
ls -lah ~/vim-mysql-plugin/plugin/vim-mysql-plugin.vim;
62+
echo "let mapleader = '\'" >> ~/.vimrc;
63+
echo "source ~/vim-mysql-plugin/plugin/vim-mysql-plugin.vim" >> ~/.vimrc;
6664
```
6765

68-
**Note**: if you already use `.my.cnf`, then add the new contents 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:
66+
## Usage
6967

70-
```conf
71-
[client]
72-
user = mymysqlmamaria
73-
password = neveryoumind
68+
There are two things to do after installation:
69+
70+
1. Put your database credentials in `~/my.cnf`
7471

75-
# ↑ that config was there before
76-
# ↓ this config is what we added
77-
[clientMyDb]
78-
database = mydb
7972
```
73+
[client]
74+
user=your_user_here
8075
81-
## Usage
76+
[clientAnysuffix]
77+
database=mydb"
78+
```
8279

83-
Create a new file whose name ends with `.sql`, adding the following three lines in the very beginning of the file:
80+
2. Use vim to issue mysql commands!
81+
82+
- `vim anyfile` (so that you can type your sql)
83+
- typically using a file with name ending in `.sql` is best (for syntax highlighting)
84+
- at the top of the file you may put command line args (but one is mandatory)
85+
- the `--defaults-group-suffix` has to be there (at least the `database` parameter must be set)
86+
- the suffix is a personal choice but it must match what is in your my.cnf (see "Anysuffix" in example)
87+
- [read more about defaults-group-suffix](https://dev.mysql.com/doc/refman/5.5/en/option-file-options.html#option_general_defaults-group-suffix)
88+
- the `--login-path` option is useful
89+
- this can be set to makes use of `mysql_config_editor` created `my_login.cnf` files
90+
- the syntax for this is `--login-path=<configured db path>` for instance `--login-path=myMysql`
91+
- the `-t` switch sets the output to table
92+
- omit this to get raw, tabbed output
93+
- omit this and replace the semi-colon at the end of the query with '\G' to get vertical format
94+
- each mysql option **must** be on its own line
8495

85-
```sql
86-
-- --defaults-group-suffix=ExampleTest
96+
```
97+
-- --defaults-group-suffix=Anysuffix
8798
-- -t
8899
--
100+
101+
SELECT * FROM USER;
102+
```
103+
- Query `SELECT * FROM USER` with these keystrokes _(`<CR>` is carriage return/"enter")_:
104+
105+
```
106+
/SELECT<CR>
107+
V
108+
\rs
89109
```
90110

91-
Notes:
111+
The following is a description of the commands including an explanation of the `\SELECT<CR>V\rs` sequence:
92112

93-
- The first line with `--default-group-suffix` identifies the configuration **suffix**, defined above. i.e. we're entering `ExampleTest` not `ClientExampleTest`. To use the 2nd configuration example you'd use `--default-group-suffix=MyDb`.
113+
- `/SELECT` then *<CR>* moves your cursor (via *search*) to the query
114+
- `V` *shift+v* selects the entire query (line)
115+
- `\rs` issues `<leader>`+rs
116+
- earlier we set `mapleader` to backslash _(change it in .vimrc)_
94117

95-
- The -t option means output as a table. You can ommit this if you want bare output.
118+
Query results appear in a split pane.
96119

97-
- Each mysql option **must** be on its own line.
120+
Remember to delimit your queries with semi-colons.
98121

99-
Then add your sql statements following the three lines. Here is a sample of the `sql` file:
122+
### Command Reference
100123

101-
```sql
124+
- `<leader>rr` "Run Instruction"
125+
- `<leader>ss` "Select Cursor Table"
126+
- `<leader>ds` "Descript Cursor Table"
127+
- `<leader>rs` "Run Selection"
128+
- `<leader>re` "Run Explain"
129+
130+
"Run Instruction" executes query and can be run from anywhere within the query.
131+
132+
"Explain" can be run from anywhere within the query.
133+
134+
"Selection" means select _query_ before issuing command.
135+
136+
"Cursor" means place your cursor on _the table_ to issue command.
137+
138+
"Selection" means select _query_ before issuing command.
139+
140+
### Usage Notes
141+
142+
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:
143+
144+
```conf
145+
[client]
146+
user = mymysqlmamaria
147+
password = neveryoumind
148+
149+
# ↑ that config was there before
150+
# ↓ this config is what we added
151+
[clientMyDb]
152+
database = mydb
153+
```
154+
155+
This is because `database` must be set, when the query is issued the database must already be selected.
156+
157+
Remember, add your sql statements following the three lines. Here is another sample `sql.sql` file:
158+
159+
```sql.sql
102160
-- --defaults-group-suffix=ExampleTest
103161
-- -t
104162
--
105163

106164
SELECT * FROM USER;
107165
```
108166

109-
- Move the caret to the line `select * from user;` and type `<leader>rr` in VIM normal mode to run the line;
167+
Here are more examples of how to run `SELECT * FROM USER:`
110168

111-
- Move the caret to the table name (such as `user`) and type `<leader>ds` (stands for "Descript") to show the columns of the table, type `<leader>ss` to `SELECT *` from the table;
169+
- Position caret/cursor on line `SEELCT * FROM USER;` and (in VIM normal mode) type `<leader>rr`
170+
- if the query does not run, but instead a replacement of the character underneath the cursor occurs your `<leader>` is not set
171+
- to set `<leader>` to the recommended backslash place `let mapleader = '\'` in your `.vimrc`
112172

113-
- Using VIM visual mode to select a range of statement and type `<leader>rs` to execute the selected statements;
173+
- Position caret/cursor on (within) the table name (`USER`) and type `<leader>ds` (stands for "Descript") to show the columns of the table
174+
- Type `<leader>ss` to select all from the `USER` table
114175

115-
> `<leader>` is the "leading key" when mapping a shortcut to a specific function, the default `<leader>` may be `\`
176+
- Using VIM visual mode, select a range of statement and type `<leader>rs` to execute the selected statements
177+
- the results (if multiple queries selected) will stack in the result window
116178

117-
After typing the shortcut the VIM window will be splitted into two, the bottom of which will show the result of the statement;
179+
Remember, after typing the shortcut the VIM window will be splitted into two, the bottom of which will show the result of the statement.
180+
Switch windows by typing [Cntl-W]+[W]. [Read more about split window navigation in VIM](http://vimdoc.sourceforge.net/htmldoc/windows.html#window-move-cursor)
181+
182+
Remember to delimit your queries with semi-colons.
118183

119184
## Contribution
120185

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

123188
## Change log
124189

190+
- Added a simplified install sequence with some descriptions that may be useful for those just getting started in vim.
191+
125192
- Security improvement: all shell commands are escaped with shellescape(). This means MySQL command options must now be one-per-line.
126193

127194
- Security improvement: Previously SQL with double quotes `"` that was run with `<Leader>rr` would escape the shell argument, meaning the following code was run in the shell(!). This would potentially do very bad things.

0 commit comments

Comments
 (0)