Pergunta

Code from GNU readline and key bindings :

#include <stdio.h>

#include <readline/readline.h>

int my_cool_readline_func (int count, int key) {
   printf ("key pressed: %d\n", key);
   rl_on_new_line ();
   return 0;
}

int main(void) {
     rl_command_func_t my_cool_readline_func;
     rl_bind_key ('\t', my_cool_readline_func);
     rl_bind_key (27, my_cool_readline_func); /* ascii code for ESC */
     rl_bind_keyseq ("\\C-a", my_cool_readline_func);

     while (1) {
         char *line = readline ("rl> ");
     }
}

Is it possible to use this kind of custom readline functions in bash commandline?

For example, I have tried these (a custom function can be much more complex, of course):

bind '"\C-t" beginning-of-line shell-kill-word'

bind '"\C-t" beginning-of-line, shell-kill-word'

bind '"\C-t" beginning-of-line; shell-kill-word'

None of them worked.

Foi útil?

Solução

I think this binding is the key to understanding how this is done:

 "\C-x\"": "\"\"\C-b"

Read "When I press Ctrl+X and then ", insert two quotes (\"\") and move the cursor once to the left (\C-b) so it ends up between the two double quotes.

On the right hand side of the :, you can only have a single readline function or a "macro" or a single shell function. A macro is a sequence of keystrokes.

So what you need to do is assign the functions you want to keys and then use the keys in the macro:

 bind '"\C-t":"\C-a\ed"'

See also: Complex keybinding in bash

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top