Bash – Execute ‘Clear’ Command Before Any Other Command

bash

I see the question at Can I configure bash to execute "clear" before every command typed in the console?, but the answers there are not valid for GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin14). The problem with the answers given there is that the output of the command gets cleared before it can be read. That is a pretty popular bash version as it is released on mac os X. I cannot comment on the best answer there due to lack of rep points so my only option is to ask a new question.

The reason for wanting this functionality is that many commands i use (such as 'git status') have multi-line output. When multi-line commands are run back-to-back, it is not very easy to automatically separate the blobs of text on the screen.

Does anyone know how to accomplish this functionality on the version of bash specified?

Best Answer

Here's a way. It assumes you have the default key bindings.

bind '"\C-m": "\C-l\C-j"'

The \C-m intercepts the 'Enter' key, the \C-l (Ctrl+L) executes the Bash clear-screen function, and the \C-j executes the Bash newline-and-indent function; so the command is binding Enter key to Ctrl+L & Ctrl+J