How can I (from a script) add something to the zsh command history?

I would like to be able to view my command history and know the context from which I issued the various commands - in other words, "in which directory have I been?" There are several ways to achieve this, but all of them (which I can think of) will require manipulating the zsh history to add (for example) a commented line with the result of $ (pwd). (I could create functions called cd, pushd and popd, etc., Or I could use the zsh preexec () function and possibly its period () function to add a comment line no more than every X seconds, just before I issue a command, or maybe there in some other way.)

The problem is that I do not want to directly manipulate the history file and bypass the shell history mechanism, but I cannot figure out a way (for example, using the fc command) to add something to the history without actually typing it on the command line. How can i do this?

+3
source share
1 answer

You can use the command print -s(see man zshbuiltins) to add everything you want to the story. You can also create a hook function called zshaddhistory(see man zshmisc), which can manipulate the contents of the story as they are created.

See my bash history logging features for inspiration.

+3
source

All Articles