bash实现当前路径下子目录跳转

说明

很早之前尝试过autojump项目,当时使用bash函数实现jc功能,最低测试过Ubuntu16.04
日常中高频使用,今天添加了bash补全,分享给诸位

配置

function _j_completion() {
    local cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=($(compgen -W "$(echo -n $PWD | sed 's/\// /g')" -- $cur))
}
function j() {
	local path=${PWD%$1*}
	local dir=${PWD:${#path}}
	cd ${path}${dir%%/*}
}
complete -o nospace -F _j_completion j

效果

补全

 (master) ~/workspace/git/dotfiles/git ✘ j
dotfiles   git        home       rothwell   workspace

输出

 (master) ~/workspace/git/dotfiles/git ✘ j xx
 (master) ~/workspace/git/dotfiles/git ✘ j it
 (master) ~/workspace/git/dotfiles/git ✘ j it/
 ~/workspace/git ✘ j home
 /home ✘ cd -
/home/rothwell/workspace/git
 ~/workspace/git ✘
posted @ 2024-06-06 11:21  rdwr  阅读(6)  评论(0编辑  收藏  举报