Change zsh preexec command

Is there a way to change the command that is about to execute? I would like to redirect the output to a file, as well as print it to the terminal. I found that ls > file.txt | cat ls > file.txt | cat is doing the job, so I would like to add that > file.txt | cat > file.txt | cat to any command that is about to be executed.
Is there a better way to redirect a file and print to a terminal? I'm trying to make a registrar.

+6
source share
2 answers

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.

+5
source

There are several ways to do this: I like this block, which I found here http://git.grml.org/?p=grml-etc-core.git;a=blob_plain;f=etc/zsh/zshrc; hb = HEAD

 abk=( '...' '../..' '....' '../../..' 'BG' '& exit' 'C' '| wc -l' 'G' '|& grep '${grep_options:+"${grep_options[*]}"} 'H' '| head' 'Hl' ' --help |& less -r' #d (Display help in pager) 'L' '| less' 'LL' '|& less -r' 'M' '| most' 'N' '&>/dev/null' #d (No Output) 'R' '| tr Az N-za-m' #d (ROT13) 'SL' '| sort | less' 'S' '| sort -u' 'T' '| tail' 'V' '|& vim -' 'co' './configure && make && sudo make install' 'fc' '> file.txt | cat' ) zleiab() { emulate -L zsh setopt extendedglob local MATCH if (( NOABBREVIATION > 0 )) ; then LBUFFER="${LBUFFER},." return 0 fi matched_chars='[.-|_a-zA-Z0-9]#' LBUFFER=${LBUFFER%%(#m)[.-|_a-zA-Z0-9]#} LBUFFER+=${abk[$MATCH]:-$MATCH} } zle -N zleiab && bindkey ",." zleiab 

Also note that I added 'fc' '> file.txt | cat' 'fc' '> file.txt | cat' to abk list

What this does is that you type fc after the command and then quickly press,. (comma and period), and zsh replaces fc with > file.txt | cat > file.txt | cat

0
source

Source: https://habr.com/ru/post/926196/


All Articles