如何(从脚本)向zsh命令历史记录添加内容? [英] How can I (from a script) add something to the zsh command history?

查看:203
本文介绍了如何(从脚本)向zsh命令历史记录添加内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够查看我的命令历史记录,并了解发出各种命令所依据的上下文-换句话说,我在哪个目录中?"我可以通过多种方法来实现此目的,但是所有这些(我能想到的)都需要操纵zsh历史记录,以添加(例如)带有$(pwd)结果的注释行. (我可以创建名为cd& pushd& popd等的函数,或者我可以使用zsh的preexec()函数以及也许其perceptor()函数最多每X秒添加一条注释行,就在我发出命令之前,或者也许还有其他方法.)

I'd like to be able to look through my command history and know the context from which I issued various commands--in other words, "what directory was I in?" There are various ways I could achieve this, but all of them (that I can think of) would require manipulating the zsh history to add (for instance) a commented line with the result of $(pwd). (I could create functions named cd & pushd & popd etc, or I could use zsh's preexec() function and maybe its periodic() function to add the comment line at most every X seconds, just before I issue a command, or perhaps there's some other way.)

问题是,我不想直接操纵历史记录文件并绕过Shell的历史记录机制,但是我想不通一种方法(例如,使用fc命令)在不添加历史记录的情况下添加一些内容实际上在命令行上键入它.我该怎么办?

The problem is, I don't want to directly manipulate the history file and bypass the shell's history mechanism, but I can't figure out a way (with the fc command, for instance) to add something to the history without actually typing it on the command line. How could I do this?

推荐答案

您可以使用print -s命令(请参阅man zshbuiltins)将所需的任何内容添加到历史记录中.您还可以创建一个名为zshaddhistory的挂钩函数(请参见man zshmisc),该函数可以在创建历史内容时对其进行操作.

You can use the print -s command (see man zshbuiltins) to add anything you want to the history. There's also a hook function you can create called zshaddhistory (see man zshmisc) that can manipulate history contents as they are created.

请参见我的Bash历史记录功能以获取启发.

这篇关于如何(从脚本)向zsh命令历史记录添加内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆