In bash one can edit the current commandline in $EDITOR by pressing <c-x><c-e>.
I am running bash inside a terminal buffer within Neovim. I would like to be able to press <c-x><c-e> and have Neovim open a new buffer (:new), in the same Neovim instance, with the current contents of the commandline. When I close that buffer (:bdelete) I would like to have the contents of the buffer replace what was on the commandline.
I've now turned the script below into a plugin: nvim-editcommand, or if you don't mind starting you terminals with a plugin command nvim-terminus provides a few more features
Finally got it working in pure (ugly) vimscript:
" set the name of the current buffer, useful for setting the name of terminal buffers
execute 'file ' . input('Enter name: ')
" - yank from last line with prompt ('> ') to last line into register c
" - clear commandline
" - call function
tnoremap <c-x> <c-\><c-n>:execute ':?> ?,$y c'<cr>A<c-c><c-\><c-n>:call EditCommandline()<cr>
" clear search highlighting
let @/ = ""
" - set an autocmd on the current (terminal) buffer that will run when the buffer is next entered
" - put from register c (where the new command will be)
" - remove the autocmd
" - go to insert mode
autocmd BufEnter <buffer> put c | autocmd! BufEnter <buffer> | call feedkeys('A')
" get all text after prompt '> '
let s:command = strpart(@c, strridx(@c, "> ") + 2)
" open new empty buffer
" make buffer a scratch buffer
" put command into buffer
" remove extra lines
" copy buffer to register when it is closed
autocmd BufLeave <buffer> :%yank c
It's not exactly what you asked, but it might be closer to what you really want:
In bash, you can type in vi mode! Just put type set -o vi into bash and you'll be able to hit escape to switch to normal mode. Stick it in your .bashrc file for it to become an incredibly useful permanent change.
edit: I just realized that this might be why you can't (by default) exit Neovim terminal mode with escape.
q:in normal mode do what you want? (See