linuxbashcommand-line-interfacezshansi-term

Custom command line editor in Linux


I would like to invoke my own command line editor in bash or zsh when Alt+Enter is being pressed. It should do some editing and submit result to the shell on Enter. So basically my editor takes current command line content and returns a modified one. Any ideas how to approach integration? I do know how to work with ANSI terminals, just wondering how to integrate my editor console app to the shell in this way.


Solution

  • For Bash:

    There is a Readline command that opens the current command in an editor, edit-and-execute-command. By default, it is bound to C-x C-e, and it opens the command in what $VISUAL is set to, or $EDITOR, or with Emacs.

    You can set $VISUAL to your editor by exporting it into the environment, for example in ~/.bashrc:

    export VISUAL=youreditor
    

    and bind it to Alt+Enter with

    bind '"\e\C-m": edit-and-execute-command'
    

    on the command line, or

    "\e\C-m": edit-and-execute-command
    

    in ~/.inputrc.