Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

161–170 of 179 posts

Re: How to navigate directories faster with Bash (2015)

#161
post #7

Earlier quoted context omitted.

Typing Esc then . does the trick too.

Only in emacs editing mode. Real programmers use set -o vi

I use that on test machines a lot and had put it in bashrc and heard some cussing from other devs so now I just have to type it every time I spend more than acouple seconds in a terminal :) .

Re: How to navigate directories faster with Bash (2015)

#162
post #22

https://github.com/junegunn/fzf also has a “cd mode”, by default triggerable using Alt-C, which can be useful.

Binding Ctrl-R to `fzf` for fuzzy history search is a killer feature.

Yeah this is one of the first things I install on a new machine along with zoxide

Re: How to navigate directories faster with Bash (2015)

#163

For me once I moved to zsh and added `oh my zsh` it really became so much easier. I like the autocomplete, the fact that when cd-ing a known path you can just type the first leter of each directory with slashes, showing the git branch in working dir. Its the little things.

You should try out out presto sometime :) .

Re: How to navigate directories faster with Bash (2015)

#164
post #48

For me once I moved to zsh and added `oh my zsh` it really became so much easier. I like the autocomplete, the fact that when cd-ing a known path you can just type the first leter of each directory with slashes, showing the git branch in working dir. Its the little things.

'oh my zsh' is fun, but they really throw the kitchen sink in there. I had to get rid of it because terminal startup speed and doing anything in large folders was incredibly slow If all the components of 'oh my zsh' were compiled into the shell and shared data structures, I bet it would be as fast as bash and only limited by IO

I believe that a lot the stuff you like from oh-my-zsh is in fish natively. You might check it out sometime.

Re: How to navigate directories faster with Bash (2015)

#165

Earlier quoted context omitted.

https://www.gnu.org/software/bash/manual/html_node/Bindable-... is a must read.

Thanks, I discovered C-o thanks to you (even though I have gone through the bash manual several times). No, more having to press C-r multiple times just to get sequential group of commands from history.

Yes I was also impressed when I found out about this one. However I use it far less frequently than ALT-. . ALT-# is also useful in the frequent cases where I am constructing a complicated commmand but I have to run another command before the current one.

Also keep in mind that most interactive command-line tools are built upon readline and can understand all those bindings (think python REPL, mysql/psql...)

Re: How to navigate directories faster with Bash (2015)

#166
post #137

The most important tool I found for switching directories is called "z". You just partially type a previously visited directory's name and it takes you there. It also keeps track of your history to rank candidates for popularity. https://github.com/rupa/z/ It's basically CDPATH on stereoids and doesn't require preconfiguration like CDPATH.

Similar is https://lib.rs/crates/zoxide if you're a fan of rust based solutions :) .

I don't think zoxide keeps an eye on shell history or uses a shell hook to see which dirs you navigate to manually? It seems it collects history only through its own CLI?

Re: How to navigate directories faster with Bash (2015)

#167

Those bunch of dots aliases are a bit ridiculous. What are you gonna do, sit and count dots?

I have a simple pair of functions which I use to go up a level based on typing the portion of the path I want to go up until:

  # When called as `argu string`, prints the closest ancestor directory which
  # contains "string". If none is found, or "string" is empty, the current
  # working directory is printed. This function is useful if you need to specify
  # a file as an argument, but the relative path to it involves moving several
  # directories up from the current directory. For example, instead of doing:
  #  $ pwd
  #  /this/is/a/very/long/directory/path/for/demonstration/purposes/
  #  $ ls ../../../../another/directory/something.log
  # You could do:
  #  $ ls `argu dir`/another/directory/something.log
  argu() {
    if [[ "$1" == "" ]]; then
        echo "`pwd`/"
        return
    fi
    echo "`pwd | sed "s/^\(.*"$1"[^/]*\)\/.*$/\1/g"`/"
  }

  # When called as `cdu string`, changes directories into the closest ancestor
  # directory of the current directory which contains "string". If none is found,
  # or "string" is empty, no changes are made to the current directory.
  cdu() {
    cd `argu $1`
  }
I also put the following in ~/.zsh/completion/_cdu so I can get tab-completion of the portions of the path:

  #compdef cdu
  _cdu_parents=$(pwd | tr '/' ' ')
  _arguments "1:parents:($_cdu_parents)"
Or bash (in ~/.bash/completion/cdu):

  # bash completion for cdu

  _cdu()
  {
    local cur prev split=false
    COMPREPLY=()
    _get_comp_words_by_ref -n : cur prev
    _dirs=$( pwd | tr '/' ' ' )
    COMPREPLY=( $( compgen -W "$_dirs" -- "$cur" ) )
  }

  complete -F _cdu cdu

  # Local variables:
  # mode: shell-script
  # sh-basic-offset: 4
  # sh-indent-comment: t
  # indent-tabs-mode: nil
  # End:
  # ex: ts=4 sw=4 et filetype=sh

Re: How to navigate directories faster with Bash (2015)

#168
post #116

I do the following [1]: - I define "cdn" to be what others call "mkcd", as then if I have a command line "cd foo" and it tells me that foo doesn't exist, I can just add the 'n' to the previous entry. I also overload "cdn" so that when not given any argument, it goes into the newest subdirectory in the current directory. - "u", "uu", "uuu", "uuuu", "uuuuu" for going "up" that many levels, and unlike the aliases in OP,…

cdn is a good idea. I'm going to steal it, and change it to ncd, so that all i have to do to add that 'n' is C-a to the begin of line.

There's a small benefit to adding the n after the cd: When my thought process goes like "I need to go into directory" I can already type cd, then "oh that doesn't exist yet" then I can type the "n", then I can type out the "foo". You could possibly argue though that "cd foo -n" would be the right thing to type then. Or, finish "cd foo" then ctl-a "n" as you say.

Re: How to navigate directories faster with Bash (2015)

#169

Earlier quoted context omitted.

Thanks, I discovered C-o thanks to you (even though I have gone through the bash manual several times). No, more having to press C-r multiple times just to get sequential group of commands from history.

Yes I was also impressed when I found out about this one. However I use it far less frequently than ALT-. . ALT-# is also useful in the frequent cases where I am constructing a complicated commmand but I have to run another command before the current one. Also keep in mind that most interactive command-line tools are built upon readline and can understand all those bindings (think python REPL, mysql/psql...)

>However I use it far less frequently than ALT-.

Haha of course. I use it so frequently I can't imagine how anyone uses their shell without it.

>can understand all those bindings

I knew that but just being reminded helped me realize that C-o is probably going to be useful in the REPL as well. Thanks.

Re: How to navigate directories faster with Bash (2015)

#170

Earlier quoted context omitted.

I don't think this is documented anywhere that I've found, but git checkout - checks out the previous branch you were on, similar to cd -

Big fan of git-aliasing this to `git back`. I actually use it a ton and that small amount of extra chars plus the "-" makes a pretty decent difference for me.

Speaking of extra characters, I'm pretty far in with the `git` commands. `git checkout` -> `g c`, `git status` -> `g s`.

I usually do `g c -` but I guess `g back` wouldn't be bad.

Post reply on HN