在 bash 中,如何将功能键绑定到命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4200800/
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
In bash, how do I bind a function key to a command?
提问by SiegeX
Example: I want to bind the F12key to the command echo "foobar"
such that every time I hit F12the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How does one go about this?
示例:我想将F12键绑定到命令,echo "foobar"
这样每次我点击F12消息“foobar”时都会打印到屏幕上。理想情况下,它可以是任意的 shell 命令,而不仅仅是内置命令。如何解决这个问题?
回答by Paused until further notice.
You can determine the character sequence emitted by a key by pressing Ctrl-vat the command line, then pressing the key you're interested in. On my system for F12, I get ^[[24~
. The ^[
represents Esc. Different types of terminals or terminal emulators can emit different codes for the same key.
您可以通过在命令行按Ctrl-v然后按您感兴趣的键来确定某个键发出的字符序列。在我的系统上F12,我得到^[[24~
. 该^[
代表Esc。不同类型的终端或终端仿真器可以为同一个密钥发出不同的代码。
At a Bash prompt you can enter a command like this to enable the key macro so you can try it out.
在 Bash 提示符下,您可以输入这样的命令来启用密钥宏,以便您可以尝试一下。
bind '"\e[24~":"foobar"'
Now, when you press F12, you'll get "foobar" on the command line ready for further editing. If you wanted a keystroke to enter a command immediately, you can add a newline:
现在,当您按下 时F12,您将在命令行上看到“foobar”,可以进行进一步编辑。如果您希望按键立即输入命令,您可以添加一个换行符:
bind '"\e[24~":"pwd\n"'
Now when you press F12, you'll get the current directory displayed without having to press Enter. What if you've already typed something on the line and you use this which automatically executes? It could get messy. However, you could clear the line as part of your macro:
现在,当您按 时F12,无需按 即可显示当前目录Enter。如果您已经在线路上输入了一些东西并且您使用它自动执行怎么办?它可能会变得混乱。但是,您可以清除该行作为宏的一部分:
bind '"\e[24~":"\C-k \C-upwd\n"'
The space makes sure that the Ctrl-uhas something to delete to keep the bell from ringing.
该空间确保Ctrl-u有一些东西可以删除以防止铃声响起。
Once you've gotten the macro working the way you want, you can make it persistent by adding it to your ~/.inputrc
file. There's no need for the bind
command or the outer set of single quotes:
一旦您让宏按照您想要的方式工作,您就可以通过将它添加到您的~/.inputrc
文件来使其持久化。不需要bind
命令或外部单引号集:
"\e[24~":"\C-k \C-upwd\n"
Edit:
编辑:
You can also create a key binding that will execute something without disturbing the current command line.
您还可以创建一个键绑定,在不干扰当前命令行的情况下执行某些操作。
bind -x '"\eW":"who"'
Then while you're typing a command that requires a username, for example, and you need to know the names of user who are logged in, you can press Alt-Shift-Wand the output of who
will be displayed and the prompt will be re-issued with your partial command intact and the cursor in the same position in the line.
然后,当您键入需要用户名的命令时,例如,您需要知道登录用户的姓名,您可以按Alt- Shift-将显示W输出who
并重新发出提示您的部分命令完好无损并且光标位于行中的相同位置。
Unfortunately, this doesn't work properly for keys such as F12which output more than two characters. In some cases this can be worked around.
不幸的是,这对于诸如F12输出两个以上字符的键不能正常工作。在某些情况下,这可以解决。
The command (who
in this case) could be any executable - a program, script or function.
命令(who
在本例中)可以是任何可执行文件——程序、脚本或函数。
回答by Barton Chittenden
You can define bash key bindings in ~/.inputrc
(configuration file for the GNU Readline library). The syntax is
您可以在~/.inputrc
(GNU Readline 库的配置文件)中定义 bash 键绑定 。语法是
<keysym or key name>: macro
<keysym 或键名>:宏
for example:
例如:
Control-o: "> output"
will create a macro which inserts "> output" when you press ControlO
将创建一个宏,当您按下时插入“> 输出” ControlO
"\e[11~": "echo foobar"
will create a macro which inserts "echo foobar" when you press F1... I don't know what the keysym for F11is off hand.
将创建一个宏,当你按下时插入“echo foobar” F1......我不知道什么键符F11是现成的。
Edit:
编辑:
.inputrc
understands the \n
escape sequence for linefeed, so you can use
.inputrc
了解\n
换行符的转义序列,因此您可以使用
"\e[11~": "echo foobar\n"
Which will effectively 'press enter' after the command is issued.
发出命令后,这将有效地“按回车”。
回答by Wesley Rice
This solution is specific to X11 environments and has nothing to do with bash, but adding the following to your .Xmodmaps
此解决方案特定于 X11 环境,与 bash 无关,但将以下内容添加到您的 .Xmodmaps
% loadkeys
keycode 88 = F12
string F12 = "foobar"
%
will send the string "foobar" to the terminal upon hitting F12.
按下 F12 后会将字符串“foobar”发送到终端。
回答by Peter Perhá?
I wanted to bind Ctrl+B
to a command. Inspired by an answer above, I tried to use bind
but could not figure out what series of cryptic squiggles (\e[24~
?) translate to Ctrl+B
.
我想绑定Ctrl+B
到一个命令。受上述答案的启发,我尝试使用bind
但无法弄清楚哪些系列的神秘曲线(\e[24~
?)转换为Ctrl+B
.
On a Mac, go to Settings of the Terminal app, Profiles -> Keyboard -> +
then press the keyboard shortcut you're after and it comes out. For me Ctrl+B
resulted in \002
which i successfully bound to command
在 Mac 上,转到终端应用程序的设置,配置文件 -> 键盘 ->+
然后按你想要的键盘快捷键,它就会出现。对我来说Ctrl+B
导致\002
我成功绑定到命令
bind '"\002":"echo command"'
bind '"\002":"echo command"'
Also, if you want the command to be executed right-away (not just inserted in to the prompt), you can add the Enter to the end of your command, like so:
此外,如果您希望立即执行命令(而不仅仅是插入到提示中),您可以在命令的末尾添加 Enter,如下所示:
bind '"\002":"echo command\015"'
bind '"\002":"echo command\015"'