I have a terminal buffer with 10000 lines of previously executed output. In order to search for the particular sequence of words I have to go through all 10000 lines before getting to my last executed command's output. Which makes me so annoyed. Is the a way of clearing a terminal buffer without quiting the terminal and running it again (because I use ssh to remote server and it's painfull to execute all the commands all over again to establish the connection to it). (settings scroll back to 0 or whatever won't do a trick)
4 Answers
AFAIK, there's no "good" way to do it, but there is a "hacky" way. Suppose you want <c-w><c-l>
combination to clear vim internal buffer in both normal and terminal(insert) modes. The mappings in vimrc would look like this:
nmap <c-w><c-l> :set scrollback=1 \| sleep 100m \| set scrollback=10000<cr>
tmap <c-w><c-l> <c-\><c-n><c-w><c-l>i<c-l>
This will "clear" the buffer, and return you to terminal "insert" mode if necessary. Extra <c-l>
at the end is to also clear the shell screen, feel free to remove if you wan't need that. Note the sleep, which is required for it to work. Maybe it's possible to use a lower value than 100m, I'm not sure.
Both clear
and Ctrl+l
work for me using Neovim and bash.
Would have asked you to try it in a comment before posting as an answer but I don't have enough rep.
-
2Unfortunatly for some reason it doesn't work for previously long executed commands in neovim terminal. I don't know why– NikitQaCommented Sep 24, 2019 at 9:54
-
1I can also confirm this doesn't work. It doesn't clear the buffer and the leftover content can still be accessed in normal mode. Commented Jan 5, 2022 at 9:03
function! ClearTerminal()
set scrollback=1
let &g:scrollback=1
echo &scrollback
call feedkeys("\i")
call feedkeys("clear\<CR>")
call feedkeys("\<C-\>\<C-n>")
call feedkeys("\i")
sleep 100m
let &scrollback=s:scroll_value
endfunction
For a neovim/nvim/neo-vim this function and mappings can help:
config.lua
vim.api.nvim_set_keymap('t', '<C-l><C-l>', [[<C-\><C-N>:lua ClearTerm(0)<CR>]], mapping_opts)
vim.api.nvim_set_keymap('t', '<C-l><C-l><C-l>', [[<C-\><C-N>:lua ClearTerm(1)<CR>]], mapping_opts)
function ClearTerm(reset)
vim.opt_local.scrollback = 1
vim.api.nvim_command("startinsert")
if reset == 1 then
vim.api.nvim_feedkeys("reset", 't', false)
else
vim.api.nvim_feedkeys("clear", 't', false)
end
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<cr>', true, false, true), 't', true)
vim.opt_local.scrollback = 10000
end