Autocompletion in the MySQL command-line client

MysqlAutocompleteConsole

Mysql Problem Overview


In Linux, and many other systems, when navigating the terminal you can press Tab to auto complete a directory or file name.

I'm wondering if there is anything like that in the MySQL terminal. For example, if I want to get the description of someTableWithRidiculousLongName I could type describe someTableW then Tab and it would auto-complete the rest.

Does anything like that exist in the MySQL terminal?

Mysql Solutions


Solution 1 - Mysql

Edit or create a file called .my.cnf in your home directory, containing:

[mysql]
auto-rehash

Solution 2 - Mysql

To enable autocomplete within the MySQL prompt type:

mysql> \#

After that you can type:

mysql> describe someTableW[TAB]

To get:

mysql> describe someTableWithRidiculousLongName

Solution 3 - Mysql

start MySQL console with additional option --auto-rehash, i.e.

mysql --auto-rehash -u root -p

Solution 4 - Mysql

I know this is an old question, but I've found very helpful MySql cli client with advanced autocompletion: mycli. It's much smarter than builtin auto-rehash feature.

Solution 5 - Mysql

On OS X 10.11.6 I set --auto-rehash as described above, but it did not work. (This is OS X so mysql is compiled with the BSD libedit library.)

Then I remembered that I had set vi key-bindings for mysql client by creating ~/.editrc, containing one line: bind -v. This works great for giving me vi-like navigation in mysql client, but it broke column name completion (I was able to verify this by removing .editrc).

So I researched a little bit and found that ~/.editrc should have at least the following lines:

bind -v
bind \\t rl_complete

With this additional line, name completion works correctly in mysql AND vi-like navigation works also. (There are other .editrc settings which greatly improve mysql client navigation, but this isn't the place to start that thread of discussion.)

Solution 6 - Mysql

Some notes about auto-rehash:

When you enable autocompletion editing the mysql config file..

[mysql]
auto-rehash

You can do it for all users or only for one user:

/etc/my.cnf: All Users

~/.my.cnf: Actual user

You can also disable autocompletion adding:

no-auto-rehash

Extracted from: http://www.sysadmit.com/2016/08/linux-mysql-autocompletar.html

Solution 7 - Mysql

You can also auto-complete based on the command history. Start typing, then invoke the keys which are bound to ed-search-prev-history and ed-search-next-history. This applies if mysql comes with libedit support. The default keybindings are Ctrl-P and Ctrl-N, but this can be customized in .editrc. My example for Ctrl-up and Ctrl-down:

# start typing, then press Ctrl-Up
bind "\e[1;5A" ed-search-prev-history
# start typing, then press Ctrl-Up, then Ctrl-Down
bind "\e[1;5B" ed-search-next-history

Previously, mysql was based on readline, and then history-search-backward and history-search-forward are the correct commands. Configuration then was by means of .inputrc. Same example as above:

# these are the key bindings for the readline library
# start typing, then press Ctrl-Up
"\e[1;5A": history-search-backward
# start typing, then press Ctrl-Up, then Ctrl-Down
"\e[1;5B": history-search-forward

So, say you started typing sel and invoke Ctrl-Up, select * from some_long_table_name would come up if that is a command I have used earlier.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlan B. DeeView Question on Stackoverflow
Solution 1 - MysqlT. ZengerinkView Answer on Stackoverflow
Solution 2 - MysqlcatmantigerView Answer on Stackoverflow
Solution 3 - MysqlrabuddeView Answer on Stackoverflow
Solution 4 - MysqlmateuszlewkoView Answer on Stackoverflow
Solution 5 - MysqlpobView Answer on Stackoverflow
Solution 6 - MysqlRoderick DeckerView Answer on Stackoverflow
Solution 7 - MysqluntillView Answer on Stackoverflow