Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

111–120 of 179 posts

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

#111
post #41

Earlier quoted context omitted.

For this exact use case I defined the following many years ago in my bash profile: # create a new directory and enter it function mkd() { mkdir -p "$@" && cd "$_"; } And use it like: mkd foo

Beware that using "$@" there might not be what you want, as it passes space-separated arguments to mkdir. What does: "mkd foo bar" do? It creates both directories, and cd's into "bar". Might be worth checking that only one argument has been passed, or use "$*" which instead would treat all arguments as one parameter, and "mkd foo bar" would create a "foo bar" directory instead (and cd to it) YMMV.

All Bash manuals and tutorials should start from explaining this issue. And I really mean it. I can't remember how many times I was bitten by it until I finally read it up.

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

#113
post #5

I use `z` as a less manual way to search common directories instead of changing $CDPATH. It's based on 'frecency'. https://github.com/rupa/z

zsh, oh-my-zsh and z are the must haves that I add to any new environment immediately. Z and similar tools is such an elegant solution to directory navigation.

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

#114

Here is one additional bookmark solution [1]: export CDPATH=.:~/.marks/ function mark { ln -sr "$(pwd)" ~/.marks/"$1" } mark @name # create a bookmark cd @name # jump to bookmark cd @ # list bookmarks cd @n # auto-complete cd @name/ # can access sub-directories within bookmarks Works best when bookmarks are prefixed by "@" or other special symbol. Additional advantage is that you can use the same function `cd` to nav…

Pretty sure this would change into the symlinks, not the linked directories. I.e. the $PWD would be under `~/.marks/`.

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

#115
post #5

I use `z` as a less manual way to search common directories instead of changing $CDPATH. It's based on 'frecency'. https://github.com/rupa/z

zsh, oh-my-zsh and z are the must haves that I add to any new environment immediately. Z and similar tools is such an elegant solution to directory navigation.

Same, I don't know how people do without them.

I love Z but I cannot believe there isn't something better yet?

One thing I'd love is something that helps navigating the current git project easier.

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

#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.

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

#117
post #114

Here is one additional bookmark solution [1]: export CDPATH=.:~/.marks/ function mark { ln -sr "$(pwd)" ~/.marks/"$1" } mark @name # create a bookmark cd @name # jump to bookmark cd @ # list bookmarks cd @n # auto-complete cd @name/ # can access sub-directories within bookmarks Works best when bookmarks are prefixed by "@" or other special symbol. Additional advantage is that you can use the same function `cd` to nav…

Pretty sure this would change into the symlinks, not the linked directories. I.e. the $PWD would be under `~/.marks/`.

Personally - never noticed a problem, but should be easy to solve just in case:

    alias cd="cd -P"

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

#118
post #100

Or you just use a shell like fish that already comes with a lot of useful defaults. No need to customize everything by yourself, just use what other smart people already created. https://fishshell.com/

> No need to customize everything by yourself, just use what other smart people already created.

Every once and then I decide to use and love the fish shell, and I commit to using it exclusively for a few days. My first session always starts with 40 minutes trying to disable all stupid colors. I don't like my terminal to look like a kitsch Christmas tree! Then, I feel frustrated with "jumps" of the interface when editing multi-line command lines; inevitably leading me to quit fish and go back to bash. I'm obviously not smart enough to appreciate the defaults that these smart people have created.

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

#119
post #2

I upvoted for this alone: Another neat short hand is !$ which is an alias for the last argument of the previous command. This can be handy if one creates a new directory and wants to change into it without typing the the directory again. So commands would be mkdir -p make/new/directory cd !$ You've no idea how many years I've been thinking "This is Linux fer gawd's sake. Surely there must be some easy way to make a n…

In zsh, you use -> % take which makes the dir and cd's into it.

The bash version works in zsh too.

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

#120
post #100

Or you just use a shell like fish that already comes with a lot of useful defaults. No need to customize everything by yourself, just use what other smart people already created. https://fishshell.com/

> No need to customize everything by yourself, just use what other smart people already created. Every once and then I decide to use and love the fish shell, and I commit to using it exclusively for a few days. My first session always starts with 40 minutes trying to disable all stupid colors. I don't like my terminal to look like a kitsch Christmas tree! Then, I feel frustrated with "jumps" of the interface when edi…

Probably you are smarter then the people who created fish :)
Post reply on HN