Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

91–100 of 179 posts

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

#91

I have a few functions in my bashrc for fast directory navigation. up - cd .. # times upto - find dir in the current path above you and cd to it down - find dir in the current tree below you including inside any number of subdirectories and cd to it https://github.com/robmccoll/dotfiles/blob/master/bashrc#L15...

[deleted]

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

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

escpae-dot (.) also works for this: "It also works to use !$ instead of escape-dot, but that is slightly harder to type, so I don’t use that anymore." Wrote about it here: https://henrikwarne.com/2018/08/11/my-favorite-command-line-...

Which you can type as ALT-.

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

#93
post #67

Earlier quoted context omitted.

I have an "mkcd" function in my startup files: mkcd() { mkdir -vp "$1" && cd "$1"; }

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)

  $ echo "test" > /dev/null && echo !$
  /dev/null
  $ echo "test" > /dev/null && echo $_
  test

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

#94
post #55

Earlier quoted context omitted.

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.

[ESC + .] and [ALT + .] are most definitely news ones for me. Thank you. Always learn something new here!

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

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

#95

Earlier quoted context omitted.

escpae-dot (.) also works for this: "It also works to use !$ instead of escape-dot, but that is slightly harder to type, so I don’t use that anymore." Wrote about it here: https://henrikwarne.com/2018/08/11/my-favorite-command-line-...

Which you can type as ALT-.

This is the `yank-last-arg` readline comment. Bound to M-. and M-_ by default.

"Insert last argument to the previous command (the last word of the previous history entry). With a numeric argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last word (or the word specified by the argument to the first call) of each line in turn."

https://tiswww.case.edu/php/chet/readline/readline.html#IDX9...

Its sibling is `yank-nth-arg`, bound to M-C-y. So if you want the 2nd argument from the last command, you can press M-1 followed by M-C-y (yeah, arguments are indexed from 0)...

https://tiswww.case.edu/php/chet/readline/readline.html#IDX9...

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

#96
post #55

Earlier quoted context omitted.

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.

[ESC + .] and [ALT + .] are most definitely news ones for me. Thank you. Always learn something new here!

or man bash /Readline Command Names

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

#97
post #67

Earlier quoted context omitted.

I have an "mkcd" function in my startup files: mkcd() { mkdir -vp "$1" && cd "$1"; }

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 would not that !$ is a bash-ism, whereas $_ works at least on zsh and ksh as well as bash.

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

#99
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?

Yes. $_ is documented here: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash....

And !$ is documented here: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash....

Post reply on HN