3

在shell中快速跳转到常用目录

 3 years ago
source link: https://www.lujun9972.win/blog/2017/04/10/%E5%9C%A8shell%E4%B8%AD%E5%BF%AB%E9%80%9F%E8%B7%B3%E8%BD%AC%E5%88%B0%E5%B8%B8%E7%94%A8%E7%9B%AE%E5%BD%95/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在shell中快速跳转到常用目录

Quickly navigate your filesystem from the command-line 中发现了一种将目录加为书签实现快速跳转的好方法.

其实原理很简单,就是将指向目标目录的软链接作为书签,统一存放在一个指定的位置,然后借助 cd -P 命令来跳转到软链接指向的实际目录.

export MARKPATH=$HOME/.marks
function jump { 
    cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function mark { 
    mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function unmark { 
    rm -i "$MARKPATH/$1"
}
function marks {
    ls -l "$MARKPATH" | sed 's/  / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' && echo
}

代码很简单,使用 mark 书签名 将当前目录加为书签; 使用 jump 书签名 跳转到书签指向的目录; 使用 marks 会列出所有的书签及其所指向的目录; 使用 unmark 书签名 删除掉指定的书签.

文章中还提供了一段在bash中按tab自动补全的代码,实在是太贴心了:

_completemarks() {
    local curw=${COMP_WORDS[COMP_CWORD]}
    local wordlist=$(find $MARKPATH -type l -printf "%f\n")
    COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
    return 0
}

complete -F _completemarks jump unmark

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK