Live data from Hacker News

Quickly navigate your filesystem from the command line

jeroenjanssens.com

101–110 of 148 posts

Re: Quickly navigate your filesystem from the command line

#101

To have completion with bash you can use this: function _jump { local cur=${COMP_WORDS[COMP_CWORD]} local marks=$(find $MARKPATH -type l -printf "%f\n") COMPREPLY=($(compgen -W '${marks[@]}' -- "$cur")) return 0 } complete -o default -o nospace -F _jump jump Bash completion really needs better docs :-|

Hmmm, this doesn't seem to work in `zsh`. Typing `jump ` suggests everything in the current directory (folders and files). Does anyone here know offhand how autocompletion in `zsh` works?

Yeah, I'm a bash user.

Re: Quickly navigate your filesystem from the command line

#102

For Windows users I have a closely related shameless plug: cdhere navigates your cmd.exe to wherever the current topmost Explorer window is pointed at. It's not the same as the OP's trick, of course, but since you're voluntarily using Windows, I'm going to assume you "live" on the command line less than the average UNIX user, and more on GUI tools such as good old Explorer. https://github.com/eteeselink/cdhere

Another useful windows trick: to open a new file explorer window at the pwd of the command prompt, type 'explorer .'.

Re: Quickly navigate your filesystem from the command line

#103

Earlier quoted context omitted.

I used to have a really hacky AppleScript to do the same thing in OS X. The crappy part was that launching the whole AppleScript runtime was really slow, but that was the only API I could find with getting that information from the Finder.

You can also drag any file or folder onto your terminal, which will paste the absolute filepath at your cursor point.

Yeah, but I rarely use the mouse when using either a file browser or the terminal, so switching to it is less convenient than a short terminal command.

Re: Quickly navigate your filesystem from the command line

#104

You can also use ranger [1] to change directories, especially if you want to explore the directory tree quickly when you don't know exactly where to jump to. Install it and add the following to ~/.bashrc: function ranger-cd { tempfile='/tmp/chosendir' /usr/bin/ranger --choosedir="$tempfile" "${@:-$(pwd)}" test -f "$tempfile" && if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then cd -- "$(cat "$tempfile")" fi r…

Thanks! I was already using ranger and this fits nicely with the i3 ethos as well.

Re: Quickly navigate your filesystem from the command line

#105
Here's a `fish 2` shell version, that works on the Mac. You can add it as `~/.config/fish/functions/mark.fish`:

    set MARKPATH $HOME/.marks
    function jump
      cd $MARKPATH/$argv; or echo "No such mark: $argv"
    end
    function mark
      mkdir -p $MARKPATH; and ln -s (pwd) $MARKPATH/$argv
    end
    function unmark
      rm -i $MARKPATH/$argv
    end
    function marks
      ls -l $MARKPATH | cut -d' ' -f12-; and echo
    end

Re: Quickly navigate your filesystem from the command line

#108
I've been using the following in some form for over 3 decades! (now in zsh)

    alias   .=cd
In /bin/sh "." sources a script. While this can be common in a script, one rarely does this interactively so I usurped . for the most common operation!

    alias   ,=pushd
    alias   ,,=popd
    alias   ,.=dirs
Think of cd as a goto, pushd as a call, popd as a return and dirs as a stack trace!

    ..() { cd ../$* }
    cdpath=(. .. ~/src ~)
Use of cdpath can be confusing so it is best to show part of $PWD in the prompt:

    PS1="%* %5. %h%(#.#.:) "
This ends the prompt with a # if you are running as superuser.

These aliases/functions are enabled in .zshrc (only if run interactively -- that is, they are included below the following test):

    if [[ ! -o interactive ]] ; then return; fi
The "benefit" of Jeroen's mark/unmark is that these paths persist (and can be used from any window running a shell. I have not found much need for that + I can have different $dirs in different windows. Also, given that my shell windows (under screen) persist for a very long time, I don't need symlink's persistence!

Alternatively I define "." to be a function that does arg specific operation (cd if a dir, acroread if .pdf, vi if text etc.).

Re: Quickly navigate your filesystem from the command line

#109
post #7

You might want to check out z: https://github.com/rupa/z

https://github.com/clvv/fasd I used to use z, but I've since switched to fasd. It's much like z, but also so much better. In addition to being able to do "z " you can do "f ". So, for example, if I've got a rails project I've worked on a lot recently I know its config.ru is high on frecency, so I'll just do "vim `f config`" to edit that file. If I want a file that's not so recent I can always do "vim `f -i config`" t…

I put z and fasd through their paces, and out of the box z works very intuitively. I like it. fasd takes a little more to get used to, but I'm going to put it through a full try out. Let's see if the additional features make it worth the while. In either case, thanks all for a great thread.
Post reply on HN