保存当前目录来砸历史 [英] Saving current directory to bash history

查看:103
本文介绍了保存当前目录来砸历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拯救那里的每个命令一起在历史上发出的命令将当前目录。为了不误事,我在想将当前目录在该行的最后一个注释。一个例子可以帮助:

I'd like to save the current directory where the each command was issued alongside the command in the history. In order not to mess things up, I was thinking about adding the current directory as a comment at the end of the line. An example might help:

$ cd /usr/local/wherever
$ grep timmy accounts.txt

我想庆典保存最后的命令:

I'd like bash to save the last command as:

grep timmy accounts.txt # /usr/local/wherever

这个想法是这样,我可以立即看到我发出命令。

The idea is that this way I could immediately see where I issued the command.

推荐答案

下面是一个班轮版本。这是原来的。我还贴出一个简短功能版本和长版本的功能与一些附加功能。我喜欢的功能版本,因为他们会不会破坏环境中的其他变量,他们是一个比班轮更具可读性。这篇文章对他们怎么可能不会在别人重复所有的工作的一些信息。

One-liner version

Here is a one-liner version. It's the original. I've also posted a short function version and a long function version with several added features. I like the function versions because they won't clobber other variables in your environment and they're much more readable than the one-liner. This post has some information on how they all work which may not be duplicated in the others.

export PROMPT_COMMAND='hpwd=$(history 1); hpwd="${hpwd# *[0-9]*  }"; if [[ ${hpwd%% *} == "cd" ]]; then cwd=$OLDPWD; else cwd=$PWD; fi; hpwd="${hpwd% ### *} ### $cwd"; history -s "$hpwd"'

这使得看起来像一个历史条目:

This makes a history entry that looks like:

rm subdir/file ### /some/dir

我用 ### 作为注释分隔符来除了评论认为剥离老路评论认为会在用户可以键入并减少冲突的机会将其设置否则积累,如果你preSS空白命令行中输入。不幸的是,侧面影响是,像回声###命令被错位,虽然这应该是相当罕见的。

I use ### as a comment delimiter to set it apart from comments that the user might type and to reduce the chance of collisions when stripping old path comments that would otherwise accumulate if you press enter on a blank command line. Unfortunately, the side affect is that a command like echo " ### " gets mangled, although that should be fairly rare.

有些人会发现我重复使用相同的变量名是不愉快的事实。通常我不会,但在这里我试图尽量减少碳足迹。它很容易在任何情况下更改。

Some people will find the fact that I reuse the same variable name to be unpleasant. Ordinarily I wouldn't, but here I'm trying to minimize the footprint. It's easily changed in any case.

据盲目地假定您不使用 HISTTIMEFORMAT 或修改其他方式的历史。它会很容易到日期命令代替 HISTTIMEFORMAT 功能添加到评论。但是,如果你需要使用它的一些原因,它仍然工作在子shell,因为它会自动获取取消设置:

It blindly assumes that you aren't using HISTTIMEFORMAT or modifying the history in some other way. It would be easy to add a date command to the comment in lieu of the HISTTIMEFORMAT feature. However, if you need to use it for some reason, it still works in a subshell since it gets unset automatically:

$ htf="%Y-%m-%d %R "    # save it for re-use
$ (HISTTIMEFORMAT=$htf; history 20)|grep 11:25

有几个非常小的问题了。其中一个是,如果你使用历史命令是这样,例如:

There are a couple of very small problems with it. One is if you use the history command like this, for example:

$ history 3
echo "hello world" ### /home/dennis
ls -l /tmp/file ### /home/dennis
history 3

结果不会显示在历史命令本身的评论,尽管你会看到它,如果你preSS向上箭头或发出另一个历史命令。

The result will not show the comment on the history command itself, even though you'll see it if you press up-arrow or issue another history command.

另一个是与嵌入的换行符命令留在历史上的一个未注释的副本,除了评论副本。

The other is that commands with embedded newlines leave an uncommented copy in the history in addition to the commented copy.

有可能是出现其它问题。让你找到任何我知道。

There may be other problems that show up. Let me know if you find any.

猛砸执行包含在 PROMPT_COMMAND 变量每次 PS1 主提示符发出的命令。这个小脚本利用了这种优势在历史抢到最后一个命令,给它添加注释,保存了回去。

Bash executes a command contained in the PROMPT_COMMAND variable each time the PS1 primary prompt is issued. This little script takes advantage of that to grab the last command in the history, add a comment to it and save it back.

这是分道扬镳与评论:

hpwd=$(history 1)              # grab the most recent command
hpwd="${hpwd# *[0-9]*  }"      # strip off the history line number
if [[ ${hpwd%% *} == "cd" ]]   # if it's a cd command, we want the old directory
then                           #   so the comment matches other commands "where *were* you when this was done?"
    cwd=$OLDPWD
else
    cwd=$PWD
fi
hpwd="${hpwd% ### *} ### $cwd" # strip off the old ### comment if there was one so they 
                               #   don't accumulate, then build the comment
history -s "$hpwd"             # replace the most recent command with itself plus the comment

这篇关于保存当前目录来砸历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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