Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

41–50 of 179 posts

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

#41
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…

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

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

#42
post #3

I vote for creating a « mkcd » alias !!

# create a directory in the current directory and cd to it function mkcd() { mkdir -p "$@" && eval cd "\"\$$#\""; }

Why the eval? This works fine for me:

  mkcd(){ mkdir --parents -- "$@" && cd -- "$@"; }

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

#43

Wow I’m just blown away by this. Really cool trick to navigate directories fast I’m definitely going to add this to my .zshrc Honestly commandline has made so many things easier for me that I don’t even use regular apps besides my browser for browsing web anyhow using commandline has really made me productive And I always look for automating or making the workflow better Anyways great post thanks for sharing:)

For Zsh, there are better stuff available. For example, you can use `cd +` to change to a past directory. And instead of aliases for `.....`, you can use:

    # Expand ... to ../..
    function vbe-expand-dot-to-parent-directory-path() {
      case $LBUFFER in
        (./..|* ./..) LBUFFER+='.' ;; # In Go: "go list ./..."
        (..|*[ /=]..) LBUFFER+='/..' ;;
        (*) LBUFFER+='.' ;;
      esac
    }
    zle -N vbe-expand-dot-to-parent-directory-path
    bindkey "." vbe-expand-dot-to-parent-directory-path
    bindkey -M isearch "." self-insert

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

#44
post #7

Earlier quoted context omitted.

Typing Esc then . does the trick too.

Alt + . does the same thing as well, with the added advantage that you can keep pressing alt and then hit . several times to go through all the "last arguments" in your history one by one. Further, holding Alt followed by numeric argument followed by dot, gives you an argument at a specific position. For example, Alt + 1, Alt + . copies the first argument.

TIL! Oh this one is nice... now just to get my fingers to remember it.

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

#47

Earlier quoted context omitted.

Cool one. "Ctrl + R" also does the same thing to search through past commands and then "Tab" to select the command you are looking for.

May favorite bash "trick" is: !-1:gs/hello/world which runs the last command with all instances of hello replaced by world. !-1 can be any so called event designator, for instance !n to refer to any specific line number from history or !string for last command containing a particular string. Seriously people, read the man page for bash. Your shell has so many cool features.

And if you only have a single instance to replace, you can use ^old^new^

$ echo "hello hello hello world" hello hello hello world $ ^hello^bonjour^ echo "bonjour hello hello world" bonjour hello hello world

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

#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

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

#49
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…

Is !$ the same as $_ ?

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

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

#50

Earlier quoted context omitted.

May favorite bash "trick" is: !-1:gs/hello/world which runs the last command with all instances of hello replaced by world. !-1 can be any so called event designator, for instance !n to refer to any specific line number from history or !string for last command containing a particular string. Seriously people, read the man page for bash. Your shell has so many cool features.

And if you only have a single instance to replace, you can use ^old^new^ $ echo "hello hello hello world" hello hello hello world $ ^hello^bonjour^ echo "bonjour hello hello world" bonjour hello hello world

I know that this expansion exists, but never use it in practice.

Why? The last command is more general, you can just drop the g from the command I wrote to only replace the first occurrence. Besides, :[g]s/old/new should already be familiar to anyone who has used vim, so that is almost like having to learn zero new things.

Post reply on HN