Live data from Hacker News

Quickly navigate your filesystem from the command line

jeroenjanssens.com

121–130 of 148 posts

Re: Quickly navigate your filesystem from the command line

#121
I have no idea if anyone else does this, but I use tmux a lot for different projects. I usually have vim in one tab, logs in another, and tests in another. However, each tab I open I need to cd into the same directory. As such I came up with this helper for zsh. Every time I change a directory it records it. When I start a new session (new tmux or iTerm tab) it goes straight to that directory:

    # record directory in ~/.lastpwd
    record_pwd() {
      pwd > ~/.lastpwd
    }
    chpwd_functions=(record_pwd)
    
    # go to last directory when opening a new shell
    if [[ -d `cat ~/.lastpwd` ]]; then
      cd `cat ~/.lastpwd`
    fi
For other shells that don't have something like chpwd_functions you could just alias cd.

Re: Quickly navigate your filesystem from the command line

#122

I have no idea if anyone else does this, but I use tmux a lot for different projects. I usually have vim in one tab, logs in another, and tests in another. However, each tab I open I need to cd into the same directory. As such I came up with this helper for zsh. Every time I change a directory it records it. When I start a new session (new tmux or iTerm tab) it goes straight to that directory: # record directory in ~…

> However, each tab I open I need to cd into the same directory.

As of tmux 1.7, this should be unnecessary. Every tab and split will open in the working directory of the current pane, and the behavior can be changed with the `default-path` option.

Re: Quickly navigate your filesystem from the command line

#123

Earlier quoted context omitted.

You can hold shift and right-click in Explorer and select "Open Command Window Here" (at least in Win 7 and 8).

Yep, this works fine, until you did it a 7th time and end up with a task bar full of unused console windows.

If you want to reuse an existing cmd window you can type in "cd " then drag/drop the directory from any Explorer shell. You could also close the shell after finishing with it.

Not trying to take anything away from your script, it has a use-case, but just pointing out that there are alternate solutions for systems that don't have your script installed.

Re: Quickly navigate your filesystem from the command line

#124

Thank you all for your kind words and helpful suggestions! I shall adapt both the post and the code accordingly. Jeroen

I have just added (1) quotes to the code, (2) a section about tab completion, and (3) a note for Mac OS X users. Many thanks again; HN is a great community!

Jeroen

Re: Quickly navigate your filesystem from the command line

#126
post #112

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

What many people don't seem to know is that the location bar in Explorer (at least on Windows 8) can act as a run dialog, and it supplies the launched program with the current folder as a parameter. So simply hit Ctrl+L and type "cmd" or "powershell" or whatever command from your PATH and hit enter.

Thanks for the tip!

Neither Ctrl+L nor Alt+D work for me (Win7 Ultimate, 64bit), but even with clicking manually this is extremely useful for me.

Re: Quickly navigate your filesystem from the command line

#127
Great idea!

I don’t want symlinks to be resolved (/var/www becomes /private/var/www on os x) so I modified it to store the path in the file:

  export MARKPATH=$HOME/.marks

  function jump {
    mark=$(head -n 1 "$MARKPATH/$1" 2>/dev/null)

    if [[ $mark != '' ]]; then
      cd $mark
    else
      echo "No such mark: $1"
    fi
  }

  function mark {
    mkdir -p "$MARKPATH"; echo "$(pwd)" > "$MARKPATH/$1"
  }

  function unmark {
    rm -i "$MARKPATH/$1"
  }

  function marks {

    find "$MARKPATH" -type f | while read filename
    do
      printf "%-12s -> %s\n" $(basename ${filename}) $(head -n 1 ${filename})
    done
  }
I think the if syntax is zsh so probably won’t work with bash.

Re: Quickly navigate your filesystem from the command line

#128
post #4

cool! I'm a command-line junkie as well. So, I've been doing something similar for quite a while. Here's what I have done : function ccb () { FOO=`pwd`; echo >> ~/shortcuts.sh; echo alias $1=\'pushd $FOO\' >> ~/shortcuts.sh; echo >> ~/shortcuts.sh; . ~/shortcuts.sh } simple but quite effective. Now, all I have to do is type ccb and give a shortcut name to register a shortcut. Then whenever i want to jump to that dire…

So interesting since I've been using pratically the SAME idea for over an year now :D Shows how we arrive at nice solutions independently. PS: I assume you are calling shortcuts.sh in your .profile - that way aliases are available across sessions.

yes. I call shortcuts.sh from my .bashrc

Re: Quickly navigate your filesystem from the command line

#129

I had to modify Jeroen's code to get the `marks` shortcut working under Mac OS 10.8: 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- && echo }

I went with:

  function marks {
      ls -l $MARKPATH | sed -E 's/ +/ /g' | cut -d' ' -f9- | sed -E 's/ -/     -/g' && echo
  }
where the "\t" (looks like a bunch of spaces in the second sed statement) was typed in using [Ctrl] + [V] and then [Tab].

Re: Quickly navigate your filesystem from the command line

#130

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

-f9- for me in cut, but otherwise it works :). Thanks!
Post reply on HN