[英]Directory bookmarking for bash
是否有任何用於 bash 的目錄書簽實用程序以允許在命令行上更快地移動?
更新
感謝大家的反饋,但是我創建了自己的簡單 shell 腳本(隨意修改/擴展它)
function cdb() {
USAGE="Usage: cdb [-c|-g|-d|-l] [bookmark]" ;
if [ ! -e ~/.cd_bookmarks ] ; then
mkdir ~/.cd_bookmarks
fi
case $1 in
# create bookmark
-c) shift
if [ ! -f ~/.cd_bookmarks/$1 ] ; then
echo "cd `pwd`" > ~/.cd_bookmarks/"$1" ;
else
echo "Try again! Looks like there is already a bookmark '$1'"
fi
;;
# goto bookmark
-g) shift
if [ -f ~/.cd_bookmarks/$1 ] ; then
source ~/.cd_bookmarks/"$1"
else
echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
fi
;;
# delete bookmark
-d) shift
if [ -f ~/.cd_bookmarks/$1 ] ; then
rm ~/.cd_bookmarks/"$1" ;
else
echo "Oops, forgot to specify the bookmark" ;
fi
;;
# list bookmarks
-l) shift
ls -l ~/.cd_bookmarks/ ;
;;
*) echo "$USAGE" ;
;;
esac
}
安裝
1./ 創建一個文件 ~/.cdb 並將上面的腳本復制到其中。
2./ 在您的 ~/.bashrc 中添加以下內容
if [ -f ~/.cdb ]; then
source ~/.cdb
fi
3./ 重啟你的 bash 會話
用法
1./ 創建書簽
$cd my_project
$cdb -c project1
2./ 轉到書簽
$cdb -g project1
3./ 列出書簽
$cdb -l
4./刪除書簽
$cdb -d project1
5./ 我所有的書簽都存儲在哪里?
$cd ~/.cd_bookmarks
另外,看看CDPATH
cd 命令可用的以冒號分隔的搜索路徑列表,其功能類似於二進制文件的 $PATH 變量。 $CDPATH 變量可以在本地 ~/.bashrc 文件中設置。
ash$ cd bash-doc
bash: cd: bash-doc: No such file or directory
bash$ CDPATH=/usr/share/doc
bash$ cd bash-doc
/usr/share/doc/bash-doc
bash$ echo $PWD
/usr/share/doc/bash-doc
和
cd -
它是與后退按鈕等效的命令行(將您帶到您所在的上一個目錄)。
感謝您分享您的解決方案,我也想分享我的解決方案,我發現它比我以前遇到的任何其他方法都更有用。
該引擎是一個很棒的通用工具:Junegunn 的命令行模糊查找器。
它主要允許您以多種方式“模糊查找”文件,但它也允許向其提供任意文本數據並過濾這些數據。 所以,快捷方式的想法很簡單:我們所需要的只是維護一個帶有路徑(即快捷方式)的文件,並對該文件進行模糊過濾。 它的外觀如下:我們輸入cdg
命令(來自“cd global”,如果您願意的話),獲取我們的書簽列表,只需按幾下鍵即可選擇所需的書簽,然后按 Enter。 工作目錄更改為選擇的項目:
它非常快速和方便:通常我只需輸入所需項目的 3-4 個字母,其他的都已經過濾掉了。 此外,當然我們可以使用箭頭鍵或類似 vim 的鍵綁定Ctrl+j
/ Ctrl+k
在列表中移動。
帶有詳細信息的文章: shell 的模糊快捷方式。
也可以將它用於 GUI 應用程序(通過 xterm):我將它用於我的 GUI 文件管理器Double Commander 。 我也計划寫一篇關於這個用例的文章。
bookmarks.sh為 Bash 4.0+ 版本提供了一個書簽管理系統。 它還可以使用午夜指揮官熱列表。
Bashmarks是一個非常簡單和直觀的實用程序。 總之,安裝后的用法是:
s <bookmark_name> - Saves the current directory as "bookmark_name"
g <bookmark_name> - Goes (cd) to the directory associated with "bookmark_name"
p <bookmark_name> - Prints the directory associated with "bookmark_name"
d <bookmark_name> - Deletes the bookmark
l - Lists all available bookmarks
是的,有 DirB: Directory Bookmarks for Bash 在這篇Linux Journal文章中得到了很好的解釋
文章中的一個例子:
% cd ~/Desktop
% s d # save(bookmark) ~/Desktop as d
% cd /tmp # go somewhere
% pwd
/tmp
% g d # go to the desktop
% pwd
/home/Desktop
受到這里問題和答案的啟發,我將以下~/.bashrc
行添加到我的~/.bashrc
文件中。
有了這個,你就有了一個favdir
命令(功能)來管理你的收藏夾和一個自動完成功能來從這些收藏夾中選擇一個項目。
# ---------
# Favorites
# ---------
__favdirs_storage=~/.favdirs
__favdirs=( "$HOME" )
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
function favdirs() {
local cur
local IFS
local GLOBIGNORE
case $1 in
list)
echo "favorite folders ..."
printf -- ' - %s\n' "${__favdirs[@]}"
;;
load)
if [[ ! -e $__favdirs_storage ]] ; then
favdirs save
fi
# mapfile requires bash 4 / my OS-X bash vers. is 3.2.53 (from 2007 !!?!).
# mapfile -t __favdirs < $__favdirs_storage
IFS=$'\r\n' GLOBIGNORE='*' __favdirs=($(< $__favdirs_storage))
;;
save)
printf -- '%s\n' "${__favdirs[@]}" > $__favdirs_storage
;;
add)
cur=${2-$(pwd)}
favdirs load
if containsElement "$cur" "${__favdirs[@]}" ; then
echo "'$cur' allready exists in favorites"
else
__favdirs+=( "$cur" )
favdirs save
echo "'$cur' added to favorites"
fi
;;
del)
cur=${2-$(pwd)}
favdirs load
local i=0
for fav in ${__favdirs[@]}; do
if [ "$fav" = "$cur" ]; then
echo "delete '$cur' from favorites"
unset __favdirs[$i]
favdirs save
break
fi
let i++
done
;;
*)
echo "Manage favorite folders."
echo ""
echo "usage: favdirs [ list | load | save | add | del ]"
echo ""
echo " list : list favorite folders"
echo " load : load favorite folders from $__favdirs_storage"
echo " save : save favorite directories to $__favdirs_storage"
echo " add : add directory to favorites [default pwd $(pwd)]."
echo " del : delete directory from favorites [default pwd $(pwd)]."
esac
} && favdirs load
function __favdirs_compl_command() {
COMPREPLY=( $( compgen -W "list load save add del" -- ${COMP_WORDS[COMP_CWORD]}))
} && complete -o default -F __favdirs_compl_command favdirs
function __favdirs_compl() {
local IFS=$'\n'
COMPREPLY=( $( compgen -W "${__favdirs[*]}" -- ${COMP_WORDS[COMP_CWORD]}))
}
alias _cd='cd'
complete -F __favdirs_compl _cd
在最后兩行中,創建了一個用於更改當前目錄(具有自動完成功能)的別名。 使用此別名 ( _cd
),您可以更改為您喜歡的目錄之一。 可能您不想將此別名更改為適合您需要的名稱。
使用功能favdirs
您可以管理您的收藏夾(請參閱用法)。
$ favdirs
Manage favorite folders.
usage: favdirs [ list | load | save | add | del ]
list : list favorite folders
load : load favorite folders from ~/.favdirs
save : save favorite directories to ~/.favdirs
add : add directory to favorites [default pwd /tmp ].
del : delete directory from favorites [default pwd /tmp ].
@getmizanur 我使用了你的 cdb 腳本。 我通過添加書簽選項卡完成稍微增強了它。 這是我的 cdb 腳本版本。
_cdb()
{
local _script_commands=$(ls -1 ~/.cd_bookmarks/)
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "${_script_commands}" -- $cur) )
}
complete -F _cdb cdb
function cdb() {
local USAGE="Usage: cdb [-h|-c|-d|-g|-l|-s] [bookmark]\n
\t[-h or no args] - prints usage help\n
\t[-c bookmark] - create bookmark\n
\t[-d bookmark] - delete bookmark\n
\t[-g bookmark] - goto bookmark\n
\t[-l] - list bookmarks\n
\t[-s bookmark] - show bookmark location\n
\t[bookmark] - same as [-g bookmark]\n
Press tab for bookmark completion.\n"
if [ ! -e ~/.cd_bookmarks ] ; then
mkdir ~/.cd_bookmarks
fi
case $1 in
# create bookmark
-c) shift
if [ ! -f ~/.cd_bookmarks/$1 ] ; then
echo "cd `pwd`" > ~/.cd_bookmarks/"$1"
complete -F _cdb cdb
else
echo "Try again! Looks like there is already a bookmark '$1'"
fi
;;
# goto bookmark
-g) shift
if [ -f ~/.cd_bookmarks/$1 ] ; then
source ~/.cd_bookmarks/"$1"
else
echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
fi
;;
# show bookmark
-s) shift
if [ -f ~/.cd_bookmarks/$1 ] ; then
cat ~/.cd_bookmarks/"$1"
else
echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
fi
;;
# delete bookmark
-d) shift
if [ -f ~/.cd_bookmarks/$1 ] ; then
rm ~/.cd_bookmarks/"$1" ;
else
echo "Oops, forgot to specify the bookmark" ;
fi
;;
# list bookmarks
-l) shift
ls -1 ~/.cd_bookmarks/ ;
;;
-h) echo -e $USAGE ;
;;
# goto bookmark by default
*)
if [ -z "$1" ] ; then
echo -e $USAGE
elif [ -f ~/.cd_bookmarks/$1 ] ; then
source ~/.cd_bookmarks/"$1"
else
echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
fi
;;
esac
}
是的,我寫過一個,叫做 anc。
https://github.com/tobimensch/anc
anc代表anchor,但是anc的anchors其實只是書簽而已。
它旨在易於使用,並且有多種導航方式,可以通過提供文本模式、使用數字、交互方式、返回或使用 [TAB] 完成。
我正在積極致力於它,並願意就如何使其變得更好提出意見。
請允許我將 anc 的 github 頁面中的示例粘貼到此處:
# make the current directory the default anchor:
$ anc s
# go to /etc, then /, then /usr/local and then back to the default anchor:
$ cd /etc; cd ..; cd usr/local; anc
# go back to /usr/local :
$ anc b
# add another anchor:
$ anc a $HOME/test
# view the list of anchors (the default one has the asterisk):
$ anc l
(0) /path/to/first/anchor *
(1) /home/usr/test
# jump to the anchor we just added:
# by using its anchor number
$ anc 1
# or by jumping to the last anchor in the list
$ anc -1
# add multiple anchors:
$ anc a $HOME/projects/first $HOME/projects/second $HOME/documents/first
# use text matching to jump to $HOME/projects/first
$ anc pro fir
# use text matching to jump to $HOME/documents/first
$ anc doc fir
# add anchor and jump to it using an absolute path
$ anc /etc
# is the same as
$ anc a /etc; anc -1
# add anchor and jump to it using a relative path
$ anc ./X11 #note that "./" is required for relative paths
# is the same as
$ anc a X11; anc -1
# using wildcards you can add many anchors at once
$ anc a $HOME/projects/*
# use shell completion to see a list of matching anchors
# and select the one you want to jump to directly
$ anc pro[TAB]
除了@Dmitri Frank 的回答- 我已經通過簡單的alias
實現了cdb
命令(又名cd bookmark )(將此行添加到您的~/.bash_profile
):
alias b='cat ~/.cd-bookmarks | grep -v "^\s*#" | grep -v "^\s*$" | fzf'
alias cdb='cd "$(b)"'
創建文件~/.cd-bookmarks
並輸入您的路徑,每行一個。 它通過#
支持空行和注釋:
$ cat ~/.cd-bookmarks
### Devel stuff ###
/Users/pujezdsky/devel/projects/stackoverflow/
### Photo stuff ###
/Users/pujezdsky/Photos/
/Users/pujezdsky/Photos/last-import/
/Users/pujezdsky/Photos/dir with spaces/
不幸的是它不支持~
擴展所以輸入完整路徑。
然后你就可以做到
$ cdb
並且因為b
別名甚至一些更高級的東西
$ mc `b`
$ cp my-file.txt `b`
$ touch `b`/test.sh
不幸的是,如果您的書簽路徑中有空格,則必須引用`b`
調用。 這使它不那么帥:
$mc "`b`"
注 1:
在執行此操作之前,請檢查您是否已經擁有
cdb
、b
命令/別名,以避免它們被覆蓋和故障。 最簡單的方法是使用這些應該返回類似-bash: type: cdb: not found
$ type cdb $ type b
筆記2:
b
別名可以簡化為alias b='egrep -v "(^\\s*#|^\\s*$)" ~/.cd-bookmarks | fzf'
注 3:
您還可以為將當前文件夾添加到書簽中創建別名。 就這么簡單
alias ba='pwd >> ~/.cd-bookmarks'
對於短期快捷方式,我在各自的 init 腳本中有以下內容(抱歉。我現在找不到源代碼,當時也沒有打擾):
function b() {
alias $1="cd `pwd -P`"
}
用法:
在您要添加書簽類型的任何目錄中
b THEDIR # <THEDIR> being the name of your 'bookmark'
它將創建一個別名到 cd(返回)到這里。
返回“書簽”目錄類型
THEDIR
它將運行存儲的別名和 cd 回到那里。
注意:只能使用,如果你明白,這可能會覆蓋現有的shell別名和這意味着什么。
實踐表明,每天不需要那么多書簽。
所以我們可以將它們存儲在腳本中:
function go {
# go home dir if no params
if [ $# -eq 0 ]; then cd ~; return; fi;
# declare an assoc array with CAPITAL -A switch
declare -A o
# declare aliases and targets
o[apd]=$APPDATA
o[cli]='/mnt/c/CLI/'
o[closk]='/mnt/d/JOB/CLosk.Work/Dev._default/'
o[ds]='/mnt/d/JOB/DS/'
o[gh]='/mnt/d/Alpha/GitHub/'
o[pf]='/mnt/c/Program Files/'
o[wsa]='/mnt/d/JOB/WSA/Dev/'
# here we go
if [ ${o[$1]+_} ]; then cd "${o[$1]}"; fi
}
使用關聯數組可以輕松更正鏈接列表。
如您所見,此腳本也在 Windows 下成功使用。 我也在 CentOS 和 Ubuntu 下使用這個腳本。 當然還有其他鏈接。
另外,我正在使用這個:
alias ~='go'
所以:
~ # go to home dir
~ apd # go to system Application Data folder
等等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.