43

I have recently discovered that if we press Ctrl+X Ctrl+E, bash opens the current command in an editor (set in $VISUAL or $EDITOR) and executes it when the editor is closed.

But it doesn't seem to be documented in the man pages. Is it documented, and if so where?

NotTheDr01ds
  • 3,547
Kartik
  • 2,004

2 Answers2

52

I have found it out now. I should have read it more carefully before asking this.

The man page says:

edit-and-execute-command (C-xC-e)
          Invoke  an  editor  on the current command line, and execute the
          result as shell commands.   Bash  attempts  to  invoke  $VISUAL,
          $EDITOR, and emacs as the editor, in that order.
Kartik
  • 2,004
2

As discussed in the comments under the accepted answer, it is very convenient/safe to have an editor feature without immediate execution.

Add this to .bashrc to make Ctrl xe binding disable execution.

_edit_wo_executing() {
    local editor="${EDITOR:-nano}"
    tmpf="$(mktemp).sh"
    printf '%s\n' "$READLINE_LINE" > "$tmpf"
    $editor "$tmpf"
    READLINE_LINE="$(<"$tmpf")"
    READLINE_POINT="${#READLINE_LINE}"
    rm "$tmpf"
}

bind -x '"\C-x\C-e":_edit_wo_executing'

Credits: https://superuser.com/a/1601690/266871