【问题标题】:Directory bookmarking for bashbash 的目录书签
【发布时间】:2011-11-14 12:42:11
【问题描述】:

是否有任何用于 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

【问题讨论】:

标签: linux bash ubuntu


【解决方案1】:

实践表明,每天都不需要那么多书签。

所以我们可以将它们存储在脚本中:

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

等等。

【讨论】:

    【解决方案2】:

    除了@Dmitri Frank 的answer - 我还通过简单的alias 实现了cdb 命令(又名cd 书签)(将此行添加到您的~/.bash_profile):

    alias b='cat ~/.cd-bookmarks | grep -v "^\s*#" | grep -v "^\s*$" | fzf'
    alias cdb='cd "$(b)"'
    

    创建文件~/.cd-bookmarks 并输入您的路径,每行一个。它通过#支持空行和cmets:

    $ 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:

    在您执行此操作之前,检查您是否已经有 cdbb 命令/别名以避免它们被覆盖和故障。最简单的方法是使用这些返回类似-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'
    

    【讨论】:

      【解决方案3】:

      对于短期快捷方式,我在各自的初始化脚本中有以下内容(抱歉。我现在找不到源代码,当时没有打扰):

      function b() {
          alias $1="cd `pwd -P`"
      }
      

      用法:

      在任何你想加入书签的目录中

      b THEDIR # <THEDIR> being the name of your 'bookmark'
      

      它会为 cd (back) 创建一个别名到这里。

      返回到“书签”目录类型

      THEDIR
      

      它将运行存储的别名并返回那里。

      警告:仅在您了解这可能会覆盖现有的 shell 别名和 什么 的含义时使用。

      【讨论】:

        【解决方案4】:

        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
        

        【讨论】:

        • 我必须对此做的唯一更改是将l 函数的名称更改为lb,因为l 已经是我的特定ls 咒语的别名bash 外壳。但是,是的,一个非常酷、直观且简单的脚本!
        【解决方案5】:

        是的,我写过的,叫做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]
        

        【讨论】:

          【解决方案6】:

          @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
          }
          

          【讨论】:

            【解决方案7】:

            感谢您分享您的解决方案,我也想分享我的解决方案,我发现它比我以前遇到的任何其他方法都更有用。

            引擎是一个很棒的通用工具:command-line fuzzy finder,Junegunn。

            它主要允许您以多种方式“模糊查找”文件,但它也允许向其提供任意文本数据并过滤此数据。因此,快捷方式的想法很简单:我们只需要维护一个带有路径(即快捷方式)的文件,并对这个文件进行模糊过滤。它的外观如下:我们输入cdg 命令(如果您愿意,可以从“cd global”获取),获取我们的书签列表,只需几次击键即可选择所需的一个,然后按 Enter。工作目录更改为选取的项目:

            非常快捷方便:通常我只需键入所需项目的 3-4 个字母,其他所有字母都已过滤掉。此外,我们当然可以使用箭头键或类似 vim 的键绑定 Ctrl+j/Ctrl+k 在列表中移动。

            详细文章:Fuzzy shortcuts for your shell.

            它也可以用于 GUI 应用程序(通过 xterm):我将它用于我的 GUI 文件管理器 Double Commander。我也打算写一篇关于这个用例的文章。

            【讨论】:

            • 感谢fzf 命令千百次!我已经用我简单的cdg 实现更新了你的答案。希望它可以帮助其他人:)
            • @PetrÚjezdský,我认为如果您只写自己的答案会更好;首先,这对读者来说会更清楚,其次,如果有一天你想编辑你的答案,我们俩都会更容易。谢谢。
            • 好吧,我认为这会使答案更加混乱。但是,我做了你想要的。我的简单cdg 实现可以在我的answer 中找到。祝大家cding 快乐 :)
            • 谢谢。我是这样使用的:添加书签=alias cdb='sort -u -o ~/.fzfcd &lt;(pwd) ~/.fzfcd',模糊cd=alias cdf='cd "$(fzf &lt; ~/.fzfcd)"'
            【解决方案8】:

            受此处问题和答案的启发,我将以下几行添加到我的~/.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 ].
            

            【讨论】:

              【解决方案9】:

              是的,有 DirB:Bash 的目录书签在 this Linux Journal article 中有很好的解释

              文章中的一个例子:

              % cd ~/Desktop
              % s d       # save(bookmark) ~/Desktop as d
              % cd /tmp   # go somewhere
              % pwd
              /tmp
              % g d       # go to the desktop
              % pwd
              /home/Desktop
              

              【讨论】:

                【解决方案10】:

                bookmarks.sh 为 Bash 4.0+ 版本提供了一个书签管理系统。它还可以使用午夜指挥官热门列表。

                【讨论】:

                  【解决方案11】:

                  另外,看看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 -
                  

                  它相当于后退按钮的命令行(将您带到您所在的上一个目录)。

                  【讨论】:

                  • 我更喜欢这个解决方案,因为我有很好的目录结构。
                  【解决方案12】:

                  在 bash 脚本/命令中,
                  你可以使用pushdpopd

                  pushd

                  保存然后更改当前目录。没有参数,pushd 交换前两个目录。

                  用法

                  cd /abc
                  pushd /xxx    <-- save /abc to environment variables and cd to /xxx
                  pushd /zzz
                  pushd +1      <-- cd /xxx
                  

                  popd是去掉变量(逆向方式)

                  【讨论】:

                  • 这是我使用的,但如果该人只想在目录之间来回跳转,我会推荐cd -
                  猜你喜欢
                  • 2011-03-08
                  • 2016-12-27
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-02-02
                  • 1970-01-01
                  • 2011-10-10
                  相关资源
                  最近更新 更多