101

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?

2
  • 4
    Try mycli, github.com/dbcli/mycli Commented Nov 19, 2016 at 3:47
  • Meanwhile there is another MySQL client, MySQL Shell released by Oracle, that has Auto Complete along side numerous modern goodies. Commented May 26, 2024 at 0:49

7 Answers 7

138

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

[mysql] auto-rehash 
Sign up to request clarification or add additional context in comments.

3 Comments

@galarant not necessarily, it better be optional
@rkmax - It still is optional with the --disable-auto-rehash flag. But now you've turned it on by default.
But, it does not work when I type the commands in small letter.
99

To enable autocomplete within the MySQL prompt type:

mysql> \# 

After that you can type:

mysql> describe someTableW[TAB] 

To get:

mysql> describe someTableWithRidiculousLongName 

4 Comments

why that is not the default?
simple and elegant
mysql> rehash - As an alternative to "\#", we can also type "rehash" mysql command as follows. Thought of sharing as "rehash" might be easier to remember for some people like me: mysql> rehash After that autocomplete will start working!
@OussamaElgoumri Because some of us like to paste complex queries into the command line, and these queries might contain tab characters for indentation.
66

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

mysql --auto-rehash -u root -p 

3 Comments

i dnt know why that does not work for me! the tab is being interpreted as a tab only and does nothing for auto completion.
Caution, tabcomplete only works after one is connected to a database as Otheus pointed out in unix.stackexchange.com/questions/270309/…
34

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.

3 Comments

Excellent alternative!
Oh yea, top notch answer
auto-rehash doesn't work on mine. Switched to mycli and boom.... worked like a pro.
7

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.)

Comments

3

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

Comments

2

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.

1 Comment

Thank you, your answer is gold to me! Your post is the only explanation I could find as to why my CLI no longer recognised my .inputrc file; as due to change from libreadline to libedit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.