For example, if I do a
mkdir thisismyfolder912
I remember there is some easier way to switch to thisismyfolder912
than having to do a
cd thisismyfolder912
What is that way and how does it work? Also, what are the other ways I can use this?
For example, if I do a
mkdir thisismyfolder912
I remember there is some easier way to switch to thisismyfolder912
than having to do a
cd thisismyfolder912
What is that way and how does it work? Also, what are the other ways I can use this?
Are you talking about classic history expansion, or Readline processing? cd !$
on the next input line will substitute in the last argument of the previous line, or M-. or M-_ will yank it using Readline.
cd !$
. "Classic history expansion or Readline processing" I don't exactly understand what you mean...
– Lazer
Aug 25 '10 at 15:37
!$
is a history expansion; the shell processes it as it parses your command. M-. is a readline keystroke; readline is the library that handles each key you press, and it sees that you pressed M-. and types in the last argument of the last command for you automatically
– Michael Mrozek
Aug 25 '10 at 17:00
If your question is about accessing command history, then try this well-named command
history
You can also try Ctrl + r, and start typing a command you're trying to remember that you've recently typed.
(reverse-i-search)`cd ': cd mydir/data/
Hit ESC to select the command or exit. This works for me on SuSE at least; not sure about other distros.
history | grep ...
!
– Oli
Aug 25 '10 at 21:04
vi
to manage your command line. Looks super cool. using vi to edit shell commands
– Banjer
Jul 14 '12 at 14:31
It's as simple as Alt + .
$ mkdir lasdfjalsdkjf
$ cd
Alt + .
$ cd lasdfjalsdkjf
this has always worked for me:
mkdir thisismyfolder
cd $_
$!
expands to the last word you actually typed, $_
expands to the last word after expansion - so you might echo /tmp/tmpfile*
and see a bunch of files you want to remove, but then rm $_
will only remove one of them.
– jmb
Mar 13 '14 at 12:40
If you use bash i suggest pushd
and popd
. You can create a stack of directory and browse it rapidly. See this example:
PWD:~$ pushd /opt/google/chrome/resources/
/opt/google/chrome/resources ~
PWD:/opt/google/chrome/resources$ pushd /etc/cron.daily/
/etc/cron.daily /opt/google/chrome/resources ~
PWD:/etc/cron.daily$ pushd /opt/pac/lib/method/
/opt/pac/lib/method /etc/cron.daily /opt/google/chrome/resources ~
PWD:/opt/pac/lib/method$ popd
/etc/cron.daily /opt/google/chrome/resources ~
PWD:/etc/cron.daily$ popd
/opt/google/chrome/resources ~
PWD:/opt/google/chrome/resources$ popd
~
PWD:~$
$
is useful when demonstrating pushd
, etc.)
– Dennis Williamson
Aug 25 '10 at 22:49
Picking up a tip from another thread, if you put:
bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forward
in your .bashrc then, you can start typing something from your history, and then press the up arrow, and then rather than going through your history item by item, it'll skip right to previous entries that begin with what you've already typed.
I guess this doesn't help much with the particular example given in the question, but it is one thing that helps me access history on the fly.
If your shell uses readline (which is the case for bash
) you can do something like pressing ATL+. together?
From the GNU Readline documentation:
yank-last-arg (M-. or M-_)
Insert last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn.
On a related note, I recommend using histverify in bash. Put this in your ~/.bashrc:
shopt -s histverify
This will cause bash to print out the command after expanding !$ or other history functions, and give you a chance to look at it before hitting enter again to actually run it. For me, the sanity check is worth the occasional extra key press. Want to make sure I'm running the cd foo
command, not the rm -rf foo
one...
I frequently use the Ctrl-R approach, as well as Alt-. (which is a good fit for the scenario you describe). I'll use !$ on occasion.
These are very useful general purpose techniques.
But to address your specific question:
Making a directory and cd'ing directly into it is such a common combination that it is useful to have a function to wrap it up..
function mcd {
local newdir='_mcd_command_failed_'
if [ -d "$1" ]; then # Dir exists, mention that
echo "$1 exists..."
newdir="$1"
else
if [ -n "$2" ]; then # We've specified a mode
command mkdir -p -m $2 "$1" && newdir = "$1"
else # Plain old mkdir
command mkdir -p "$1" && newdir="$1"
fi
fi
builtin cd "$newdir" # No matter what, cd into it
}
Usage: mcd thisismyfolder
cd this<TAB>
– invert Aug 26 '10 at 13:37