是否有可能具有目录感知的 bash 历史记录

iSc*_*E4m 9 linux bash

我不是唯一一个在某些目录中使用某些命令而在其他目录中使用某些命令的人。当我进入一个特定目录并从中运行带有多个参数的脚本时,我希望可以轻松获得它的历史记录。取而代之的是,历史被来自不同选项卡和窗口的成百上千的其他命令填满了垃圾邮件,并且很难达到我想要重新运行的东西。

想象一下,您有一个服务器,用于非常特定的脚本,该脚本启动时带有大量参数-x this -y that/thing/there.conf -d -g -t also -t more- 每当您通过 ssh 连接到该服务器时,要使用该脚本,您只需按几次,它就在那里。

那么,有没有办法在一个系统上,在一个用户下执行此操作?

cur*_*arn 5

有一个 shell 历史扩展,允许您搜索历史记录并首先根据当前目录、git 存储库、退出状态和主机显示相关结果。

您可以在此处安装该项目: https: //github.com/curusarn/resh
使用CTRL+启动它R(替换反向搜索)。
搜索应用程序如下所示:

搜索应用程序的屏幕截图

免责声明:我是该项目的创建者,这是无耻的自我推销


ter*_*don 4

首先,将UpDown按钮分别映射到history-search-backward和可能会更简单history-search-forward。从man bash

   history-search-forward
          Search  forward through the history for the string of characters
          between the start of the current line and the point.  This is  a
          non-incremental search.
   history-search-backward
          Search backward through the history for the string of characters
          between the start of the current line and the point.  This is  a
          non-incremental search.
Run Code Online (Sandbox Code Playgroud)

启用此功能后,如果开始输入命令名称,然后点击Up,则只会显示历史记录中以您输入的内容开头的命令。这样,您可以非常快速地找到您感兴趣的命令,而无需摆弄特定于目录的历史文件。只需输入s,然后就只会找到Up以 开头的命令。s使用foobaand 只会fooba显示以 开头的内容。

要启用此功能,请将以下行添加到~/.inputrc服务器上的文件中(根据您的终端模拟器,您可能需要稍微不同的格式。如果这不起作用,请查看我的答案)

"\e[A": history-search-backward
"\e[B": history-search-forward
Run Code Online (Sandbox Code Playgroud)

也就是说,是的,可以为每个目录设置一个历史文件。将此函数添加到您的~/.profile(不是您的,因为在用于登录远程计算机~/.bashrc时默认情况下不会读取此文件):ssh

   history-search-forward
          Search  forward through the history for the string of characters
          between the start of the current line and the point.  This is  a
          non-incremental search.
   history-search-backward
          Search backward through the history for the string of characters
          between the start of the current line and the point.  This is  a
          non-incremental search.
Run Code Online (Sandbox Code Playgroud)

然后将PROMPT_COMMAND变量设置为它(这是每次显示 shell 提示符时执行的命令):

export PROMPT_COMMAND='setHistFile'
Run Code Online (Sandbox Code Playgroud)

将数组更改targetDirs为您想要拥有自己的历史文件的目录列表。