You can change the action that is performed when a line is executed to change the command to be executed. This can be done by defining a function that you then associate with the input key.
Lets you first define a function that can add '> file.txt | cat 'ends with any command:
function log_and_accept { BUFFER="$BUFFER > file.txt | cat" zle accept-line }
The next part is to actually change the default input key behavior to your new function. The default behavior that we are replacing is the accept-line function, and if we look at the zle documentation , you will see that accept-line is bound to ^ J and ^ M.
To associate this function with the letters that you first need to turn into a widget:
zle -N log_and_accept_widget log_and_accept
You can then bind it by replacing the previous behavior:
bindkey '^J' log_and_accept_widget bindkey '^M' log_and_accept_widget
You will now expand this team for every team you make. Every cd, ls, vim etc. Therefore, I recommend that you define a few more functions that really turn this on and off:
function turn_on_logging { bindkey '^J' log_and_accept_widget bindkey '^M' log_and_accept_widget } function turn_off_logging { bindkey '^J' accept-line bindkey '^M' accept-line } zle -N turn_on_logging_widget turn_on_logging zle -N turn_off_logging_widget turn_off_logging bindkey '^P' turn_on_logging_widget bindkey '^O' turn_off_logging_widget
I think you should be careful with this. After a little testing, I quickly began to dislike.
source share