3

Neovim has a very handy terminal emulator.

In normal mode, I can use all the read-only vim commands on the output, like scrolling or going to the file under the cursor.

But more often than not, I want to delete some lines. Is it possible?

3
  • What does the documentation say?
    – romainl
    May 21 '16 at 6:19
  • I have read all the documentation. I know the buffer is unmodifiable, and I am interested in workarounds or tips that people used to overcome this limitation. May 22 '16 at 2:11
  • 1
    How about, since you can use read-only command, copy part of the output you want, and paste it in another buffer. I guess it doesn't make sense to try to delete output of the terminal inside the terminal mode. But it surely does to copy the output for later use.
    – nobe4
    May 23 '16 at 6:47
3

You can use this mapping to copy the content of the terminal buffer into a new buffer:

tnoremap <C-U> <C-\><C-N>:%y \| vertical new \| normal! P<CR>

<C-\><C-N> goes back to normal mode so you can use any normal mode command you want.

:%y copy the content of the buffer.

vertical new create a new vertical split.

normal! P paste the previously yanked text into the buffer.

0

I'm too lazy to be bothered to manually re-enter term mode and then paste the output of @nobe4's solution above. I combined this with another keybinding in normal mode to accomplish insert, kill the current command with ctrl-c, enter term mode, paste and then insert.

"term stuff
" let C-U open a new buffer to edit the current terminal line
tnoremap <C-U> <C-\><C-N>:y \| vertical new \| normal! P<CR> \| ^dw
nnoremap <C-U> ^D \|:q!<CR> \| i<C-c><C-\><C-n>pi
" let ESC take out out of term mode
tnoremap <ESC> <C-\><C-n>

so C-U will do both steps.

  1. ctrl-u in terminal mode will let you edit the current bash line in a new buffer.
  2. edit it
  3. use ctrl-u again and it will take the current line from the buffer and paste it back into your terminal for you.
2
  • 1
    Welcome to Vi and Vim! Just a note that you're overwriting a potentially useful command (:help CTRL-U). Also, why does the normal mode map have space-escaped-bar after ^D? Shouldn't that be ^D:q!<CR>i<C-c><C-\><C-n>pi---and then why i<C-c>? I think, for a terminal, that should be ^D:q!<CR>I<C-w>"". And the tnoremap should maybe be <C-\><C-n>:yank \| vertical new<CR>P^dw?
    – D. Ben Knoble
    Jun 5 at 0:11
  • Thanks for commenting. I do not use CTRL-U so I don't mind. I have extra characters from my terminal prompt that is deleting so that's why! Jun 6 at 3:04

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.