idb*_*rii 34 cd bash history file-search
是否有类似 bash 的反向搜索历史 ( Ctrl- R) 之类的东西,但仅适用于目录?
我有一些我想跳转到的深层文件夹层次结构,所以我想使用类似反向搜索历史的东西,但它只查找文件夹名称并给我绝对路径。
从本质上讲,它会给出与使用类似的结果,!?
但只匹配cd
前面的命令,您可以单步执行结果和完整路径。
到目前为止,我找到的最好的解决方案是bashmarks。
有
cd -
Run Code Online (Sandbox Code Playgroud)
那是“cd[space][hyphen]”命令,它转到您之前所在的目录,本质上是“深度1的历史记录”。重复“cd -”在两个目录之间来回切换。
引用手册页:
应支持以下操作数:[...]
当 [连字符] 用作操作数时,这应等效于以下命令:
Run Code Online (Sandbox Code Playgroud)cd "$OLDPWD" && pwd
不幸的是,我不知道真正的内置目录历史记录。
cd
您可以使用pushd
、popd
、dirs
内置命令构建自己的命令。
cd --
(列出当前历史记录)
cd -num
(转到 num 目录)
cd -
(转到上一个目录)
function cd ()
{
local hnum=16;
local new_dir index dir cnt;
if ! [ $# -eq 0 ]; then
if [[ $# -eq 2 && $1 = "--" ]]; then
shift;
else
if ! {
[ $# -eq 1 ] && [[ $1 =~ ^(-[0-9]{,2}|-|--|[^-].*)$ ]]
}; then
builtin cd "$@";
return;
fi;
fi;
fi;
[ "$1" = "--" ] && {
dirs -v;
return
};
new_dir=${1:-$HOME};
if [[ "$new_dir" =~ ^-[0-9]{,2}$ ]]; then
index=${new_dir:1};
if [ -z "$index" ]; then
new_dir=$OLDPWD;
else
new_dir=$(dirs -l +$index) || return;
fi;
fi;
pushd -- "$new_dir" > /dev/null || return;
popd -n +$hnum &> /dev/null || true;
new_dir=$PWD cnt=1;
while dir=$(dirs -l +$cnt 2> /dev/null); do
if [ "$dir" = "$new_dir" ]; then
popd -n +$cnt > /dev/null;
continue;
fi;
let cnt++;
done
}
Run Code Online (Sandbox Code Playgroud)
小智 5
bash 有 pushd/popd/dirs。我在我的 .bashrc 中有这个来自动将目录推送到 bash 的堆栈上。
#let cd also pushd directories into stack. Use popd to reverse stack
function cd ()
{
if [ -e $1 ]; then
pushd $1 &> /dev/null #dont display current stack
fi
}
Run Code Online (Sandbox Code Playgroud)
使用弹出这些popd
并使用显示堆栈dirs
归档时间: |
|
查看次数: |
16005 次 |
最近记录: |