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.

Is this possible?

  • Does typing q: in normal mode do what you want? (See :help cmdline-window) – EvergreenTree Dec 18 '15 at 13:17
  • @EvergreenTree ah, I should have made the distinction between the vim commandline and the commandline of the shell running in the terminal buffer more clear. – Brett Y Dec 20 '15 at 19:11

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: ')

" - 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

  " make buffer a scratch buffer
  setlocal buftype=nofile
  setlocal bufhidden=hide
  setlocal noswapfile

  " put command into buffer
  put! =s:command

  " 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.

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.