Is it possible to have an arbitrary amount of text placed next to the cursor on the command-line? I have heard that it is possible using Bash, but I am not sure how to accomplish it. It seems to be possible with readline, but the syntax of the shortcut seems to be rather obscure, as when I place it in my ~/.inputrc and use the shortcut, nothing appears in the terminal. Can anyone explain the correct syntax of the entries in ~/.inputrc?

  • Shift+Ctrl+C Shift+Ctrl+V Shift+Insert – user157230 May 9 '13 at 19:17
  • Please show us what you tried. – glenn jackman May 9 '13 at 19:33
  • @glennjackman Thanks, I've given an answer as well- it was meant to be 'an ask and answer my own question'! – user76204 May 9 '13 at 19:35
  • ah, missed that. Good one. – glenn jackman May 9 '13 at 19:39

When on the command-line it is certainly possible to do what you want, although the syntax of readline can be confusing: see the appendix at the end of the article for a short list of examples.

You can use the GNU readline library and create some macros and place them in your ~/.inputrc. Then you will be able to insert arbitrary strings into the terminal when you press a key combination. As noted in the bash manual :

In addition to command names, readline allows keys to be bound to a string that is inserted when the key is pressed (a macro).
The syntax for controlling key bindings in the inputrc file is simple. All that is required is the name of the command or the text of a macro and a key sequence to which it should be bound. The name may be specified in one of two ways: as a symbolic key name, possibly with Meta- or Control- prefixes, or as a key sequence.

Be careful when assigning shortcut combinations to arbitrary strings, as many keybindings are already set whether you are using Bash's emacs or vi mode (in addition to the independent tty bindings). See bind -P for a list of currently bound Bash shortcuts, and stty -a for the others in use.

You just need to place your macros in ~/.inputrc (create it if it doesn't exist), and add, for example:

Control-o: "U+16a1" 

but it's probably better to use, for example, Ctrl+Alt+Oinstead, because most ctrl and a single letter combinations are already bound:

"\e\C-o": "U+16a1"

Then restart terminal and use your key combinations to place the string at the cursor's position. There are many complex and more interesting possibilities; experiment and you'll find the macros very useful when you have to repeat various things a lot. List your currently assigned macros with bind -s.

List of examples for reference (don't use these as some will already be assigned): see bind -P:

"\e\C-k": "string"  # ctrl+alt+k
"\ey": "string" # alt+y
"\C-w": "string" # ctrl+w
"\C-x\C-e": "string" # ctrl+x, ctrl+e

See man 3 readline for more information and the Ubuntu manpages online.

  • Is there a list of possible symbolic key names? – jarno Jan 3 '20 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy