stu*_*ent 5 command-history zsh options
假设我不久前做过cd /path/to/foo/bar
,然后evince file.pdf
。现在,如果我想file.pdf
再次打开,我还必须再次执行这两个步骤(使用历史记录)。但是我会一步完成。即我希望not evince file.pdf
写入.zsh_history
but evince /path/to/foo/bar/file.pdf
。例如,我如何通过修改我的 来实现这一点.zshrc
?evince
这只是一个例子。它应该适用于任何命令。
我应该对这种新行为有什么缺点吗?
这不是可以对任意 shell 代码执行的操作,因为 zsh 无法知道代码中的哪些单词实际上是命令将视为文件路径的参数,更不用说作为相对于当前工作目录的路径了。代码存储到历史记录中的时间。
对于最简单的 shell 代码,例如cmd with its literal arguments
和 对于有限的预定义命令集,您可以执行以下操作:
commands_with_expanded_paths=(evince okular)
zshaddhistory() {
local words=( ${(z)1%$'\n'} )
if (( $commands_with_expanded_paths[(Ie)$words[1]] )); then
local replaced_words=($words[1]) word
for word in $words[2,-1]; do
local decoded_word=${(Q)word}
if [[ $decoded_word = [^/]* && -e $decoded_word ]]; then
word=${(q+)decoded_word:P}
fi
replaced_words+=($word)
done
print -rs -- $replaced_words
fc -p
fi
}
Run Code Online (Sandbox Code Playgroud)
如果第一个单词是给定列表中命令的名称,则如果发现其余参数是保存到历史记录中的文本中现有文件的相对路径,则将其替换为绝对路径。
归档时间: |
|
查看次数: |
143 次 |
最近记录: |