I would like to prepend clear; to all commands in terminal mode. This reduces the amount of 'clutter' in my terminal screen and makes things slightly clearer for me.

  • 1
    I'm not sure that affecting the shell like that is something you can do from Vim. May be better to ask at Unix & Linux SE how you would enable that in Bash (or whatever). Then from the Vim side you might be able to set an environment variable, inherited by terminal launched shells only, that enables whatever you figure out for the shell end.
    – B Layer
    Mar 1 at 18:51
  • It may help you to know that vim sets the VIM_TERMINAL environment variable on unix systems (:h terminal-unix) which you could exploit in your shell's initialisation files. Mar 1 at 19:17

In Bash and Zsh, a DEBUG trap can be used to chain a given set of commands or function to each issued command. Thus,

trap 'clear; "$@"' DEBUG

solves your problem.

Presumably you do not want to do it manually every single time, nor to affect every interactive shells, only those in Vim's terminal buffer. In that case, the solution is to add this to your .bashrc or .zshrc file:

[ -n "$VIM_TERMINAL" ] && trap 'clear; "$@"' DEBUG

The test verifies if the VIM_TERMINAL environment variable is set; if yes, it sets the trap.

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.