I find myself repeating a lot of...

mkdir longtitleproject

cd longtitleproject

Is there a way of doing it in one line without repeating the directory name? I'm on bash here.

link|improve this question
1  
mcd from unix.stackexchange.com/questions/6628/… – Mikel Mar 12 '11 at 1:32
feedback

4 Answers

up vote 37 down vote accepted

In your ~/.bashrc:

mkcd () {
  mkdir -p "$1" && cd "$1"
}

Then run mkcd longtitleproject. You might want to omit -p, use pushd instead of cd, give the function a different name, or other variants.

There are also less specialized ways to not have to retype the word from the previous line:

  • Type cd , then Esc . (or Alt+.) to insert the last argument from the previous command.
  • cd !$ executes cd on the last argument of the previous command.
  • Press Up to recall the previous command line, then edit it to change mkdir into cd.
link|improve this answer
Thanks! the Esc . seems the most convenient to me, does the key sequence have any special meaning? – Duopixel Mar 12 '11 at 1:36
It's just the Bash (and inherited from ksh, and also works in zsh) sequence for "repeat last word of previous command". I use it quite often. – geekosaur Mar 12 '11 at 1:46
10  
@Gilles I'm beginning to think that the "Gilles" account is actually shared by a panel of experts. ;-) – Keith Mar 12 '11 at 3:24
1  
Gilles deserves 2 votes: One for the wonderful shortcuts, and another one for the nice Key formatting :-D – pepoluan Mar 12 '11 at 4:32
Hear! Great answer! – Mr. Shickadance Mar 12 '11 at 16:20
show 1 more comment
feedback

As per What customizations have you done on your shell profile to increase productivity?, this is how I do it:

# make a directory and cd to it
mcd()
{
    test -d "$1" || mkdir "$1" && cd "$1"
}

it means it also works if the directory already exists.

link|improve this answer
2  
The -p option to mkdir will suppress errors. – glenn jackman Mar 12 '11 at 5:42
@glenn jackman: Indeed it does, thanks. – Mikel Mar 12 '11 at 6:29
+1 to use test -d and || – Johan Mar 12 '11 at 21:06
mcd is an already existing command. Though you've just given an example, I used it myself as it's a letter shorter than mkcd. – Dharmit Shah Apr 13 '11 at 14:44
@Dharmit Shah: What is the existing mcd command? Which package or project provides this command? – Mikel Apr 13 '11 at 22:05
show 1 more comment
feedback

It would never have occurred to me to script up this behaviour because I enter the following on a near daily basis ...

$ mkdir someDirectory<ENTER>
$ cd !$

where bash kindly substitutes !$ with the last word of the last line; i.e. the long directory name that you entered.

In addition, filename completion is your friend in such situations. If your new directory was the only file in the folder a quick double TAB would give you the new directory without re-entering it.

Although it's cool that bash allows you to script up such common tasks as the other answers suggest I think it is better to learn the command line editing features that bash has to offer so that when you are working on another machine you are not missing the syntactic sugar that your custom scripts provide.

link|improve this answer
feedback

Or you could just create a short variable on-the-fly and use it twice x = longproject ; mkdir $x ; cd $x - which I admit is still longer than using a shellscript function :)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.