Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A directory bookmark feature of some sort #2

Open
dimo414 opened this issue Sep 1, 2017 · 2 comments
Open

A directory bookmark feature of some sort #2

dimo414 opened this issue Sep 1, 2017 · 2 comments
Labels
enhancement New feature or request major

Comments

@dimo414
Copy link
Owner

dimo414 commented Sep 1, 2017

Original report by Michael Diamond (Bitbucket: dimo414).


Both named bookmarks (e.g. foo=/path/to/some/important/foo) and key directories (e.g. /the/subdirectories/of/this/dir/are/interesting) would be nice to have shortcuts to, so that you could say (from anywhere):

$ bd foo
$ bd bar # where bar is a subdirectory of the .../interesting directory above

Bonus points for subdirectory support, e.g. bd foo/bar cd's to the bar subdirectory of the foo bookmark.

Similar projects include:

@dimo414
Copy link
Owner Author

dimo414 commented Apr 17, 2018

Original comment by Michael Diamond (Bitbucket: dimo414).


Introduce a goto function which supports jumping to directories via fzf. Does not support further subdirectories, which would be nice. See #2.

@dimo414 dimo414 added major enhancement New feature or request labels Feb 23, 2020
@dimo414
Copy link
Owner Author

dimo414 commented May 17, 2020

I investigated tweaking goto to support arbitrary globs, rather than just subdirectories. Ultimately I'm not convinced this is preferable, since globs are so messy. This approach requires the caller to escape all globs (to ensure they're expanded lazily) and forces goto to muck with unquoted array expansions, wordsplitting, and the like.

Since unquoted array expansions trigger glob expansion (and word-splitting...) paths=(${globs[@]}) will mostly work, as long as globs doesn't contain any whitespace. It's OK if the expanded paths contain whitespace, though. To account for whitespace in globs it's necessary to disable word-splitting with IFS=, and shopt -s nullglob is necessary to avoid including unmatched globs.

For posterity:

# Uses https://github.com/junegunn/fzf "fuzzy find" to support easily cd-ing
# to commonly used directories. To register paths for use with this function
# add them to one of the following arrays:
# * GOTO_PATHS  Immediate subdirectories of the given path(s)
# * GOTO_GLOBS  Directories matching the given globs; be sure to quote these
#               globs so they are lazily expanded when you invoke goto.
# * GOTO_DIRS   Literal paths to include directly
# Arguments to this function seed the fuzzy find; if the arguments match
# exactly one path skips the find UI and jumps straight to the directory.
goto() {
  local fzf_flags=()
  # if an argument is passed and does not contain a / search only against the
  # final directory.
  # Maybe this should be the default behavior, even with no arguments?
  # That would require users to say `goto /` in order to search the full path
  # which would be somewhat surprising.
  if (( $# > 0 )) && [[ "$*" != *'/'* ]]; then
    fzf_flags=('--delimiter=/' '--nth=-1')
  fi

  local path
  path=$(
    { shopt -s nullglob # discard unmatched globs
      IFS= # disable wordsplitting; https://stackoverflow.com/a/26554333/113632
      paths=(${GOTO_GLOBS[@]} "${GOTO_DIRS[@]}") # globs intentionally unquoted
      for dir in "${GOTO_PATHS[@]}"; do
        paths+=("${dir}"/*)
      done
      (( ${#paths[@]} > 0 )) && find "${paths[@]}" -maxdepth 0 -type d -print0
    } | sort -z \
      | fzf --tiebreak=end --select-1 --exit-0 --read0 "${fzf_flags[@]}" \
        --query="$*"
  ) || return
  cd "$path"
}

One goal was to let goto support subdirectories, dropping you into a foo/dir_i_care_about directory with goto foo. This doesn't work as-is, however, because if any arguments are passed to goto it configures fzf's --delimiter=/ --nth=-1 behavior constraining the search to the last directory in the path, i.e. just dir_i_care_about. I'm not sure I want to do away with that behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request major
Projects
None yet
Development

No branches or pull requests

1 participant