Example: I want to bind the F12 key to the command echo "foobar"
such that every time I hit F12 the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How does one go about this?
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
You can define bash key bindings in the .inputrc (configuration file for the GNU Readline library). The syntax is
<keysym or key name>: macro
for example:
will create a macro which inserts "> output" when you press
will create a macro which inserts "echo foobar" when you press F1... I don't know what the keysym for F11 is off hand.
This solution is specific to X11 environments and has nothing to do with bash, but adding the following to your .Xmodmaps
will send the string "foobar" to the terminal upon hitting F12.
You can determine the character sequence emitted by a key by pressing Ctrl-v at 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.At a Bash prompt you can enter a command like this to enable the key macro so you can try it out.
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:
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:
The space makes sure that the Ctrl-u has something to delete to keep the bell from ringing.
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 thebind
command or the outer set of single quotes:Edit:
You can also create a key binding that will execute something without disturbing the current command line.
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-W and 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.Unfortunately, this doesn't work properly for keys such as F12 which output more than two characters. In some cases this can be worked around.
The command (
who
in this case) could be any executable - a program, script or function.I wanted to bind
Ctrl+B
to a command. Inspired by an answer above, I tried to usebind
but could not figure out what series of cryptic squiggles (\e[24~
?) translate toCtrl+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 meCtrl+B
resulted in\002
which i successfully bound to commandbind '"\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:
bind '"\002":"echo command\015"'