Lurker News
A command line trick, a random factoid, anything that’s interesting to you.

Comments (762) 128 threads

Show all comments

Showing thread 15 of 128

hiisukun8 replies#
In a lot of terminals, Alt+. (or Option+. on Mac) will paste in the last argument of the previous command.

    $ mkdir hi
    $ cd
You can hit . several times to cycle through earlier final args.
burner556#
or .
schoen2 replies#
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!

senjin2 replies#
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!
schoen1 reply#
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".

Continue this thread — 1 reply

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

Continue this thread — 1 reply

gnubison1 reply#
History expansion originated in csh, and was never in the Bourne shell. Bash, certainly.
schoen#
Thanks for the correction. I often recognize when bashisms are bashisms, but I didn't realize that was.
sph#
Holy shit, I have gone 35 years without ever learning this fact.
I_complete_me#
zsh has "take" to mkdir and cd to it. Something like take() { mkdir -p "$1" && cd "$1" }
sbergjohansen1 reply#
!! substitutes the last command:

  sudo !!
and

  cd -
takes you to the previous directory.
Beldin#
!$ substitutes the last command's arguments, if memory serves.
mikevin#
man 3 readline

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

pards2 replies#
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