News for hackers. Stuff that matters.
Re:Ask HN: Teach me something new by hiisukun on Saturday April 30, 2022 @02:25AM (#31213846)
In a lot of terminals, Alt+. (or Option+. on Mac) will paste in the last argument of the previous command.

    $ mkdir hi
    $ cd <alt+.>
You can hit . several times to cycle through earlier final args.
Re:Ask HN: Teach me something new by kmarc on Saturday April 30, 2022 @06:33AM (#31214871)
More precisely, this is not a feature of the terminals but of readline.

https://www.man7.org/linux/man-pages/man3/readline.3.html

All engineers working with the command line (even after 35 years) really, really should open and read

    man bash
and

    man readline
Re:Ask HN: Teach me something new by mikevin on Sunday May 01, 2022 @03:07PM (#31227731)
man 3 readline

to make sure you don't end up with something like urxvt-readline(1)

Re:Ask HN: Teach me something new by schoen on Saturday April 30, 2022 @02:42AM (#31213912)
Wow, I did not know that!

I would have referred to this using !$, which is one of the Bourne shell history substitutions. Like

  $ mkdir awesome
  $ cd !$
(The "!" has the connotation "history" in the shell, while the "$" has the connotation "last", as in regular expressions.)

An advantage of the way you mentioned is that you could actually see the history item displayed on the command line before running the command!

Re:Ask HN: Teach me something new by senjin on Saturday April 30, 2022 @10:23AM (#31216281)
I use !$ multiple times every day but you just helped me make the connection to try !^ for the first argument and I think I'm going to start using that as well. Thanks!
Re:Ask HN: Teach me something new by schoen on Saturday April 30, 2022 @02:45PM (#31218198)
There is also "!!" for the entire prior command line. Someone elsewhere in this thread mentioned the usefulness of

  sudo !!
for "run the entire previous command under sudo".

Another example could be

  echo "!!" > myscript
for "save the previous command into the file myscript".
Re:Ask HN: Teach me something new by senjin on Sunday May 01, 2022 @09:50AM (#31224999)
I also use cat `which !!` every once in a while
Re:Ask HN: Teach me something new by aasasd on Saturday April 30, 2022 @09:02PM (#31221071)
At least in zsh, there are also !:1 for the first argument, et cetera with the rest of natural numbers, and something like !:* for all the arguments without the command.

The generic form is !n:m for n-th last command line and m-th argument, with an omitted n being a shortcut for the last line.

Not sure if all of this works the same in bash.

Re:Ask HN: Teach me something new by senjin on Sunday May 01, 2022 @09:52AM (#31225011)
I use zsh so this is also going in my toolbox. I think I've reached for !:* in the past and didn't know it so this is useful
Re:Ask HN: Teach me something new by gnubison on Saturday April 30, 2022 @03:35PM (#31218595)
History expansion originated in csh, and was never in the Bourne shell. Bash, certainly.
Re:Ask HN: Teach me something new by schoen on Saturday April 30, 2022 @09:06PM (#31221095)
Thanks for the correction. I often recognize when bashisms are bashisms, but I didn't realize that was.
Re:Ask HN: Teach me something new by mkl on Saturday April 30, 2022 @08:20AM (#31215481)
And if you don't want the last argument but the nth one, press Alt-n before Alt-. (n starts at 0, so Alt-0 Alt-. in the above situation would get you "mkdir").

There are several other ways to do this sort of thing too. See e.g. https://stackoverflow.com/questions/4009412/how-to-use-argum...

Re:Ask HN: Teach me something new by sph on Saturday April 30, 2022 @04:43AM (#31214412)
Holy shit, I have gone 35 years without ever learning this fact.
Re:Ask HN: Teach me something new by sbergjohansen on Saturday April 30, 2022 @05:31AM (#31214597)
!! substitutes the last command:

  sudo !!
and

  cd -
takes you to the previous directory.
Re:Ask HN: Teach me something new by Beldin on Saturday April 30, 2022 @07:08PM (#31220366)
!$ substitutes the last command's arguments, if memory serves.
Re:Ask HN: Teach me something new by pards on Saturday April 30, 2022 @09:01AM (#31215725)
Or ^r to search through the command history (bash only?):

    $ grep blah *
    $ ^r
    (reverse-i-search)`g': grep blah *
This is far and above my most commonly used shell command
Re:Ask HN: Teach me something new by ahnberg on Saturday April 30, 2022 @09:30AM (#31215936)
Then you can even superpowerboost the ^r by installing fzf! https://github.com/junegunn/fzf
Re:Ask HN: Teach me something new by PufPufPuf on Saturday April 30, 2022 @01:12PM (#31217481)
This can be made much more powerful by McFly, I recommend: https://github.com/cantino/mcfly
Re:Ask HN: Teach me something new by burner556 on Saturday April 30, 2022 @02:32AM (#31213867)
or <esc> .
Re:Ask HN: Teach me something new by I_complete_me on Saturday April 30, 2022 @04:52AM (#31214450)
zsh has "take" to mkdir and cd to it. Something like take() { mkdir -p "$1" && cd "$1" }