29

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 doesen't seem to be documented in the man pages.

2
  • 1
    Search for edit-and-execute-command in the man page. – n. 1.8e9-where's-my-share m. Aug 3 '13 at 7:41
  • 4
    If you use Bash's vi mode, the short cut is Esc, V. I trigger this all the time by accident. Finally learned what it means today :) – Mark E. Haase Sep 23 '14 at 14:43
39

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.
4
  • 12
    Be very careful with this feature. If you cancel the edit, the original command line will be immediately executed. So if you are editing rm -rf / and invoke the editor and realize you are into something dangerous and thus cancel the edit, your rootfs will be deleted without further questions asked. – marlar Dec 26 '19 at 22:44
  • 8
    @marlar if your editor is vim, you can use :cq, it makes it exit with non-zero code, and prevents the command from execution – karlicoss Jan 6 at 5:39
  • 1
  • @karlicoss Sure, :cq etc works if you are aware of it and remember it. But if you don't, shit can happen. – marlar Jun 1 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.