9

Vim's new :terminal feature is pretty neat, but when I run something like a rails server, I want to be able to clear logging output every now and then.

The usual methods in a non-vim term window don't seem to work, like <D-k> (Cmd-k clears scrollback in Terminal.app on macOS), etc.

I've tried <C-w>:Clear<CR>, but it seems to be for something different as well.

If I weren't running a rails server, I could just type clear in the terminal.

I glanced through :help :term, and I'm not seeing what I want jump out at me.

8
  • Isnt <C-l> the standard clear key?
    – D. Ben Knoble
    Commented Nov 16, 2018 at 20:43
  • C-l is redraw. C-l actually does look like it clears the term window in Vim, but inconveniently it does not work when you have a process like rails server running. It has to be implemented by vim itself, because obviously if I use the terminal's actual clear key, it will wipe the whole screen including vim (which of course comes back if I hit C-l to redraw). Perhaps this is just an edge case the new feature just hasn't addressed yet. Because it should be simple. You also can't <C-\><C-n> and delete the text, because modifiable is off and cannot be turned on for a running process.
    – Wumbo
    Commented Nov 16, 2018 at 23:15
  • Bizarre... if i do :term and then press <C-l> in my spawned shell it clears the term screen
    – D. Ben Knoble
    Commented Nov 16, 2018 at 23:24
  • Yes, but try that after starting a process that needs C-c to end. Or just do sleep 10000 and try to C-l before it is done. This is actually the case for a non-vim terminal as well.
    – Wumbo
    Commented Nov 16, 2018 at 23:27
  • Well sure, the term never gets the clear. Hmm. I see the issue now
    – D. Ben Knoble
    Commented Nov 16, 2018 at 23:29

4 Answers 4

1

Use https://github.com/kassio/neoterm.

It provides A better terminal support.

You can use the command :<terminal number>Tclear to clear the terminal. :Tclear for the last opened/active terminal (not sure which).

1

I don't know how to clear the previous texts but if you have problem with scrolling back to the last command after executing it you can go to text mood by pressing ctrl + W N and set mark by pressing m <any key on the keyboard> press i to comeback to interactive mode and and execute your command and when you finished again go back to text mode by pressing ctrl + W N and go to the previously marked place by pressing ' <that key you have pressed before>.

0

Going through Vim's terminal.txt, I don't think you have a chance to clear the window.

It's because Vim doesn't do anything about <c-l>, just passing it as well as other typed keys to the running job, like bash or rails server. (when your focus is in the window)

The <c-w>:clear doesn't do the trick, either. The command is abbreviation of :clearjumps for newer Vim versions.

For normal terminal, like gnome-terminal or mac's terminal, it's the terminal dealing with your <c-l> or <cmd-l>, <cmd-k>. Vim's terminal looks not doing it.

However, I happened to try out a workaround. You can try it for your environment:

:tmap <c-l> <c-w>s<c-w>c

  • tmap is mapping specific for terminal job mode.
  • <c-w>s creates a new window, and when <c-w>c closes it and gets back, the terminal is cleared.
6
  • Sorry, but c-w s c-w c also does not work. You can try with sleep 10000. Text for processes in progress is not lost. There's probably situations where that is desirable. I agree, there are commands that vim is interpreting, and commands that the terminal app is interpreting. :clear and :Clear are different. vim terminal has some idea about breakpoints. I'm not entirely sure what it is about.
    – Wumbo
    Commented Nov 17, 2018 at 13:53
  • Let's check the version: I tested it with Vim 2.8.0 on ubuntu 18.04. However, I don't have gui on this server. I 'ssh' to the server and launch 'screen' to work with. Both sides are gnome-terminal. I don't understand why you test with "sleep 10000". It doesn't have anything on screen for your to CLEAR. (If you run it by ":term sleep 10000".) I test with ":term tail -f /var/log/syslog", which spit out log info from time to time. It works very well. Commented Nov 17, 2018 at 16:47
  • sleep is just an example of a long running process for someone who isn't using rails server or similar. Start :term without an argument. type echo 'test'. type sleep 10000. tail gives output instantly, so it's not a good example. You don't have to remap <C-l> for tail, it would work already because it is finished quickly.
    – Wumbo
    Commented Nov 17, 2018 at 22:15
  • There is some misunderstanding. Tail with option '-f' (follow) will track a growing file and keep printing to the screen. It's a good way to simulate the so-called 'log' behavior of a server. You can either test with it directly via :term tail -f GROWING_FILE or run it in the bash called :term. Commented Nov 17, 2018 at 23:22
  • In addition, tail -f will never come back until you press <ctrl-c>. It's better than sleep 10000 because it has output from time to time. Commented Nov 17, 2018 at 23:34
0

put this in ~/.zshrc:

# I export nvimServer_4_TermAPI, a env-var defined by myself, in some other place...
cle(){
    clear
    nvim --server "$nvimServer_4_TermAPI" \
         --remote-expr   "Tapi_clear(0)"
}

put this in init.vim:

    func! Tapi_clear(buf) abort
        try
            let ori_scrollback = getbufvar('', '&scrollback')
            setl scrollback=1
            sleep 100m
            exe 'setl scrollback=' . ori_scrollback
        catch
            echom 'Tapi_clear有错'
        endtry
        return ''
    endf

Inspired by this in autoload/neoterm.vim of https://github.com/kassio/neoterm:

        if get(a:opts, 'force_clear', 0)
            let l:bufname = bufname(l:instance.buffer_id)
            let l:scrollback = getbufvar(l:bufname, '&scrollback')

            call setbufvar(l:bufname, '&scrollback', 1)
            sleep 100m
            call setbufvar(l:bufname, '&scrollback', l:scrollback)
        end

About shell command clear

https://unix.stackexchange.com/questions/410708/the-easiest-way-to-clear-scrollback-buffer-of-terminal-some-deeper-explanation/741099#741099

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.