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
function! SetFileName()
execute 'file ' . input('Enter name: ')
endfunction
" - 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>
function! EditCommandline()
" 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
new
" make buffer a scratch buffer
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal noswapfile
" put command into buffer
put! =s:command
" remove extra lines
%join!
" copy buffer to register when it is closed
autocmd BufLeave <buffer> :%yank c
endfunction
q:
in normal mode do what you want? (See:help cmdline-window
)