Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libreadline edit-and-execute-command support

In bash(and others) I can use C-x C-d or other keybindings to edit command using external editor defined in $EDITOR or $VISUAL.

However, when I use libreadline in my program, it doesn't have similar functionality by default.

How to enable it in my program? Or maybe it is bash specific fearure, not libreadline? How to implement it then?

like image 230
Uladzislau_Khamkou Avatar asked Apr 15 '26 00:04

Uladzislau_Khamkou


1 Answers

That readline binding is probably "edit-and-execute-command" (C-x C-e for me, according to my man bash), and turns out it's basically an alias for the fc builtin command. I just took a look at bashline.c in Bash source code and found:

   1 /* bashline.c -- Bash's interface to the readline library. */
 ...
 440 /* Called once from parse.y if we are going to use readline. */
 441 void
 442 initialize_readline ()
 443 {
 ...
 482   rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command, -1);
 ...
 592   /* Bind C-xC-e to invoke emacs and run result as commands. */
 593   rl_bind_key_if_unbound_in_map (CTRL ('E'), emacs_edit_and_execute_command, emacs_ctlx_keymap);
 594 #if defined (VI_MODE)
 595   rl_bind_key_if_unbound_in_map ('v', vi_edit_and_execute_command, vi_movement_keymap);
 ...
 924 #define VI_EDIT_COMMAND         "fc -e \"${VISUAL:-${EDITOR:-vi}}\""
 925 #define EMACS_EDIT_COMMAND      "fc -e \"${VISUAL:-${EDITOR:-emacs}}\""
 926 #define POSIX_VI_EDIT_COMMAND   "fc -e vi"
 ...
1003 #if defined (VI_MODE)
1004 static int
1005 vi_edit_and_execute_command (count, c)
1006      int count, c;
1007 {
1008   if (posixly_correct)
1009     return (edit_and_execute_command (count, c, VI_EDITING_MODE, POSIX_VI_EDIT_COMMAND));
1010   else
1011     return (edit_and_execute_command (count, c, VI_EDITING_MODE, VI_EDIT_COMMAND));
1012 }
1013 #endif /* VI_MODE */
1014 
1015 static int
1016 emacs_edit_and_execute_command (count, c)
1017      int count, c;
1018 {
1019   return (edit_and_execute_command (count, c, EMACS_EDITING_MODE, EMACS_EDIT_COMMAND));
1020 }
....
like image 66
pynexj Avatar answered Apr 16 '26 14:04

pynexj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!