12

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?

Lazer
  • 35,307
  • 1
    Use TAB-completion for commands and directories not in your recent history list. cd this<TAB> – invert Aug 26 '10 at 13:37

8 Answers8

10

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.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
jmb
  • 354
  • 3
  • 8
9

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.

Banjer
  • 2,920
7

It's as simple as Alt + .

$ mkdir lasdfjalsdkjf
$ cd 

Alt + .

$ cd lasdfjalsdkjf
Umang
  • 1,171
2

this has always worked for me:

mkdir thisismyfolder
cd $_
Sandy
  • 3,262
  • 2
  • 22
  • 14
  • Watch out for this, though: $! 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
1

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:~$
lcipriani
  • 742
  • If you use an abbreviated prompt it makes posts more readable. (I'm referring to removing the user and hostnames. I understand that showing the PWD instead of just $ is useful when demonstrating pushd, etc.) – Dennis Williamson Aug 25 '10 at 22:49
1

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.

frabjous
  • 8,691
1

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.

Elitmiar
  • 519
  • 3
  • 7
  • 10
1

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

jmanning2k
  • 821
  • 5
  • 5