MySQL 命令行客户端中的自动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8332338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:52:25  来源:igfitidea点击:

Autocompletion in the MySQL command-line client

mysqlautocompleteconsole

提问by Alan B. Dee

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

在 Linux 和许多其他系统中,在终端导航时,您可以按Tab自动完成目录或文件名。

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

我想知道 MySQL 终端中是否有类似的东西。例如,如果我想获得someTableWithRidiculousLongName我可以输入的描述,describe someTableW然后Tab它会自动完成其余部分。

Does anything like that exist in the MySQL terminal?

MySQL终端中是否存在类似的东西?

回答by T Zengerink

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

编辑或创建一个.my.cnf在您的主目录中调用的文件,其中包含:

[mysql]
auto-rehash

回答by catmantiger

To enable autocomplete within the MySQL prompt type:

要在 MySQL 提示中启用自动完成,请键入:

mysql> \#

After that you can type:

之后,您可以键入:

mysql> describe someTableW[TAB]

To get:

要得到:

mysql> describe someTableWithRidiculousLongName

回答by rabudde

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

使用附加选项启动 MySQL 控制台--auto-rehash,即

mysql --auto-rehash -u root -p

回答by mateuszlewko

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.

我知道这是一个老问题,但我发现具有高级自动完成功能的 MySql cli 客户端非常有用: mycli。它比内置的自动重新哈希功能要聪明得多。

回答by pob

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

在 OS X 10.11.6 上,我如上所述设置了 --auto-rehash,但它不起作用。(这是 OS X,所以 mysql 是用 BSD libedit库编译的。)

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

然后我想起我已经通过创建 ~/.editrc 为 mysql 客户端设置了 vi 键绑定,其中包含一行:bind -v。这对于在 mysql 客户端中为我提供类似 vi 的导航非常有用,但它破坏了列名完成(我能够通过删除.editrc来验证这一点)。

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

所以我研究了一下,发现 ~/.editrc 至少应该有以下几行:

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

使用此附加行,名称完成在 mysql 中可以正常工作,并且类似 vi 的导航也可以工作。(还有其他 .editrc 设置可以极大地改善 mysql 客户端导航,但这不是开始讨论的地方。)

回答by Roderick Decker

Some notes about auto-rehash:

关于自动重新哈希的一些注意事项:

When you enable autocompletion editing the mysql config file..

当您启用自动完成编辑 mysql 配置文件时..

[mysql]
auto-rehash

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

您可以为所有用户或仅为一个用户执行此操作:

/etc/my.cnf: All Users

/etc/my.cnf: 全部用户

~/.my.cnf: Actual user

~/.my.cnf: 实际用户

You can also disable autocompletion adding:

您还可以禁用自动完成添加:

no-auto-rehash

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

摘自:http: //www.sysadmit.com/2016/08/linux-mysql-autocompletar.html

回答by untill

You can also auto-complete based on the command history. Start typing, then invoke the keys which are bound to ed-search-prev-historyand 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:

您还可以根据命令历史记录自动完成。开始输入,然后调用绑定到ed-search-prev-history和的键ed-search-next-history。这适用于 mysql 附带 libedit 支持的情况。默认键绑定是 Ctrl-P 和 Ctrl-N,但这可以在 .editrc 中自定义。我的 Ctrl-up 和 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-backwardand history-search-forwardare the correct commands. Configuration then was by means of .inputrc. Same example as above:

以前mysql是基于readline的,然后history-search-backwardhistory-search-forward都是正确的命令。然后通过 .inputrc 进行配置。与上面相同的示例:

# 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 seland invoke Ctrl-Up, select * from some_long_table_namewould come up if that is a command I have used earlier.

因此,假设您开始输入sel并调用 Ctrl-Up,select * from some_long_table_name如果这是我之前使用过的命令,就会出现。