Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

121–130 of 179 posts

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

#121

Earlier quoted context omitted.

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.

I still have to read it up from time to time even though I know it already. The difference is huge.

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

#122
post #13
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

After trying such history-based automatic tools I found they lacked predictability for me. So the very small 50 LOC kd was born: kd # jumps to project top level (based on .git/Gemfile) kd foobar "$PWD" # saves an entry named foobar pointing to $PWD kd foo # jumps to foobar cp qux $(kd foo) # expands to foobar's value I should probably make it expand to absolute paths so that I could change "$PWD" to . https://github.…

Nice, looks like mine from ~20 years ago

https://gist.github.com/pcdv/3c52d82f59fe9042793033d5e249b14...

    g .       # add current dir to bookmarks
    g         # list bookmarks
    g del     # open bookmarks in vi (should use $EDITOR ...)
    g  # jump to first matching bookmark
Wonder how many monthdays I saved compared to colleagues still navigating manually :D

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

#124

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,…

Reminds me of the family of LISP functions to access specific elements of a list, for example `(caddr l)` being defined as `(car (cdr (cdr l)))`

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

#125

Earlier quoted context omitted.

Wait, now I'm confused. I have: mkcd() { mkdir -p "$@" && cd "$_"; } Can someone please detail the differences between $! and $_? (Apologies if I'm missing something obvious)

You're thinking of $_ and !$, and I think they are equivalent - $_ is explicitly the last argument of the previous command, whereas !$ is a history expansion command. ! starts a new history expansion command, and $ is a word designator for the last word of the selected history entry. To quote man bash, " If a word designator is supplied without an event specification, the previous command is used as the event." I wou…

[deleted]

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

#126
Another one:

  cdof () {
    cd "$(dirname $1)"
  }
as in 'Change into Directory Of"

Changes into the directory of a file. This is useful when you're doing complicated commands with long path names, but realize it would be easier if you were to cd into that directory. Use in combination with . Especially useful if the filename component is long:

  $ pwd
  => ~/
  $ ls -l path/to/whatever/deep/somewhere/2021-04-22-filewithverylongnamefoobarbaz.xyz
  $ cdof 
  $ pwd
  => path/to/whatever/deep/somewhere

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

#127
post #126

Another one: cdof () { cd "$(dirname $1)" } as in 'Change into Directory Of" Changes into the directory of a file. This is useful when you're doing complicated commands with long path names, but realize it would be easier if you were to cd into that directory. Use in combination with . Especially useful if the filename component is long: $ pwd => ~/ $ ls -l path/to/whatever/deep/somewhere/2021-04-22-filewithverylongn…

oooh; stealing this nice.

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

#128
post #4
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…

I hava a mkcd alias to create a directory and changing into it.

I have one called `enter`.

    enter () 
    { 
        mkdir -p "$1";
        cd "$1"
    }

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

#129
post #68

Earlier quoted context omitted.

https://stackoverflow.com/questions/41385015/what-is-bang-do...

Looks like !$ is a readline feature and $_ is a shell variable?

I don't think !$ is properly understood as a readline feature. It's a sh/bash abbreviation.

!! -> last whole command

!!:1 -> first argument of last command

!!:$ -> last argument of last command

!$ -> abbreviation for above

Post reply on HN