39

On Linux machines the current bash shell command can be edited in VI (or whatever the EDITOR is set to) by pressing Ctrl+XCtrl+E.

This doesn't seem to work on OS X 10.8. Any ideas on how to edit the current bash command in the default editor?

5
  • This should work in 10.8 (at least it does for me). Did you try to unset FCEDIT and EDITOR?
    – nohillside
    Apr 11, 2013 at 18:23
  • The documentation for C-xC-e is here -- what's in your VISUAL variable? Apr 11, 2013 at 21:29
  • it is not about 10.8. I am on 10.8 and it works fine with me. I did not change anything in my .bash_profile to enable this.
    – Vincent
    Apr 12, 2013 at 2:37
  • 1
    Note: This Apple.SE question is basically the same as the Super User question "I'm using Bash in 'vi mode'. What's the hotkey sequence to edit the current command line using an external editor?" Jan 8, 2015 at 21:07
  • 1
    I just realized that I was doing Ctrl+(x,e) (holding ctrl, pressing x, pressing e, releasing control) - this is why it didn't work for me. When I changed to pressing two clear strokes Ctrl-x, release Ctrl, Ctrl-e - then everything started to work perfectly!
    – Dimitry K
    Aug 21, 2018 at 11:34

3 Answers 3

42

You want set -o vi (add it to your ~/.bashrc). Then, to edit the current line, just hit Esc to enter command mode.

See http://www.gnu.org/software/bash/manual/bashref.html#Readline-vi-Mode

To edit the current command in a full-screen editor, set your VISUAL environment variable to vim (I assume you want vim), then in command mode in the current comment, hit v.


To show the mode in the prompt:

$ cat ~/.inputrc
set editing-mode vi
set show-mode-in-prompt on
set vi-ins-mode-string +
set vi-cmd-mode-string :
5
  • 7
    pressing the "v" was the secret sauce I was missing. thanks. Apr 27, 2013 at 0:32
  • 3
    Is it possible to get this v command while in emacs (non-vi) mode?
    – Alex Kahn
    Oct 28, 2015 at 14:15
  • 14
    Just do Ctrl-X then Ctrl-E.
    – albapa
    Jun 7, 2016 at 10:18
  • There is no visual indication that I have entered vim editing mode. Is there some setting to turn this on ?
    – gprasant
    Jan 31, 2020 at 23:02
  • Yes. See my edit. Feb 1, 2020 at 1:57
10

Your edit-and-execute-command may not be bound for vi mode. Try:

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

To make it permanent, add that line to your ~/.inputrc file.

(via this answer; credit to @unforgettableid for the link)

2

This worked for me:

If you’re using zsh (as you should), you could add the following to your .zshrc (or .zprofile):

export EDITOR=nvim
# Enable Ctrl-x-e to edit command line
autoload -U edit-command-line
# Emacs style
zle -N edit-command-line
bindkey '^xe' edit-command-line
bindkey '^x^e' edit-command-line
# Vi style:
# zle -N edit-command-line
# bindkey -M vicmd v edit-command-line

Then, reload your .zshrc.

Now all you need to do is hit Ctrl-x Ctrl-e (Ctrl-x e also works) or, if you use vi style, ESC v, and zsh will launch $VISUAL or $EDITOR, allowing you to edit the current command to your hearts content, and once you save & close your editor’s window, ZSH will execute your command.

https://nuclearsquid.com/writings/edit-long-commands/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .