Custom command line editor in Linux

223 views Asked by At

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.

2

There are 2 answers

0
Benjamin W. On BEST ANSWER

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.

0
okapi On

Almost the same for zsh:

  export VISUAL=youreditor
  autoload -Uz edit-command-line
  zle -N edit-command-line
  bindkey '\e\C-m' edit-command-line